|
@ -12,7 +12,7 @@ if __name__ == '__main__': |
|
|
List |
|
|
List |
|
|
---- |
|
|
---- |
|
|
```python |
|
|
```python |
|
|
<list>[<from inclusive>:<to exclusive>:<step size>] |
|
|
|
|
|
|
|
|
<list>[from_inclusive : to_exclusive : step_size] |
|
|
<list>.extend(<list>) |
|
|
<list>.extend(<list>) |
|
|
<list>.sort() |
|
|
<list>.sort() |
|
|
<list>.reverse() |
|
|
<list>.reverse() |
|
@ -22,7 +22,7 @@ sorted_by_second = sorted(<list>, key=lambda tup: tup[1]) |
|
|
|
|
|
|
|
|
#### Flatten List |
|
|
#### Flatten List |
|
|
```python |
|
|
```python |
|
|
[item for sublist in list for item in sublist] |
|
|
|
|
|
|
|
|
[item for sublist in <list> for item in sublist] |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Dictionary |
|
|
Dictionary |
|
@ -32,20 +32,20 @@ Dictionary |
|
|
<dict>.get(<key>, <default>) |
|
|
<dict>.get(<key>, <default>) |
|
|
<dict>.setdefault(<key>, <default>) |
|
|
<dict>.setdefault(<key>, <default>) |
|
|
<dict>.update(<dict>) |
|
|
<dict>.update(<dict>) |
|
|
collections.defaultdict(<type>) # Creates list |
|
|
|
|
|
|
|
|
collections.defaultdict(<type>) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Init from two lists |
|
|
|
|
|
|
|
|
#### Initiates dict from two lists. |
|
|
```python |
|
|
```python |
|
|
dict(zip(keys, values)) |
|
|
dict(zip(keys, values)) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Filter by keys |
|
|
|
|
|
|
|
|
#### Filter by keys |
|
|
```python |
|
|
```python |
|
|
{k: v for k, v in d.iteritems() if k in [2,3]} |
|
|
|
|
|
|
|
|
{k: v for k, v in <dict>.iteritems() if k in keys} |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Counter |
|
|
|
|
|
|
|
|
#### Counter |
|
|
```python |
|
|
```python |
|
|
>>> from collections import Counter |
|
|
>>> from collections import Counter |
|
|
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] |
|
|
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] |
|
@ -90,14 +90,14 @@ TestResults(filed=1, attempted=2) |
|
|
|
|
|
|
|
|
Iterator |
|
|
Iterator |
|
|
-------- |
|
|
-------- |
|
|
Reads input until it reaches empty line. |
|
|
|
|
|
|
|
|
#### Reads input until it reaches empty line. |
|
|
```python |
|
|
```python |
|
|
for line in iter(input, ''): |
|
|
for line in iter(input, ''): |
|
|
print(line) |
|
|
print(line) |
|
|
``` |
|
|
``` |
|
|
Use partial from functools if function needs arguments. |
|
|
Use partial from functools if function needs arguments. |
|
|
|
|
|
|
|
|
Skips first element. |
|
|
|
|
|
|
|
|
#### Skips first element |
|
|
```python |
|
|
```python |
|
|
next(<iter>) |
|
|
next(<iter>) |
|
|
for element in <iter>: |
|
|
for element in <iter>: |
|
@ -191,7 +191,7 @@ now.strftime('%Y%m%d%H%M%S') |
|
|
|
|
|
|
|
|
Arguments |
|
|
Arguments |
|
|
--------- |
|
|
--------- |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
args = (1, 2) |
|
|
args = (1, 2) |
|
|
kwargs = {'x': 3, 'y': 4, 'z': 5} |
|
|
kwargs = {'x': 3, 'y': 4, 'z': 5} |
|
|
func(*args, **kwargs) |
|
|
func(*args, **kwargs) |
|
@ -220,7 +220,7 @@ lambda: <return value> |
|
|
(x+5 for x in range(0, 10)) - generator |
|
|
(x+5 for x in range(0, 10)) - generator |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
[i+j for i in range(10) for j in range(10)] |
|
|
[i+j for i in range(10) for j in range(10)] |
|
|
# Same as: |
|
|
# Same as: |
|
|
for i in range(10): |
|
|
for i in range(10): |
|
@ -236,12 +236,12 @@ functools.reduce(combining_function, list_of_inputs) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Any, All |
|
|
### Any, All |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
any(a[1] for a in aaa) |
|
|
any(a[1] for a in aaa) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### If - Else |
|
|
### If - Else |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
expression_if_true if condition else expression_if_false |
|
|
expression_if_true if condition else expression_if_false |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
@ -256,8 +256,8 @@ def mult_clos(x): |
|
|
mul_by_3 = mult_clos(3) |
|
|
mul_by_3 = mult_clos(3) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
or |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
#### or |
|
|
|
|
|
```python |
|
|
from functools import partial |
|
|
from functools import partial |
|
|
partial(<function>, <parameter>) |
|
|
partial(<function>, <parameter>) |
|
|
``` |
|
|
``` |
|
@ -270,8 +270,8 @@ def function_that_gets_passed_to_closure(): |
|
|
... |
|
|
... |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Debugger example |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
#### Debugger example |
|
|
|
|
|
```python |
|
|
from functools import wraps |
|
|
from functools import wraps |
|
|
|
|
|
|
|
|
def debug(func): |
|
|
def debug(func): |
|
@ -351,7 +351,7 @@ sys.argv |
|
|
with open(<filename>, encoding='utf-8') as file: |
|
|
with open(<filename>, encoding='utf-8') as file: |
|
|
return file.readlines() |
|
|
return file.readlines() |
|
|
``` |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
def get_file_contents(file_name): |
|
|
def get_file_contents(file_name): |
|
|
with open(file_name, encoding='utf-8') as f: |
|
|
with open(file_name, encoding='utf-8') as f: |
|
|
return f.readlines() |
|
|
return f.readlines() |
|
@ -375,7 +375,7 @@ filename = input('Enter a file name: ') |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Print lines until EOF |
|
|
Print lines until EOF |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
while True: |
|
|
while True: |
|
|
try: |
|
|
try: |
|
|
print(input()) |
|
|
print(input()) |
|
@ -424,7 +424,7 @@ db.commit() |
|
|
|
|
|
|
|
|
Exceptions |
|
|
Exceptions |
|
|
---------- |
|
|
---------- |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
while True: |
|
|
while True: |
|
|
try: |
|
|
try: |
|
|
x = int(input("Please enter a number: ")) |
|
|
x = int(input("Please enter a number: ")) |
|
@ -519,7 +519,7 @@ from itertools import * |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Islice |
|
|
### Islice |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
islice([1, 2, 3], 1, None) |
|
|
islice([1, 2, 3], 1, None) |
|
|
[2, 3] |
|
|
[2, 3] |
|
|
``` |
|
|
``` |
|
@ -530,7 +530,7 @@ Filter, map and zip functions that return generators instead of iterators |
|
|
|
|
|
|
|
|
Introspection and Metaprograming |
|
|
Introspection and Metaprograming |
|
|
-------------------------------- |
|
|
-------------------------------- |
|
|
Inspecting code at runtime and code that generates code. |
|
|
|
|
|
|
|
|
#### Inspecting code at runtime and code that generates code. |
|
|
|
|
|
|
|
|
```python |
|
|
```python |
|
|
>>> class B: |
|
|
>>> class B: |
|
@ -547,7 +547,7 @@ Inspecting code at runtime and code that generates code. |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
same as |
|
|
same as |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
B.__getattribute__(b, 'a') |
|
|
B.__getattribute__(b, 'a') |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
@ -578,7 +578,7 @@ b = BB() |
|
|
|
|
|
|
|
|
MetaClass |
|
|
MetaClass |
|
|
--------- |
|
|
--------- |
|
|
Classes that creates classes. |
|
|
|
|
|
|
|
|
#### Classes that creates classes. |
|
|
```python |
|
|
```python |
|
|
def my_meta_class(name, parents, attrs): |
|
|
def my_meta_class(name, parents, attrs): |
|
|
... do stuff |
|
|
... do stuff |
|
@ -613,7 +613,7 @@ class BlaBla: |
|
|
|
|
|
|
|
|
Eval |
|
|
Eval |
|
|
---- |
|
|
---- |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
import ast |
|
|
import ast |
|
|
import operator as op |
|
|
import operator as op |
|
|
|
|
|
|
|
@ -651,7 +651,7 @@ Libraries |
|
|
|
|
|
|
|
|
Plot |
|
|
Plot |
|
|
---- |
|
|
---- |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
import matplotlib |
|
|
import matplotlib |
|
|
matplotlib.pyplot.plot(<data>, ...) |
|
|
matplotlib.pyplot.plot(<data>, ...) |
|
|
matplotlib.pyplot.show() |
|
|
matplotlib.pyplot.show() |
|
@ -660,12 +660,12 @@ matplotlib.pyplot.savefig(<filename>) |
|
|
|
|
|
|
|
|
Web |
|
|
Web |
|
|
--- |
|
|
--- |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
import bottle |
|
|
import bottle |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Run |
|
|
### Run |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
bottle.run(host='localhost', port=8080) |
|
|
bottle.run(host='localhost', port=8080) |
|
|
bottle.run(host='0.0.0.0', port=80, server='cherypy') |
|
|
bottle.run(host='0.0.0.0', port=80, server='cherypy') |
|
|
``` |
|
|
``` |
|
@ -679,7 +679,7 @@ bottle.run(host='0.0.0.0', port=80, server='cherypy') |
|
|
|
|
|
|
|
|
Curses |
|
|
Curses |
|
|
------ |
|
|
------ |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
import curses |
|
|
import curses |
|
|
def main(): |
|
|
def main(): |
|
|
curses.wrapper(draw) |
|
|
curses.wrapper(draw) |
|
@ -690,19 +690,19 @@ def draw(screen): |
|
|
pass |
|
|
pass |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Get char from int |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
#### Gets char from int |
|
|
|
|
|
```python |
|
|
chr(<int>) |
|
|
chr(<int>) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
Profile |
|
|
Profile |
|
|
------- |
|
|
------- |
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
import timeit |
|
|
import timeit |
|
|
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) |
|
|
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
```python |
|
|
import pycallgraph |
|
|
import pycallgraph |
|
|
graph = pycallgraph.output.GraphvizOutput() |
|
|
graph = pycallgraph.output.GraphvizOutput() |
|
|
graph.output_file = "{}-{}.png".format("profile", |
|
|
graph.output_file = "{}-{}.png".format("profile", |
|
@ -720,8 +720,8 @@ def get_datetime_string(a_datetime): |
|
|
|
|
|
|
|
|
Audio |
|
|
Audio |
|
|
----- |
|
|
----- |
|
|
Saves list of floats of size 0 to 1 to a WAV file. |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
#### Saves list of floats of size 0 to 1 to a WAV file. |
|
|
|
|
|
```python |
|
|
import wave |
|
|
import wave |
|
|
import struct |
|
|
import struct |
|
|
frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>] |
|
|
frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>] |
|
|