Friday, June 9, 2017

python practice

check if dict has key

if key in dict

python 3.6 install package

after 3.4, pip is not standalone, to install a package, run
python -m pip install SomePackage

functools.partial

returns a function with partially providing the parameters
https://docs.python.org/2/howto/functional.html 

python threads pool

csvFiles = list(filter(isFile, os.listdir("d:/quotes")))
with  multiprocessing.Pool(multiprocessing.cpu_count() - 1) as p:
    p.map(loadCsv, csvFiles)
Here loadCsv is the function, csvFiles is the list to iterate
https://docs.python.org/3.6/library/multiprocessing.html

pickle

with open('filename', 'a') as f:
        pickle.dump(data, f)

with open('filename', 'rb') as f:
    x = pickle.load(f)

python prirnt current exception

import traceback
traceback.print_exc()

zip two arrays into tuples

let's say you have two arrays [-1, 0, 1, 2] and [7,8,9,10]
you want to merge them into tuples like this [(-1, 7), (0, 8), (1, 9), (2, 10)]
list(zip(array1, array2))

array filter

result = [a for a in A if a not in subset_of_A]

python function argument

http://detailfocused.blogspot.ca/2017/07/python-function-parameters.html

define function inside function

def method_a(arg):

    def method_b(arg):
        return some_data

    some_data = method_b

No comments:

Post a Comment