Python CheatSheet

This cheatsheet includes some very basic but easy to forget operations and some random notes.
A good tutorial for beginner in Chinese

1. Decorator Syntax

def decorator(func):  
	def new_func(*args, **argkw):  
		#add stuff
		print("Hello World")   
		return func(*args, **argkw)
return new_func 
 
@decorator  
def f(args):
	pass

#run function f
f()
#result:
#Hello World

2. Open file, read, write

Open: f = open(“hello.text”, flag), flag: 'r' = read, 'b' = binary, 'w' = write
read sing line:
f.readline() 
read a list of lines:
f.readlines()
read whole file as string:
f.read()

3. Read from stdin

import sys
for line in sys.stdin.readlines():
	#convert each line from a string to a list of integer
	line_list = (map(int, line.split()))

4. look up table of element, origin index -> the sorted index

idx = np.argsort()
lut = np.zeros_like(idx)  
lut[idx] = np.arange(len(origin_list))

lut[origin_index] = sorted_index

5. load object and save object

def save_obj(obj, name ):
    with open('obj/'+ name + '.pkl', 'wb') as f:
        pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)

def load_obj(name ):
    with open('obj/' + name + '.pkl', 'rb') as f:
        return pickle.load(f)

6. xrange and range

difference between list[0,1,2,3,4] and range(0,5) in python 3.0+
difference between range(0,5) and xrange(0,5) in python before 3.0)

the former one needs to allocate the actuall space on memory, the later one doesn't have to.
The later ones returns a range object( a iterable object similar to iteratorbut allows random access), so allocate memory on demand.

7. install different python version packages using pip

python 2.x:
pip2 install -r requirements.txt
python 3.x:
pip3 install -r requirements.txt

8. minHeap and maxHeap

import heapq
mylis = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
heapq.heapify(mylis) # for a min heap
heapq.heappop(mylis) # pop from minheap

heapq._heapify_max(mylis)
heapq._heappop_max(mylis)

9. init a NxM 2d array

n, m = 8, 5;
matrix = [[0 for x in range(m)] for y in range(n)]

10. topological ordering

import networkx as nx

G = nx.DiGraph()
G.add_edges_from([(0, 2), (1, 2), (2, 3)])
all_topological_sorts = list(nx.algorithms.dag.all_topological_sorts(G))
print([0, 1, 2, 3] in all_topological_sorts) # True
print([2, 3, 1, 0] in all_topological_sorts) # False