diff --git a/README.md b/README.md index 221a998..d70079b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Comprehensive Python Cheatsheet ![Monty Python](web/image_888.jpeg) + Main ---- ```python @@ -72,6 +73,7 @@ Counter({'blue': 3, 'red': 2, 'yellow': 1}) 'blue' ``` + Set --- ```python @@ -96,6 +98,7 @@ Set = frozenset() ``` + Range ----- ```python @@ -105,6 +108,7 @@ range(from_inclusive, to_exclusive, step_size) range(from_inclusive, to_exclusive, -step_size) ``` + Enumerate --------- ```python @@ -112,6 +116,7 @@ for i, in enumerate( [, i_start]): ... ``` + Named Tuple ----------- ```python @@ -126,6 +131,7 @@ Point(x=1, y=2) ('x', 'y') ``` + Iterator -------- #### Skips first element: @@ -285,9 +291,9 @@ import textwrap textwrap.wrap(text, width) ``` + Numbers ------- - ### Basic Functions ```python round( [, ndigits]) @@ -332,6 +338,7 @@ import random random.shuffle() ``` + Datetime -------- ```python @@ -346,6 +353,7 @@ now.strftime('%Y%m%d%H%M%S') # '20180315002834' = datetime.strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M') ``` + Arguments --------- **"*" is the splat operator, that takes a list as input, and expands it into actual positional arguments in the function call:** @@ -384,6 +392,7 @@ def add(*a): [2, 3] ``` + Inline ------ ### Lambda @@ -446,6 +455,7 @@ Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3}) Creature = type('Creature', (), {'position': Point(0, 0), 'direction': Direction.n}) ``` + Closure ------- ```python @@ -463,6 +473,7 @@ from functools import partial partial(, [, , ...]) ``` + Decorator --------- ```python @@ -504,14 +515,23 @@ class : return cls.__name__ ``` -#### Constructor overloading: +### Constructor Overloading ```python class : def __init__(self, a=None): self.a = a ``` -### Enum +### Copy +```python +import copy + = copy.copy() + = copy.deepcopy() +``` + + +Enum +---- ```python from enum import Enum, auto class (Enum): @@ -543,7 +563,7 @@ list(a.name for a in ) # == ['', '', ...] random.choice(list()) # == random ``` -#### Inline: +### Inline ```python Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon']) Cutlery = Enum('Cutlery', 'knife fork spoon') @@ -558,17 +578,9 @@ LogicOp = Enum('LogicOp', {'AND': (auto(), lambda l, r: l and r), 'OR' : (auto(), lambda l, r: l or r)} ``` -### Copy -```python -import copy - = copy.copy() - = copy.deepcopy() -``` - System ------ - ### Arguments ```python import sys @@ -615,7 +627,6 @@ b'.\n..\nfile1.txt\nfile2.txt\n' 0 ``` - ### Input ```python filename = input('Enter a file name: ') @@ -630,6 +641,7 @@ while True: break ``` + JSON ---- ```python @@ -662,6 +674,7 @@ def write_to_json_file(filename, an_object): json.dump(an_object, file, ensure_ascii=False, indent=2) ``` + SQLite ------ ```python @@ -683,6 +696,7 @@ db.execute() db.commit() ``` + Exceptions ---------- ```python @@ -701,6 +715,7 @@ while True: raise IOError("input/output error") ``` + Bytes ----- **Bytes objects are immutable sequences of single bytes.** @@ -738,6 +753,7 @@ def write_bytes(filename, bytes): = b''.join() ``` + Struct ------ **This module performs conversions between Python values and C structs represented as Python Bytes objects:** @@ -768,6 +784,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03' * `f` - float * `d` - double + Hashlib ------- ```python @@ -775,6 +792,7 @@ Hashlib '33d0eba106da4d3ebca17fcd3f4c3d77' ``` + Threading --------- ```python @@ -797,6 +815,7 @@ lock.acquire() lock.release() ``` + Itertools --------- **Every function returns a generator and can accept any collection. If you want to print an output of generator, as in examples, you need to pass it to the list() function.** @@ -806,7 +825,6 @@ from itertools import * ``` ### Combinatoric iterators - ```python >>> combinations('abc', 2) [('a', 'b'), ('a', 'c'), ('b', 'c')] @@ -822,7 +840,6 @@ from itertools import * ``` ### Infinite iterators - ```python >>> i = count(5, 2) >>> next(i), next(i), next(i) @@ -837,7 +854,6 @@ from itertools import * ``` ### Iterators - ```python >>> chain([1, 2], range(3, 5)) [1, 2, 3, 4] @@ -912,7 +928,6 @@ type(, , ) >>> z = Z() ``` - ### MetaClass #### Class that creates class: ```python @@ -980,6 +995,7 @@ def eval_(node): -5.0 ``` + Coroutine --------- * **Similar to Generator, but Generator pulls data through the pipe with iteration, while Coroutine pushes data into the pipeline with send().** @@ -1037,6 +1053,7 @@ pyplot.show() pyplot.savefig(, transparent=True) ``` + Table ----- #### Prints CSV file as ASCII table: @@ -1050,6 +1067,7 @@ with open(, newline='') as csv_file: print(tabulate(reader, headers)) ``` + Curses ------ ```python @@ -1072,6 +1090,7 @@ def get_border(screen): return P(width - 1, height - 1) ``` + Image ----- #### Creates PNG image of greyscale gradient: @@ -1091,6 +1110,7 @@ img.save('out.png') * `RGBA` - 4x8-bit pixels, true color with transparency mask * `HSV` - 3x8-bit pixels, Hue, Saturation, Value color space + Audio ----- #### Saves list of floats with values between 0 and 1 to a WAV file: @@ -1105,6 +1125,7 @@ wf.writeframes(b''.join(frames)) wf.close() ``` + UrlLib ------ ### Translate special characters @@ -1113,6 +1134,7 @@ import urllib.parse = urllib.parse.quote_plus() ``` + Web --- ```python @@ -1159,6 +1181,7 @@ def p_handler(sport): return json.dumps([home_odds, away_odds]) ``` + Profile ------- #### Basic: @@ -1199,6 +1222,7 @@ def get_datetime_string(a_datetime): return a_datetime.strftime('%Y%m%d%H%M%S') ``` + Progress Bar ------------ ### Basic: @@ -1260,6 +1284,7 @@ for i in range(STEPS): bar.finish() ``` + Basic Script Template --------------------- ```python @@ -1293,6 +1318,3 @@ def read_file(filename): if __name__ == '__main__': main() ``` - - -