Environ

import os, pprint 
pprint.pprint(dict(os.environ))
home = os.envron['HOME']
user = os.environ['USER']

Ternary operation

_min = a if a < b else b

List

copy = arr[:]
rcopy= arr[::-1]

FILE

t   Opens in text mode. (default)
b   Opens in binary mode.
Read Only (‘r’)
Read and Write (‘r+’)
Write Only (‘w’)
Write and Read (‘w+’)
Append Only (‘a’)
Append and Read (‘a+’)

f.write(str1) Writes the string s to the file and returns the number of characters written.
f.writelines(L) : L = [str1, str2, str3]

s = read(n)  Reads at most n characters from the file. Reads till end of file if it is negative or None.
s = f.readline()
L = f.readlines L = [str1, str2, str3]

# Changes the file position to offset bytes, in reference to from (start, current, end) # seek(offset,from=SEEK_SET ) print("FILE"*100) import os.path filename = "file.txt" result = os.path.exists(filename) result = os.path.isfile(filename) result = os.path.isdir(filename) import datetime with open(filename, "wt") as f: for _ in range(100): f.write(f"{datetime.datetime.now()}") f.write('\n') with open(filename) as f: for line in f: print(line, end="")

 


import os, sys
with open(os.path.join(sys.path[0],"files.py"), "rt") as f:
while (byte := f.read(1)):
print(byte, end="")
with open(os.path.join(os.path.dirname(__file__),"files.py"), "rb") as f:
r = f.read(10)
print(r.decode())
f.seek(0)
while (byte := f.read(1)):
print(chr(int.from_bytes(byte,'big')), end="")
with open("ss.bin", "wb") as f:
for i in range(10):
f.write(i.to_bytes(1,"big"))
with open("ss.bin", "rb") as f:
while i := f.read(1):
print(int.from_bytes(i, "big"))

Jason
import json data = ['foo', {'bar': ('baz', None, 1.0, 2)}] #### JUMPS: Data TO String #### JUMP: Data TO Json File s = json.dumps(data) print(type(s)) ### LOADS: Json String to Data ### LOAD: Json File to Data d = json.loads(s) print(d[1]["bar"]) filename = "foo.json" with open(filename, "wt") as f: json.dump(data, f) with open(filename) as f: d = json.load(f) print(d[1]["bar"])
Pickle
# Pickle is used for serializing and de-serializing Python object structures, also called marshalling
# or flattening.
# Serialization refers to the process of converting an object in memory to a byte stream that can be
# stored on disk or sent over a network. Later on, this character stream can then be retrieved and
# de-serialized back to a Python object.import collections
import collections
class BinaryTree:
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __init__(self, array):
queue = collections.deque()
if len(array) >= 1:
self.root = BinaryTree.Node(array[0])
queue.append(self.root)
i = 1
else:
self.root = None
i = 0
while i < len(array):
l, r = None, None
if array[i]:
l = BinaryTree.Node(array[i])
queue.append(l)
i += 1
if i < len(array) and array[i]:
r = BinaryTree.Node(array[i])
queue.append(r)
i += 1
node = queue.popleft()
node.left = l
node.right = r
def __str__(self):
def traverse(node, array):
queue = collections.deque()
queue.append(node)
while queue:
n = queue.popleft()
if n == None:
array.append(None)
else:
array.append(n.val)
# if n.left:
queue.append(n.left)
# if n.right:
queue.append(n.right)
array = []
traverse(self.root, array)
tf = list(map(lambda i: ascii(i) + " ", array))
return "".join(tf)

import pickle
array = [1, 2, 3, None, 4, 5, None, None, None, 8, 9, 10]
btree = BinaryTree(array)
with open("tree.bin", "wb") as f:
pickle.dump(btree, f)
with open("tree.bin", "rb") as f:
btree = pickle.load(f)
print(btree)


 

 

Time
date – contains only date information (year, month, day). time – refers to time independent of the day (hour, minute, second, microsecond). datetime – combines date and time information. timedelta – represents the difference between two dates or times. tzinfo – an abstract class for manipulating time zone information. %Y – year with century (e.g., 2019). %y – year without century (e.g., 19). %B – month as a full name (e.g., March). %b – month as an abbreviated name (e.g., Mar). %m – month as a zero-padded integer (e.g., 03). %d – day of the month as a zero-padded integer (e.g., 25). %A – weekday as a full name (e.g., Monday). %a – weekday as an abbreviated name (e.g., Mon). Converting Strings into Datetime Objects date = datetime.strptime('March 25, 2019', '%B %d, %Y') datetime.strptime(mapper[id][0][0] + " " + mapper[id][0][1],'%Y-%m-%d %H:%M:%S') Converting datetime into string now = datetime.now() print(now.strftime("%m/%d/%y, %H:%M:%S")) now.strftime("%d/%m/%Y %H:%M:%S") Getting Time Intervals (No hours, no months, no years) days = (datetimeend-datetimestart).days seconds = (datetimeend-datetimestart).seconds microseconds = (datetimeend-datetimestart).microseconds import time time.sleep(2)
epoch_time = time.time() #1634865565.063788
datetime_time = datetime.datetime.fromtimestamp(epoch_time)

 

API
from urllib.parse import urlparse
domain = urlparse('http://www.example.test/foo/bar')
#ParseResult(scheme='http', netloc='www.example.test', path='/foo/bar', params='', query='', fragment='')
domain = urlparse('http://example.test/foo/bar')
#ParseResult(scheme='http', netloc='example.test', path='/foo/bar', params='', query='', fragment='')
domain = urlparse('www.example.test/foo/bar') #ParseResult(scheme='', netloc='', path='www.example.test/foo/bar', params='', query='', fragment='')
domain = urlparse('example.test/foo/bar')
#ParseResult(scheme='', netloc='', path='example.test/foo/bar', params='', query='', fragment=''
print(domain.path) # example.test/foo/bar


import requests
URL ="https://api.github.com/repos/pytorch/pytorch/releases/latest"
parms = {}
response = requests.get(url=URL, params=parms)
dataform = json.loads(response.text)
print(dataform)

URL = "http://numbersapi.com/" + ascii(34)
response = requests.get(url=URL, params={})
print(response.text)

r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
'{"type":"User"...'
>>> r.json()
{'private_gists': 419, 'total_private_repos': 77, ...}

import aiohttp
import asyncio
import time

start_time = time.time()
async def get_pokemon(session, url):
async with session.get(url) as resp:
pokemon = await resp.json()
return pokemon['name']

async def main():
async with aiohttp.ClientSession() as session:
tasks = []
for number in range(1, 151):
url = f'https://pokeapi.co/api/v2/pokemon/{number}'
tasks.append(asyncio.ensure_future(get_pokemon(session, url)))
original_pokemon = await asyncio.gather(*tasks)
for pokemon in original_pokemon:
print(pokemon)

asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))
 

 

Exception
try: raise ValueError except Exception as e: print(e) else: print("result is", result) finally: print('finally!') Exception +-- StopIteration +-- AssertionError +-- FileNotFoundError +-- RuntimeError | +-- RecursionError +-- ValueError +-- LookupError | +-- IndexError | +-- KeyError +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError

Collections
import colections keyTolist = collections.defaultdict(list) keyTolist = collections.defaultdict(int) q = collections.deque() q.append("b") q.appendleft("a") q.popleft() q.reverse() c = collections.Counter(list) c = collections.Counter("changpil")

a_list = ["a", "b", "b", "c", "f", "b", "c"]
a_counter = collections.Counter(a_list)
sorted_by_count = a_counter.most_common()
print(sorted_by_count)
#[('b', 3), ('c', 2), ('a', 1), ('f', 1)]

heapq & maxheap, iteration

import heapq
heap = []
heapq.heappush(heap, 10)
heapq.heappush(heap, 6)
print(heapq.heappop(heap)) # 6
heapq.heappush(heap, 6)
heapq.heappush(heap, 9)
print(heapq.nlargest(2, heap)) # [10, 9]
print(heapq.nsmallest(2, heap)) # [6, 9]

l = [[1,2], [3,3], [3, 3], [1, 5]]
heapq.heapify(l)  # [[1, 2], [1, 5], [3, 3], [3, 3]]

class MaxHeap:
    def __init__(self):
        self.heap = []
    def push(self, num):
        heapq.heappush(self.heap, -num)
    def pop(self):
        return -heapq.heappop(self.heap)
    def heapify(self, arr):
        heapq.heapify(map(lambda x: -x, arr))
self.heap = arr def __len__(self): return len(self.heap) def __getitem__(self, i): return -self.heap[i] def __iter__(self): self.n = 0 return self def __next__(self, default = StopIteration): if self.n >= len(self): if default == StopIteration: raise StopIteration else: return default rev = self.heap[self.n] self.n += 1 return rev def __str__(self): return f"{self.heap}" import random h = MaxHeap() for _ in range(20): h.push(random.randint(-20, 20)) for n in h: print(n, end = " ") print() it = iter(h) for _ in range(23): print(next(it, None), end=" ")

 

itertools

import itertools
bills = [20, 20, 20, 10, 10, 10, 10, 10, 5, 5, 1, 1, 1, 1, 1]
list(itertools.combinations(bills, 3)) #[(20, 20, 20), (20, 20, 10), (20, 20, 10), ... ]
list(combinations([1, 2, 3], 2)) # (1, 2), (1, 3), (2, 3)
list(itertools.permutations(['a', 'b', 'c'])) # [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
list(permutations('abc')) # [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

 


# Number of Permutation
def nPr(n, r):
return int(math.factorial(n)//math.factorial(n-r))
# Number of Combination
def nCr(n, r):
return int(math.factorial(n)/(math.factorial(n-r)*math.factorial(r)))

list(zip(itertools.count(), ['a', 'b', 'c'])) # [(0, 'a'), (1, 'b'), (2, 'c')]
ranks = ['A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2']
suits = ['H', 'D', 'C', 'S']
cards = ((rank, suit) for rank in ranks for suit in suits)
cards = itertools.product(ranks, suits)
import random
random.shuffle(deck)
list(itertools.product([1, 2], ['a', 'b'])) # [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
list(itertools.chain('ABC', 'DEF')) #['A' 'B' 'C' 'D' 'E' 'F']
list(itertools.chain([1, 2], [3, 4, 5, 6], [7, 8, 9])) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
list(itertools.chain([23,4], "asas", {"3":[4,5]})) # [23, 4, 'a', 's', 'a', 's', '3']

functools

import functools
l = [1, 3, 5, 6, 2 ]
functools.reduce(lambda a, b: a*b, l)

Bit Operation

import hashlib
def getbyte(b ):
abyte = 0
for i in range(8):
abyte += (b & 1 << i)
return abyte

 

def main():
bytea = hashlib.md5('http://stackoverflow.com212121'.encode()).digest()
num = 0
for b in bytea:
num <<= 8
num |= b
for i in range(16):
print(getbyte(num))
num >>= 8

Big Endian Byte Order (PowerPC (MAC, SPARK, Motolora, JAVA Virtual Machine), TCP/IP NETWORK ) :
The most significant byte (the "big end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.

Little Endian Byte Order (x86 chipset (Intel)) :
The least significant byte (the "little end") of the data is placed at the byte with the lowest address.
The rest of the data is placed in order in the next three bytes in memory.

>>> i = 20
>>> i.to_bytes(2, 'little')
b'\x14\x00'
>>> i.to_bytes(2, 'big')
b'\x00\x14'

Design an algorithm to encode a list of strings to a string.
class Codec:
def encode(self, strs: [str]) -> str:
S = ""
for s in strs:
l = self.getStrLength(s)
S += l + s
return S

def decode(self, s: str) -> [str]:
strs = []
i = 0
while i < len(s):
l = self.getIntLength(s[i: i + 4])
i += 4
strs.append(s[i: i+l]).
i += l
return strs
def getStrLength(self, s):
l = len(s)
bytes = [chr(l >> (i * 8) & 0xff) for i in range(4)]
bytes_str = ''.join(bytes)
return bytes_str

def getIntLength(self, bytes_str):
result = 0
for ch in bytes_str:
result = result << 8 | ord(ch)
return result
 

Fractions

import fractions
f1 = fractions.Fraction(3/4)
f2 = fractions.Fraction(3/1)
i = int(f1 + f2)
f = float(f1 + f2)

 


THREAD

The Python Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread 
to hold the control of the Python interpreter. Since the GIL allows only one thread to execute at a time even
in a multi-threaded architecture with more than one CPU core, the GIL has gained a reputation
as an “infamous” feature of Python. The GIL is a single lock on the interpreter itself which adds a rule
that execution of any Python bytecode requires acquiring the interpreter lock. This prevents deadlocks
(as there is only one lock) and doesn’t introduce much performance overhead. But it effectively makes
any CPU-bound Python program single-threaded.

 

import threading import logging logging.basicConfig( level=logging.INFO, format='[%(threadName)-10s] %(message)s' ) logging.info(f'%(asctime)s {threading.current_thread()} has slept for {e - s:.2f} seconds and exit now') normal thread: the threads executed and then main thread exits and terminates the program. daemon thread: program exits even though daemon thread was running Thread.join: Main program will hold the sub thread in the line until it finsishes lock = threading.Lock() threads = [] for _ in range(1000): threads.append(threading.Thread(target=sharedSrcFunction, args=(lock,))) for t in threads: t.start() for t in threads: t.join() def sharedSrcFunction(lock) lock.acquire() increment() lock.release()


Running Linux Commands

import os
cmd = 'ls -l'
os.system(cmd)

import subprocess
servers = {"localhost":"localhost", "Printer":"192.168.1.16"}
cmd = 'ping'
for server in servers:
    p = subprocess.Popen([cmd, '-c 1', servers[server]], stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
    stdout, err = p.communicate()
    print(err, " : ", stdout.decode())

String

Boolean String Methods
str.isupper(): String’s alphabetic characters are all uppercase str.islower(): String’s alphabetic characters are all lower case str.istitle(): String is in title case str.isspace(): String only consists of whitespace characters str.isnumeric(): String only contains numeric characters str.isalnum(): String contains only alphanumeric characters (no symbols) str.isalpha(): String contains only alphabetic characters (no numbers or symbols)

 


Split and Join and partition and splitlines Strings
arr = ['Split', 'and', 'Join'] "".join(arr) # 'Split and Join' words = list(map(lambda x: x.strip(), str.split()) words = list(map(lambda x: x.upper(), str.split()) words = list(map(lambda x: x.lower(), str.split()) s = "Split and Join" l = s.partition("and") # ('Split ', 'and', ' Join') s = "Split and Join" l = s.partition("anp") # ('Split and Join', '', '') splitlines(self, /, keepends=False) | Return a list of the lines in the string, breaking at line boundaries. | | Line breaks are not included in the resulting list unless keepends is given and | true. str.upper() str.lower() str.capitalize()

Starts With and Ends With

full_name = "Bill Jefferson"
full_name.startswith("Bill")  True/False
full_name.endswith("Bill")

 


Search for list and string

List
['a','b','c','d','e','b','c'].count("c") # 2 ['a','b','c','d','e','b','c'].index("c") # 2

 


String
"SyntaxError: invalid syntax".count("Error") # 1 "SyntaxError: invalid syntax".count("Errors") # -1 "SyntaxError: invalid syntax".index("Error") # 6 "SyntaxError: invalid syntax".index("Errors") # ValueError "SyntaxError: invalid syntax".find("Error") # 6 "SyntaxError: invalid syntax".find("Errors") # -1


Built-in
hash("a") # quickly compare dictionary keys during a dictionary lookup id(obj) # obj1 is obj2 is called, the id values of both the objects is compared, not their hash values. type(obj) bool([])/bool(-1)/bool("") # True/False chr(48) '0'/chr(65) 'A'/chr(97) 'a' ord('0') / ord('a') / ord('A') dif = ord(c) - ord('a') quotient, remainder = divmod(23,10) abs(-12)
Number to String
ascii(23) # '23' bin(3) '0b11' hex(3) '0x3' oct(3) '0o3'
String To Number
int("101") # 101 int("101", 2) # 5 int('0b11',2) # 3 int('0o3',8) # 3 int('0x3', 16) # 3
Map, Zip, Filter, iter
l = [1,2,3,4,5,6,7,8,9] l =list(map(lambda x: x%2 == 1, l) # [True, False, True, False, True, False, True, False, True] l= list(map(lambda x: x%2, l) # [1, 0, 1, 0, 1, 0, 1, 0, 1] l = list(filter(lambda x: x%2 == 1, l)) # [1, 3, 5, 7, 9] l = list(zip([1,2,3,4],[3,4,5,6])) #[(1, 3), (2, 4), (3, 5), (4, 6)] L1 = [1, 2, 3] L2 = [2, 3, 4] l = list(map(lambda x, y: x*y, L1, L2)) # [2, 6, 12] list(map(len, ['abc', 'de', 'fghi'])) #[3, 2, 4] list(map(sum, zip([1, 2, 3], [4, 5, 6]))) # [5, 7, 9] it = iter([1,2]) next(it) # 1 next(it) # 2 next(it) # StopIteration next(it, None) # None