diff --git a/README.md b/README.md index a9868a3..8c713e2 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,11 @@ Dictionary .get(, ) .setdefault(, ) .update() +collections.defaultdict() # Creates list +``` +Init from two lists +``` +dict(zip(keys, values)) ``` Set @@ -54,6 +59,30 @@ Enumerate for i, in enumerate() ``` +Named Tuples +------------ +``` +>>> TestResults = collections.namedtuple('TestResults', ['filed', 'attempted']) +>>> TestResults(1, 2) +TestResults(filed=1, attempted=2) +``` + + +Iterator +-------- +Reads input until it reaches empty line. +``` +for line in iter(input, ''): + print(line) +``` +Use partial from functools if function needs arguments. + +Skips first element. +``` +next() +for element in : + ... +``` Type ---- @@ -103,6 +132,12 @@ re.search(, ) {:.f} -> ' 3.14' ``` +```python +>>> person = {'name': 'Jean-Luc', 'height': 187.1} +>>> '{p[height]:.0f}'.format(p=person) +'187' +``` + ### Text Wrap ```python import textwrap @@ -134,6 +169,15 @@ now.strftime('%Y%m%d') now.strftime('%Y%m%d%H%M%S') ``` +Args +---- +``` +args = (1, 2) +kwargs = {'x': 3, 'y': 4, 'z': 5} +func(*args, **kwargs) +# same as +func(1, 2, x=3, y=4, z=5) +``` Inline ------ @@ -154,8 +198,8 @@ lambda: ### Map, Filter, Reduce ```python -A. map(lambda x: x+1, range(10)) -B. filter(lambda x: x>5, range(10)) +map(lambda x: x+1, range(10)) +filter(lambda x: x>5, range(10)) functools.reduce(combining_function, list_of_inputs) ``` @@ -164,13 +208,18 @@ Closure ------- ```python def mult_clos(x): - def wrapped(y): + def wrapped(y): return x * y - return wrapped + return wrapped mul_by_3 = mult_clos(3) ``` +or +``` +from functools import partial +partial(, ) +``` Decorator --------- @@ -180,6 +229,22 @@ def function_that_gets_passed_to_closure(): ... ``` +Debugger example +``` +from functools import wraps + +def debug(func): + @wraps(func) # Nedded for metadata copying (func name, etc) + def wrapper(*args, **kwargs): + print(func.__name__) + return func(*args, **kwargs) + return wrapper + +@debug +def add(x, y): + return x + y +``` + Generator --------- @@ -211,7 +276,7 @@ class : ```python import enum class (enum.Enum): - = + = # or auto() ``` ### Copy @@ -254,6 +319,14 @@ os.popen().read() filename = input('Enter a file name: ') ``` +Print lines until EOF +``` +while True: + try: + print(input()) + except EOFError: + break +``` JSON ---- @@ -297,6 +370,17 @@ db.commit() ``` +Exceptions +---------- +``` +while True: + try: + x = int(input("Please enter a number: ")) + break + except ValueError: + print("Oops! That was no valid number. Try again...") +``` + Threading --------- ```python @@ -405,6 +489,11 @@ Inspecting code at runetime and code that generates code. 'sdfsd' ``` +same as +``` +B.__getattribute__(b, 'a') +``` + ### Hasattr ```python >>> hasattr(b, 'c')