-
Notifications
You must be signed in to change notification settings - Fork 19
/
python.txt
44 lines (37 loc) · 1.05 KB
/
python.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Quick setup with venv
$ python3 -m venv env
$ source env/bin/activate
$ python3 -m pip install -r requirements.txt
Use Map reduce to divide CPU intensive tasks:
*def get_paths():
return []
def get_inpus():
return []
pool = Pool(4)
paths = pool.map(get_paths, get_input())*
Comprehensions
*EXP for x in seq*
With filter
*EXP for x in seq if COND *
You need to a assign it to a data structure:
*x = [COMP] # list
set(COMP) lis(COMP) str(COMP) iter(COMP)*
Python functional
Iterators
*iter(obj)
next()
list(iter), tuple(iter) -> converts iterator to list*
Generator expressions and list comprehensions: Perform an operation to every element, select a subset of elemnts that meet a condition.
*iter = (elem.func() for elem in list)
list = [elem.func() for elem in list]*
You can select elements that meet a specific condition:
*list = [elem.func() for elem in list
if elem != ""]*
pyenv
*$ mkdir foo ;\
virtualenv foo ;\
cd foo ;\
source ./bin/activate ;\
pip install bar ;\
python baz.py ;\
deactivate*