Download as pdf
Download as pdf
You are on page 1of 52
Jay 2,2020 / tare Soe Comprehensive Python Cheatsheet Download text le, Buy PDF Fork me on GitHub ar Check out FAQ. Hheefeehes # Contents 1. Collections': [List, Dictionary, Set, Tuple, Range, Enumerate, Iterator, Generator], "2. Types': [Type, String, Regular Exp, Format, Numbers, Combinatorics, Datetine], 3. Syntax’ [Args, Inline, Closure, Decorator, ‘Class, Duck Type, Enum, Exception], “4. System" [Exit, Print, Input, Conmand_Line Arguments, Open, Path, OS_Commands],, "5. Data’: (ISON, Pickle, CSV,'SQLite, Bytes, Struct, Array, Memory View, Dequel, "6. Advanced’ (Threading, Operator, Introspection, Metaprograming, Eval, Coroutine], ‘7. Libraries': (ProgressBar, Plot, ‘Table, Curses, Logging, Scraping, Web, Profile, NunPy, Image, Audio, Games, Data, Cython] + # Main if _name_ == '_main_' # Runs main() if file wasn't imported. main() # List = [frominclusive : toLexclusive : sstep_size] . append () # Or: += [] .extend() —# Or: += . sort() . reverse() ’ = sorted() = reversed() sun_of_elements elementwise_sum sorted_by_sécond = sorted(, key=lanbda e sorted_by_both flatter_list product_of_eltems Ust_ofichars sun() [sum(pair) for pair in zip(list_a, list_b)] ela) sorted(, key=lanbda el: (el[1], el{0])) List (itertools. chain. from_iterable()} functools. reduce(lambda out, el: out * el, ) = Ust() + Module operator provides functions itemgetter() and mul() that offer the same functionality as lambda expressions above. = ,count() index = , index() . insert (index, ) = . pop( [index] } . renove() . clear() Returns Returns Inserts Renoves Renoves Renoves nunber of occurrences, Also works on strings. index of first occurrence or raises ValueError. iten at index and moves the rest to the right. and returns item at index or from the end. first occurrence of item or raises ValueError. all items. Also works on dictionary and set. # Dictionary .keys() # Coll. of keys that reflects changes. .values() # Coll. of values that reflects changes. litens() # Coll. of key-value tuples that reflects chgs. value = .get(key, default-None) # Returns default if key is missing. value = .setdefauit(key, default-None) # Returns and writes default if key is missing, = collect ions. defaultdict () # Creates a dict with default value of type. = collections.defaultdict(Lambda: 1) # Creates a dict with default value 1. = dict () # Creates a dict from coll. of key-value pairs. = dict (zip(keys, values)) # Creates a dict from two collections. = dict. fromkeys(keys [, value]) # Creates a dict from collection of keys. . update() # Adds items. Replaces ones with matching keys. value = .pop(key) # Renoves item or raises KeyError. {k for k, v in ,itens() if v == value) # Returns set of keys that point to the value, {ki v for k, v in ,items() if k in keys} # Returns a dictionary, filtered by keys. Counter >>> from collections import Counter >>> colors = ['blue', ‘blue’, ‘blue’, ‘red’, ‘red*] >>> counter = Counter(colors} >>> counter ‘yellow’ ] += 1 Counter({'blue': 3, 'red': 2, 'yeUlow': 1}) >>> countersmost_common() [0] blue", 3) # Set = set() sadd() # Or: |= (} .update( [= = .union() # or: | = . intersection() # Or: & = -difference() # Or: - = ,synmetric_difference() # Or: > = . issubset() # Or: <= sbool> = . issuperset () # Or: >= = .pop() # Raises KeyError if enpty. .renove() # Raises Keyérror if missing. .discard() # Doesn't raise an error. Frozen Set + Is immutable and hashable. ‘+ That means it can be used as a key in a dictionary or as an element in a set. = frozenset() # Tuple Tuple is an immutable and hashable list. = () = (, ) stuple> = (celle, [, 61) Named Tuple ‘Tuple's subclass with named elements. >>> from collections import namedtuple >>> Point = namedtuple('Point', ‘x y') >>> p = Point(1, y=2) Point(x=1, y=2) >>> pl0] 1 So> px 1 po» getattrip, ty") 2 Ss> p._fields # Or: Point._fields Cx Ty) # Range range(to_exclusive) range( from_inclusive, to_exclusive) range(fron_inclusive, to_exclusive, +step_size) from_inclusive = .start to_exclusive = .stop # Enumerate for i, el in enumerate( [, i_start]) # Iterator = iter() # “iter()* returns untodified iterator. = iter(, to_exclusive) #A sequence of return values until ‘to_exclusive’. = next( [, default) # Raises Stoplteration or returns ‘default’ on end. # Returns a list of iterator's remaining elenents. Itertools from itertools import count, repeat, cycle, chain, islice = repeat( [, times]) = cycle() Returns updated value endlessly. Accepts floats. Returns elenent endlessly or ‘tines’ tines. Repeats the sequence endlessly. aah chain(, [, ...]) # Empties collections in order. chain. from iterable() # Empties collections inside a collection in order. islice(, to_exclusive) islice(>> counter = count(1, 2) >>> next (counter), next (counter), next (counter) (10, 12, 14) # Type + Everything is an object. + Every object has a type. ‘© Type and class are synonymous. 1 ._class_ issubclass(type(), ) = type() = isinstance(, ) >>> type('a'), ‘a'._class_, str (, , ) ‘Some types do not have built-in names, so they must be imported: from types import FunctionType, MethodType, LambdaType, GeneratorType Abstract Base Classes Bach abstract base class specifies a set of virtual subclasses. These classes are then recognized by isinstance() and issubclass() as subclasses of the ABC, although they are really not. >o> from collections. abe import Sequence, Collection, Iterable >>> isinstance( (1, 2, 3], Iterable) True Sequence ] Collection | Iterable Ust, range, str ’ ’ ’ dict, set ’ ? iter ’ >>> from nunbers import Integral, Rational, Real, Complex, Number >>> isinstance(123, Number) True J antegrat | Rational ] Real | Complex ] Number int 7 7 7 7 7 fractions.Fraction ; Z : i float : ; ; conplex : 3 decimal. Decimal 2 # String = .strip() # Strips all whitespace characters from both ends. estr> = .strip(*") # Strips all passed characters from both ends. = .split() # Splits on one or more whitespace characters. = .split(sep-None, maxsplit=-1) # Splits on ‘sep! str at most 'maxsplit' times. = .splitlines(keepends=False) # Splits on \n,\r,\r\n. Keeps them if ‘keepends*. estr> = .join() # Joins elements using string as separator. = in # Checks if string contains a substring. = .startswith() # Pass tuple of strings for multiple options. = .endswith() # Pass tuple of strings for multiple options. = .find() # Returns start index of first match or -1. # = .index() Sane but raises Valuetrror if missing. = .replace(old, new [, count]) # Replaces ‘old with ‘new’ at most ‘count’ times. = stranslate() # Use “stremaketrans()° to generate table. = chr() # Converts int to Unicode char. = ord() # Converts Unicode char to int. + Also: "Istrip()', 'rstrip()' + Also: "lower()', 'upper()', 'capitalize()' and 'title()'. Property Methods (fs) | fa-zA-z) | GAT pay [0-9] isprintable() ’ ’ ’ ’ ’ isatnum() ’ y ’ ’ isnumeric() y ’ ’ isdigit() “ ‘ isdecinal() ‘ ‘Also: "isspace()' checks for *[ \t\n\r\fWwel! # Regex import re = rersub(, new, text, count=0) # Substitutes all occurrences with tnew', = re.findall(,’ text) # Returns all occurrences as strings. = re.split(, text, maxsplit=0) # Use brackets in regex to include the matches. = re,search(, text) # Searches for first occurrence of the pattern, Hatch» = resmatch( = re.finditer( = atch>.group() # Returns the whole match. Also group(@). estr> = atch>.group(1) # Returns part in the first bracket. =tuple> = Match>.groups() # Returns all bracketed parts. = atch>,start() # Returns start index of the match. = atch>send() # Returns exclusive end index of the match. Special Sequences ‘+ By default digits, alphanumeries and whitespaces from all alphabets are matched, unless *flags=re. ASCII! argument is used. + Use a capital letter for negation. ‘Nd! == 1 [0-9]" # Matches any digit. "Aw! == '[a-zA-Z0-9_]" # Matches any alphanumeric, Ns! == 'T \t\n\r\ flv! # Matches any whitespace. Format = f'{}, {}* = '(}, {}'-format(Zel_t>, ) Attributes >>> from collections import namedtuple >>> Person = namedtuple( ‘Person’, ‘name height") >>> person = Person( ‘Jean-Luc’, 187) >>> f"{person. height}! "187" >>>_'{p.height}*. format (p=person) "187" General Options {:<10} {eel>:*10} {eel>:>10} {eel>.<10} {eel>:=0) seaee Strings Ir! calls object’s repr method, instead of strQ, to get a string. {'abede! !r:10} #“abede! {‘abede! 110.3} # ‘abe . # ‘abc’ Numbers { 12345 #* 123,456" { 123451 #* 123/456" { 12345 #4123456 {2123451 # '- 123456! {123451 # * 123456" {-123456: # 123456" Floats {1.23456:10.3} a 1,23" 1,235! # * 1.235640" # 123.4568" Comparison of presentation types: {float} | (:f {efloat>re) fefloat>ss) @.000056729 | *5.6789¢-05" “9.000057 | '5.678900e-05" *0.005679%" 0-00056789 +0.00056789" +9.00568' | '5.678900e-04' 0.056789" 0.056789 +0,0056789" +0,005679' | '5.678900e-03' +0,567890%" 0.056789 10,056789" '9,056789' | '5.678900e-02' 156789008" 0156789 9156789" '91567890' | '51678900e-01' "56.789000%" 516789 15.6789! '5.678900' | '5.678900e+00' | _'567-a90000%" 56.789 *56.789" ‘s6.7a9000' | '5.678900e+01' | ‘5678.900000%" 567.89 *567.89" “567.890000' | '5.678900e+02' | *56789-000000%" {efloat:.2) ] (:.2f) | {efloats:.2e) | ( int() # Or: math. Floor() float() # Or: et = conplex(real=®, ima # OF: + j = fractions.Fraction(®, 1) # Or: Fraction(nunerator=®, denoninator=1) = decinal.Decinal() # Or: Decimal((sign, digits, exponent) ) + int ()! and 'float()! raise ValueError on malformed strings. + Decimal numbers can be represented exactly, unlike floats where '1,1 + 2.2 != 3.3. + Precision of decimal operations is set with: ‘decinal.getcontext().prec = ' Basic Functions ‘ = pow(, ) # Or: ee = abs(} # = abs() = round( [, tndigits}) # “round(126, -1) == 130° Math from math import e, pi, inf, nan, isinf, isnan from math import cos, acos, sin, asin, tan, atan, degrees, radians from math import log, logid, log2 Statistics from statistics import mean, median, variance, stdev, pvariance, pstdev Random from random import random, randint, choice, shuffle = random() = randint(from_inclusive, to_inclusive) choice() shuffle() Bin, Hex 0b 0xchex> zint> int(‘#', 2) int(‘', 16) int(‘#@b*, 0) "[-]0bebin>* = bin() + hex() Bitwise Operators = & # And | #0r zint> sint> * # Xor (@ if both bits equal) << n_bits # Shift left (> for right) = ~cint> # Not (also: — 1) # Combinatorics + Every function returns an iterator. * Ifyou want to print the iterator, you need to pass it to the list() function first! from itertools import product, combinations, combinations with_replacement, permutations >>> product({®, 1], repeat=3) U2, @ 8), (0, 0, 1), (, 1, ®), (0, 2, 1), (8%), (4, 1, (1, 1, 0), 1, 2, 1) >>> product("ab", "12") (at, 11), Cal, 124), (be 1), Cet, 129) >>> combinations(‘abc*, 2) (lat, tb), Cat, tet), (bt, tet) >>> combinations, (at, ta), Ca (ory by, Co) (Get) te] >>> permutations(‘abc", 2) (rai, bY, Cat, tet), (phy tat), Cary ed, Cet ta), Cet, "bY # Datetime ‘+ Module ‘datetime’ provides ‘date’ , classes. All are immutable and hashable. ‘+ Time and datetime objects can be ‘aware’ , meaning they have defined timezone, or ‘naive’ , meaning they don't. + Ifobject is naive, itis presumed to be in the system's timezone, ‘ and ‘timedelta' from datetine import date, time, datetine, timedeita from dateutil.tz import UTC, tzlocal, gettz, resolve_imaginary Constructors = date(year, month, day) = time(hour0, minute=0, second-0, microsecond=0, tzinfo-None, foli
= datetime(year, month, day, hour=0, minute=0, second=2, ...) = timedelta(days=é minutes: Seconds=8, microseconds=0, milliseconds=0, =, hours=0, weeks=0) + Use '<0/DT>.weekday()' to get the day of the week (Mon == 0) " means the second pass in case of time jumping back for one hour. resolve_inaginary()' fixes DTs that fall into the missing hour. Now = D/DT.today() # Current local date or naive datetine. DT. utcnow( ) # Naive datetine from current UTC tine. <0Ta> DT: now() # Aware datetime from current tz tine. + Toextract time use '.time()', '.time()' or '. timetz()'. Timezone = UTC UTC timezone. London without DST. = tzlocal() Local timezone. Also gettz(). # * = gettz( '/') # ‘Continent/City_Name* timezone or None. * #
,ast imezone() Datetime, converted to passed timezone. = <1/DT>.replace(tzinfo=) # Unconverted object with new timezone. Encode = D/T/0T.fromisoformat('') _# Object from ISO string. Raises ValueError.
= DT.strptine(, ‘') # Datetime from str, according to format. = /DT. fromordinal() # D/DTn from days since Christ, at midnight. = DT, fromtimestamp() # Local time DTn from seconds Since Epoch. = DT. fromtimestanp(, ) # Aware datetine from seconds since Epoch. ‘+ 180 strings come in following forms: 'YYYY-NM-DD', 'HH:MM:SS. ffffff[tl ', or both separated by an arbitrary character. Offset is formatted as: 'HH:NM' + Epoch on Unix systems is: '197@~@1-01 00:00 UTC', '1970~@1-01 01:00 CET’, Decode . isoformat (sep="T") # Also tinespec="auto/hours/minutes/seconds'. . strftime('') # Custom string representation. zint> . toordinal() # Days since Christ, ignoring time and tz. = , timestamp() # Seconds since Epoch, from DIn in local tzs = . timestamp() # Seconds since Epoch, from DTa. Format >>> from datetime import dat ime >>> dt = datetine, strptine( ‘2015-05-14 23:39:08.0 +0200", ‘AY-Am-Ad AH:AM:NSSf 42") >>> dtstrftine("%A, Adth of 8B ‘ay, ST:aRp AZ") “Thursday, 14th of May ‘25, 11:39PM’ UTC+02:00" + When parsing, "52" also accepts '+HH:MM! + For abbreviated weekday and month use "a! and "®b'. Arithmetics <0/0T> = = # Returned datetime can fall into missing hour. =10> = # Returns the difference, ignaring tine jumps. # Ignores tine jumps it they share tzinfo object. = # Convert OTs to UTC to get the actual delta. # Arguments Inside Function Call () () (, ) Inside Function Definition def f(): # def fx, y) def f() # def f(xt0, y=0) def f(cnondefault_args>, ): # def f(x, # Splat Operator Inside Function Call Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments. args = (1, 2) kwargs = {'x': 3, ty! func(sargs, *#kwargs) Is the same as: func(1, 2, x Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary. def add(xa): return sum(a) pes add(1, 2, 3) 6 Legal argument combinations: def f(x, y, ® | fC, ya2, 253) | FG, 2, 253) | FL, 2, 3) det f(%, x, ‘ def f(x, *, z | fa, 253) def f(x,y, * ira, 2=3) | f(1, 2, 2°3) def f(xaras) def f(x, *args) def f(+args, z) def f(x, *args, z): def f(vkwargs) def f(x, *#kwargs) : def f(x, x, sekwargs): def f(xargs, #*kwargs) | #0, 2, 293) | #0, 2, 3) def f(x, xargs, ++kwargs) 1G, 2, 223) | #4 2, 3) def f(xargs, y, ‘#kvargs) def f(x, *args, z, tekwargs) 1 f(1, 2, 23) Other Uses = [* [, eset> = {recollection> [, Stuple> = (secollection>, [ = {xecdict> [, viel} head, +body, tail = # Inline Lambda = Lambda: = lanbda , : Comprehensions = [+1 for i in range(10)] = (i for i in range(1@) if i > 5} = (i#5 for i in range(10)) = (i: ix for i in range(10)} out = [itj for i in range(10) for j in range(10)] Is the same as: out = () for i in range(10): for j in range(10) out append(itj) Map, Filter, Reduce from functools import reduce = map(lambda x: x + 1, range(1@)) #1, 2, » 10) filter(Lanbda x: x > 5, range(10)) # (6, 7, 8, 9) = reduce(lambda out, x: out +x, range(10)) # 45 Any, ALL = any() # False if empty. = alt(el [1] for el in ) # True if empty. If- Else = if else >>> [a if a else ‘zero’ for a in (0, 1, 2, 3)] (zero", 1, 2, 3] Namedtuple, Enum, Dataclass from collections import namedtupte Point = nanedtuple('Point', 'x y') point Point(@, 0) from enum import Enum Direction = Enum( ‘Direction’, 'n e sw’) direction = Direction.n from dataclasses import make_dataclass Creature = make_dataclass( ‘Creature’, [‘location’, ‘direction"]) Creature = Creature(Point(®, 8), Direction.n) # Closure We have a closure in Python when: + Anested function references a value of its enclosing function and then * the enclosing function returns the nested function. def get_multiplier(a): def out(b): return a * b return out pom multiply by 3 = get_multiplier(3) po> multiply_by_3(10) 30 ‘+ If multiple nested functions within enclosing function reference the same value, that value gets shared. + To dynamically access function's first free variable use ‘.__closure__[0].cell_contents'. Partial from functools import partiat = partial( [, , , ...]) >>> import operator as op S5> multiply by_3 = partial(op.mut, 3) >>> multiply_by_3(10) 30 + Partial is also useful in cases when function needs to be passed as an argument, because it enables us to set its arguments beforehand. + A few examples being: 'defaultdict() ', 'iter(, ‘to_exclusive) ' and dataclass's ' Field (default_factory=) ' Non-Local If variable is being assigned to anywhere in the scope, itis regarded as a local variable, unless it is declared as a ‘global’ or a ‘nonlocal’. def get_counter(): i=e def out(): nonlocal i iis return i return out >>> counter = get_counter() >>> counter(), counter(), counter() (1, 2, 3) Decorator ‘A decorator takes a function, adds some functionality and returns it. @decorator_name def function_that_gets_passed_to_decorator(): Debugger Example Decorator that prints function’s name every time it gets called. from functools import wraps def debug( func) @eraps( func) def out(+args, *+kwargs) print (func.__name_) return func(#args, **kwargs) return out debug def add(x, y): return x + y ‘+ Wraps is a helper decorator that copies the metadata of the passed function (func) to the function it is wrapping (out). * Without it 'add._name__' would return ‘out'. LRU Cache Decorator that caches function's return values. All function's arguments must be hashable. from functools import Lru_cache @lru_cache(maxs ize-None) def Fib(n): return n if n <2 else fib(n-2) + fib(n-1) + CPython interpreter limits recursion depth to 1000 by default. To increase it use sys. setrecursionLimit() '. Parametrized Decorator A decorator that accepts arguments and returns a normal decorator that accepts a function. from functools import wraps def debug(print_result-False) def decorator( func): @vraps( func) def out(+args, **kwargs): result = func(*args, **kwargs) print(func._name_, result if print_result else * return resutt return out return decorator @debug(print_result=True) def add(x, y): return x + y # Class class def _init_(sett, a) Belf.a =a def __repr_(setf) Tlass_name = self._class, a return f'{class_nane}({self.a!r})" def _str_(setf) Feturn str(self.a) @classmethod def get_class_nane(cls) : return cls._nane_ + Return value of repr0) should be unambiguous and of str() readable. + Ifonly repr( is defined, it will also be used for str0. Str) use cases: print() print(f'{}') raise Exception() Loguru. Logger. debug() csv.writer () .writerow( []) Repr( use cases: print ([]) print(f'{!r}") S>> Loguru. logger.exception() Z = dataclasses.make_dataclass(*Z', ['a']); print(Z()) Constructor Overloading class def _init_(self, a-None): Self.a =a Inheritance class Person: def _init_(self, name, age): Self.name = name self.age = age class Employee(Person) def _init (self, name, age, staft_num): Super(].__init__(name, age) self. staf_num = staff_num Multiple Inheritance class A: pass class B: pass class C(A, 8): pass ‘MRO determines the order in which parent classes are traversed when searching for a method: >>> C.mro() [, , , ) Property Pythonic way of implementing getters and setters. class MyClass ‘@oroperty def a(self) return self, @a,setter def a(self, value) self. = value >o> el = MyClass() >>> ela = 123 >>> ela 123 Dataclass Decorator that automatically generates init, repr() and eq() special methods. from dataclasses import dataclass, field Qdataclass(order=False, froze’ class ‘zattr_nane_1> attronane_2> eattronane_3> alse) stype> = List/dict/set = field(default_factory=list/dict/set) ‘+ Objects can be made sortable with ‘order=True! and/or immutable and hashable with “frozen=True! + Function field9 is needed because ': List shared among all instances. + Default factory can be any callable. [1 would make a list that is Intine: from dataclasses import make_datactass = make_dataclass(*', ) = make_dataclass(*', ) = (‘', [, ]) Slots ‘Mechanism that restricts objects to attributes listed in ‘slots’ and significantly reduces thei ‘memory footprint. class NyClasswithSlots: —stots__ = ['a'] def _init_(set) Self.a Copy from copy import copy, deepcopy = copy() = deepcopy () # Duck Types ‘A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type. Comparable + Ifeq( method is not overridden, it returns 'id(seLf) == id(other) ', which is the sameas ‘self is other'. ‘+ That means all objects compare not equal by default. + Only the left side object has eq0 method called, unless it returns Notimplemented, in ‘which case the right object is consulted. class MyComparable: def _ init (sett, a) self.a def _eq_(self, other): FF isinstance (other, return self.a return NotImplenented type(self)): other.a Hashable * Hashable object needs both hash and eqQ methods and its hash value should never change. + Hashable objects that compare equal must have the same hash value, meaning default hhasho that returns ' id(seLf) ' will not do. ‘+ That is why Python automatically makes classes unhashable if you only implement eq0. class NyHashable: def _init_(self, a) Belf.a=a property def a(setf) return self._a def _eq_(self, other) FF isinstance(other, type(self)): return self.a == other.a return NotInplenented def _hash_(self) Teturn hash(self.a) Sortable ‘+ With total ordering decorator, you only need to provide eqQ and one of ItQ, gt0, le0 or e0 special methods. from functools import total_ordering @totat_ordering class MySortable: def _init_(self, a) Helf.a= a def _eq_(self, other) TF isinstance(other, type(sett)): return self.a == other.a return NotInplenented def _lt_(self, other): TF isinstance(other, type(self)): return self.a < other.a return NotInplenented Iterator + Any object that has methods next() and iter( is an iterator. ‘+ Next() should return next item or raise Stoplteration. + Iter( should return ‘self. class Counter def _init_(self) Belfi = 0 def _next_(setf) Selfive 1 return self.i def _iter_(seif) Teturn self >>> counter = Counter() >>> next (counter), next (counter), next (counter) (1, 2, 3) Python has many diferent iterator objects: ‘+ Iterators returned by the iterQ function, such as list iterator and set iterator. + Objects returned by the itertools module, such as count, repeat and cycle. + Generators returned by the generator functions and generator expressions. * File objects returned by the open() function, etc. Callable + All functions and classes ha a call() method, hence are callable. + When this cheatsheet uses '' as an argument, it actually means *' class Counter: def _init_(setf) Belf.i= 0 def _call_(setf) Selfei = return self. i >>> counter = Counter() >>> counter(), counter(), counter() (1, 2, 3) Context Manager ‘+ Enter() should lock the resources and optionally return an object. + Exit should release the resources. * Any exception that happens insi * fit wishes to suppress the except the with block is passed to the exit) method. it must return a true value. class MyOpen: def _init_(self, filename): Self. filenane’= filenane def _enter_(sett): Belt. file = open(selt. filename) return self. file def _exit_(self, exc_type, exception, traceback) Self. file. close() >>> with open('test.txt', 'w') as file: see Tile.write( Hello World!) 25 with Myopen( ‘test.txt") as file w.. print(file.read()) Helle World! # Iterable Duck Types Iterable + Only required method is iter(. It should return an iterator of object’ * Contains( automatically works on any object that has iter( defined. class NyIterable: def _init_(self, a) Self. def _iter_(setf) Feturn iter(setf.a) def _contains_(self, eU): Teturn el in self.a >>> obj = MyIterable({1, 2, 3]) >>> [el for el in obj] (1, 2, 3) 355 1 in obj True Collection + Only required methods are iter() and len0. + This cheatsheet actually means '' when it uses "' + Those not to use the name ‘iterable’ because it sounds scarier and more vague than. ‘collection’ class NyCollection: def _init_(setf, a) Self.a=a def _iter_(setf) ‘turn iter(self.a) def _contains_(self, et): Feturn el in self.a def _len_(setf): Teturn len(self.a) Sequence + Only required methods are len0 and getitemQ. + Getitemo should return an item at index or raise IndexEsror. * Iter( and contains() automatically work on any object that has getitem( defined. + Reversed() automatically works on any object that has getitem( and len() defined. class MySequence: def _init_(self, a) Self.a =a def _iter_(self) Feturn iter(selt.a) def _contains_(self, el): Feturn el in self.a def _len_(self): Feturn len(self.a) def _getitem_(selt, i) Feturn setf- ali] def _reversed_(setf): Teturn reversed(self.a) ‘ABC Sequence + Itsa richer interface than the basic sequence. + Extending it generates iter(), contains(, reversed@, index() and count. + Unlike 'abc. Iterable! and 'abc.Collectiion', itis not a duck type. That is why ssubclass(MySequence, abc. Sequence) ' would return False even if MySequence had all the methods defined. from collections import abc class MyAbcSequence(abc. Sequence) : def _init_(self, a) Selfea def _len_(setf): Teturn len(self.a) def _getitem_(self, i) Feturn self. ali] ‘Table of required and automatically available special methods: Tterable | Collection | Sequence | abc.Sequence iter() ! ! ’ contains) ’ ’ ’ Ten() ! : getiten() ! reversed() ‘ index() count () + Other ABCs that generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping. ‘+ Names of their required methods are stored in ', _abstractmethods_'. # Enum from enum import Enum, auto class (enum) : “enenbernane_1> = - = , ‘nember"nane_3> = auto() * Ifthere are no numeric values before auto(, it returns 1. + Otherwise it returns an increment of the last numeric value. = . # Returns a menber. = [*' ] # Returns a menber or raises KeyError = () # Returns a member or raises ValueError. -enenber>. name # Returns member's nane. enenber>. value # Returns member's value. Listof_menbers = Uist() mnenber_nanes nenber_values randon_nenber {a-nane for a in ] [a.value for a in ] random. choice( List() ) def get_next_nenber(nenber): members = list(member._class_) index = (members. index(menber) + 1) % len(members) return menbers [index] Intine Cutlery = Enum(*Cutlery', ‘fork knife spoon’) Cutlery = Enum(*Cutlery", [‘fork', ‘knife’, "spoon*]) Cutlery = Enum(*Cutlery", {'fork "knife': 2, spoon": 3}) User-defined functions cannot be values, so they must be wrapped: from functools import partial LogicOp = Enum(*Logicop", {'AND* OR" Land r), Vor r)}) partial(Lanbda 1, 1 partial(Tanbda 1 ‘+ Another solution in this particular case is to use built-in functions and_Q/ and or_( from the module operator. # Exceptions Basic Example try: ‘ except Complex Example try: except : ‘ except : ‘ else: ‘ finally: ‘ ‘+ Code inside the ‘else! block will only be executed if 'try' block had no exception. + Code inside the "finally" block will always be executed. Catching Exceptions except as except (, ...) except (, 11.) as * Also catches subclasses of the exception. + Use "traceback.print_exc()' to print the error message to stderr. R ing Exceptions raise raise () raise ( [, « Reraising caught exception except as raise Exception Object argunents = .args exc_type = , _class__ filename = .—traceback_.tb_frame,f_code,co_filename func_name = . traceback__. t_frame. f_code. co_name Line Linecache.get Line(Filenae, ._traceback_.tb_Lineno) error_msg = traceback. format_exception(exc_type, , .__traceback__ Built-in Exceptions BaseException |— systenéxit # Raised by the sys.exit() function. |— keyboardinterrupt # Raised when the user hits the interrupt key (ctrl-c). L exception 4# User-defined exceptions should be derived from this class. |— arithneticerror # Base class for arithnetic errors. ZeroDivisionError # Raised when dividing by zero. | attributeError # Raised when an attribute is missing, L- eorerror # Raised by input() when it hits end-of-file condition. FE Lookuperror 4# Raised when a look-up on a collection fails. — IndexError # Raised when a sequence index is out of range. KeyError # Raised when a dictionary key or set element is not found. |_ Naneerror # Raised when a variable name is not found. [- ostrror # Failures such as “file not found” or “disk full". \ FileNotfoundError # When a file or directory is requested but doesn't exist. |- runtinezrror # Raised by errors that don't fall in other categories. \— RecursionError # Raised when the maximum recursion depth is exceeded. |_ stoptteration # Raised by next() when run on an enpty iterator. t— Typerror # Raised when an argument is of wrong type. CE Vatuetrror # When an argunent is of right type but inappropriate value. \—‘unicodetrror 4# Raised when encoding/decoding strings to/from bytes fails. Collections and their exceptions list dict set getiten() | Indexerror | keyerror pop() Indexérror | Key€rror | KeyError remove() | ValueError KeyError index() | Valueérror Useful built-in exceptions: raise TypeError( ‘Argument is of wrong type!") raise ValueError(‘Argunent is of right type but inappropriate value!') raise RuntimeError('None of above!') User-defined Exceptions class MyError(Exception): pass class NyInputError(Myérror) : pass # Exit Exits the interpreter by raising SystemExit exception. import. sys sys. exit() # Exits with exit code 0 (success). sys-exit() # Prints to stderr and exits with 1. sys. exit () # Exits with passed exit code. # Print print(, ..., sep=' *, end='\n', file=sys.stdout, flush=False) + Use 'file=sys.stderr' for messages about errors. + Use 'flush=True! to forcibly flush the stream. Pretty Print from pprint import pprint pprint(, width=80, depth-None, conpact=False, sort_dicts=True) + Levels deeper than ‘depth’ get replaced by # Input Reads a line from user input or pipe if present. = input (prompt=None) + Trailing newline gets stripped ‘+ Prompt string is printed to the standard output before reading input. ‘+ Raises EOFError when user hits EOF (ctrl-d/2) or input stream gets exhausted. # Command Line Arguments import sys Script_name = sys.argv[0] arguments = sys.argv[1:] Argument Parser from argparse import ArgumentParser, FileType P = ArgunentParser(description=) pradd_argument(*—', '-~cname>', action='store_true') # Flag p.add_argunent(*—", '~~cname>', type=ctype>) Option pladd_argunent(‘", type=ctype>, narg} p.add_argument(*", type-, nargs: p.add_argument(‘", type=, narg} args = p.parse_args() value = args. ) +) #1) First argument Remaining arguments Optional arguments Exits on errors + Use ‘help=' to set argument description. + Use tdefault=cel>' to set the default value. + Use 'type=FileType()' for files. # Open Opens the file and returns a corresponding file object. = open('", modi + encoding-None, newLine-None) + ‘encoding=None! means that the default encoding is used, which is platform dependent. Best practice is to use "encoding="utf-8"' whenever possible. + ‘newline=None! means all different end of line combinations are converted to ‘\n' on read, while on write all \n' characters are converted to system's default line separator. + ‘newLine="""' means no conversions take place, but input is stil broken into chunks by readline() and readlines() on either ‘\’, \F of \e\n’. Modes # tr! - Read (default). + hw! - Write (truncate). + 1x! - Write or fal if the file already exists. + ta! Append. + ‘we! - Read and write (truncate). + 'r#! - Read and write from the start. + "ab! - Read and write from the end. + 't!- Text mode (default). + tb! - Binary mode. Exceptions + 'FileNotFoundérror' can be raised when reading with 'r' or 'r#!. + 'FilefxistsError’ can be raised when writing with "x! + 'IsADirectoryError! and 'PermissionError' can be raised by any. + "OSError is the parent class of al listed exceptions. File Object . seek(@) # Moves to the start of the file. : seek(offset) # Moves ‘offset! chars/bytes from the start. seek(8, 2) # Moves to the end of the file. .seek(toffset, ) # Anchor: @ start, 1 current position, 2 end. = .read(size=-1) # Reads 'size' chars/bytes or until EOF. = .readline() # Returns a Line or enpty string/bytes on EOF. creadlines() # Returns a List of remaining Lines. = next() # Returns a Line using buffer. Do not mix. . write() # Writes a string or bytes object. .writelines() # Writes a coll. of strings or bytes objects. ! flush() # Flushes write buffer. ‘+ Methods do not add or strip trailing newlines, even writelinesQ. Read Text from File def read_file(filenane): with open( filename, encoding='utf-8") as file: return file, readlines() Write Text to File def write to_file(fitenane, text): with open( filename, 'w!, encoding='utf-8') as file: file.write(text) # Path from os import getcwd, path, listair from glob import glob = getewd() # Returns the current working directory. = path. join(, # Joins two or more pathname components. = path-abspath( = path, basenane() = path. dirname() = path. spLitext () = Listdir(path=". = glob(‘") = path.exists() = path. isfile() = path. isdir() DirEntry Returns final component of the paths Returns path without the final component. Splits on last period of the final component. sas # Returns filenames located at path. # Returns paths matching the wildcard pattern, -. exists() .is_file() .is_dir() Using scandir( instead of listdirQ can significantly increase the performance of code that also needs file type information. from os import scandir = scandir(path=". = ,path = .nane = open() Path Object from pathlib import Path = Path( [, -«-]) = / (/ « = Path() = Path. cwd() = .resolve() = .parent estr> = inane = isten estr> = suffix = .parts .iterdir() -glob( "") = str() = open() # OS Commands Files and Directories Returns DirEntry objects located at path. Returns path as a string, Returns final component as a string. Opens the file and returns file object. saan # Accepts strings, Paths and DirEntry objects. # One of the paths must be a Path object. # Returns relative cwd. Also Path('."). # Returns absolute cwd. Also Path()-resolve(). # Returns absolute Path without symlinks. Returns Path without final component. Returns final component as a string. Returns final component without extension. Returns final component's extension. Returns alt components as strings. seaae Returns dir contents as Path objects. Returns Paths matching the wildcard pattern. ae Returns path as a string. Opens the file and returns file object. as + Paths can be either strings, Paths or DirEntry objects. + Functions report 0S related errors by raising either OSError or one ofits subclasses. import os, shutit os .chdir() os.mkdir(, mod: shutil. copy(from, to) shutit. copytree(from, to) # Changes the current working directory. # Creates 2 directory. Mode is in octal. # Copies the file. ‘to’ can exist or be a dir. # Copies the directory, *tot must not exist. os.rename(from, to) # Renames/moves the file or directory, ossreplace(from, to) # Same, but overwrites 'to' if it exists. os. renove() # Deletes the file. os.rmdir() # Deletes the enpty directory. shutit, rmtree() # Deletes the directory, Shell Commands import os = 0s.popen( ‘'). read() Sends 1 + 1 to the basic calculator and captures its output: >>> from subprocess import run >>> run('be', input='1 + 1\n', capture_output ConpletedProcess(args="bc', returncode=0, stdou rue, encoding='utf-8") 2\n', stderr="") Sends test.n to the basic calculator running in standard moc saves its output to test.out: >>> from shlex import split >>> 05.popen( echo 1+ 1 > test.in') >>> run(split( "be -s'), stdin=open( ‘test. in’), s pen(‘test.out', ‘w')) ConpletedProcess(args=['bc', '-s'], returncode=0) >>> open('test.out"). read() "2\n" # JSON ‘Text file format for storing collections of strings and numbers. import json estr> json.dumps(, ensure_asci: json. loads() rue, indent=None) Read Object from JSON File def read_json_file( filename) with open(filenane, encoding='utf-8") as file: return json, load( file) Write Object to JSON File def write_to_json_file(filename, an_object) with open(filename, ‘w', encoding'utf-8") as file: json.dunp(an_object, file, ensure_ascii=False, indent=2) # Pickle Binary file format for storing objects. import pickle =bytes> = pickle.dumps () = pickle, loads () Read Object from File def read_pickle_file(titename) : with open(fitename, ‘rb') as file: return pickle. toad(fite) Write Object to File def write_to_pickle_file(filenane, an_object): with open(filename, ‘wb') as file: pickle.dump(an_object, file) # csv Text file format for storing spreadsheets. import csv Read = csv. reader() # Also: “dialect="excel', delimiter=","°. = next () # Returns next row as a list of strings. = list() # Returns List of remaining rows. + File must be opened with 'newLine=" fields will not be interpreted correctly! argument, or newlines embedded inside quoted Write = csvewriter() # Also: “dialect=texcel', delimiter=","°y swriter>.writerow() _ # Encodes objects using ‘str()". swriter>.writerows() # Appends multiple rows. + File must be opened with ‘newLine=" ‘\n’ on platforms that use “\\n' line ending argument, or \r' will be added in front of every Parameters + ‘dialect! - Master parameter that sets the default values. ‘delimiter - A one-character string used to separate fields. + ‘quotechar' - Character for quoting fields that contain special characters. + ‘doublequote' - Whether quotechars inside fields get doubled or escaped. + 'skipinitialspace' - Whether whitespace after delimiter gets stripped. + 'Lineterminator' - Specifies how writer terminates rows. + ‘quoting’ - Controls the amount of quoting: 0 «as necessary, 1 - all. * ‘escapechar' - Character for escaping ‘quotechar' if doublequote’ is False. Dialects excel excel-tab unix: delimiter Nt quotechar doublequote True True skipinitialspace False False Lineterminator "\r\n" "\r\nt quoting ° ° escapechar None None Read Rows from CSV File def read_csv_file(fitenane): with open(filename, encoding="utf-8", newline= return lst(csv. reader( file) ) ) as file: Write Rows to CSV File def write_to_csy_file(filename, rows) with open(filename, ‘w', encoding="utf-8', newline='*) as file: writer = csv.writer( file) writer.writerows (rows) # SQLite Server-less database engine that stores each database into a separate file. Connect Opens a connection to the database file. Creates a new file if path doesn't exist. import sqlite} = sqlite3.connect(*") # Also ‘rmemory:'. ,close() Read Returned values can be of type str, int, float, bytes or None. = ,execute( *') # Can raise a subclass of sqlite3.érror. = . fetchone() # Returns next row. Also next() . = :fetchall() # Returns remaining rows. Also list(). Write .execute( ‘") , commit () or: with . execute( “") Placeholders ‘+ Passed values can be of type st, int, float, bytes, None, bool, datetime.date or datetime.datetme, + Bools will be stored and returned as ints and dates as ISO formatted strings. -execute('', ) # Replaces '?'s in query with values. sexecute('', ) _# Replaces ':"s with values, sexecutemany(*', ) # Runs execute() many times, Example In this example values are not actually saved because 'con.conmit()' is omitted! >>> con = sglite3. connect (‘test-db') >>> con-execute( ‘create table person (person_id integer primary key, name, height) ") >>> consexecute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid 1 >>> con-execute('select * from person). fetchall() [, *Jéan-Luc*, 187)] MySQL Has a very similar interface, with differences listed below. # $ pip3 install _mysql-connector from mysql import connector = connector.connect(host=, ~) # cuser=, password=, database-"s = .cursor() # only cursor has execute method. .execute( '") # Can raise a subclass of connector.Error. .execute( '", ) # Replaces '%s's in query with values. ,execute( '", ) # Replaces '%()s's with values. # Bytes Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray. = b'' = [] = [] cbytes> = . join() Only accepts ASCII characters and \x0o - \xff. Returns int in range from @ to 255. Returns bytes even if it has only one elenent. Joins elements using bytes object as separator. eaee Encode = bytes () = bytes(, "Utf-8") Ints must be in range from @ to 255. or: .encode( ‘utf-8") eae = .to_bytes(n_bytes, ~) “pyteorder='big/little’, signed=False’ . = bytes. fronhex(*') Hex numbers can be separated by spaces. Decode = List() Returns ints in range from 0 to 255. = str(, ‘utf-8") Or: .decode( ‘utf-8") = int.from_pytes(, ..) "chex>' = -hex() byteorder='big/Little', signed-False’ Returns a string of hexadecimal numbers. as Read Bytes from def read_bytes( filename): with open( filename, ‘rb') as file: return file. read() Write Bytes to File def write_bytes(fitenane, bytes_obj): with open(fitename, ‘wb') as file file,write(bytes_obj) # Struct + Module that performs conversions between a sequence of numbers and a bytes object. + Machine's native type sizes and byte order are used by default. from struct import pack, unpack, iter_unpack = pack(" [) ) ..+]) stuple> = unpack("', ) = iter_unpack(‘', ) Example >>> pack('>hhl', 1, 2, 3) b'\x00\x01\ x00\x02\ x00\x00\x00\x03" po> unpack(*>hhU', b'\x0\x01\x00\x02\x00\x00\x00\x03") (1, 2, 3) Format For standard type sizes start format string with: + '=!- native byte order # te! -fittle-endian + ">! big-endian (also "1 Integer types. Use a capital Letter for unsigned type. Standard sizes are in brackets: + x! = pad byte + 'b! char (1) + th! short @ +i int @ + 1 tong 4 + 'q" long long ®) Floating point types: + 'F! float 4) = 'd" double (8) # Array List that can only hold numbers of a predefined type. Avail are listed above. from array import array = array('", ) # Array from collection of numbers. = array('', ) # Array from oytes object. = array('", ) # Treats array as a sequence of nunbers. = bytes () W Or: .tobytes() # Memory View ‘+ A sequence object that points to the memory of another object. ‘+ Each element can reference a single or multiple consecutive bytes, depending on format. * Order and number of elements can be changed with slicing. = menoryview() # Inmutable if bytes, else mutable. = [] # Returns an int or a float. enview> = [] # Mview with rearranged elements. = .cast("' ) # Casts menoryview to the new format. enview>. release() # Releases the object's memory buffer. Decode .write() = bytes ( = . join() sarray> = array('', ) = Ust( = ste( = int. from_bytes(.Fex() # Deque Writes mview to the binary file. Creates a new bytes object. Returns list of ints or floats. Treats mview as a bytes object. Treats mview as a bytes’ object. A thread-safe list with efficient appends and pops from either side. Pronounced “deck”. from collections import deque . appendlett() . extendleft() = .popleft() , rotate(n=1) # Threading deque(, maxten-None) # Opposite elenent is dropped if full. # Collection gets reversed. # Raises IndexError if empty. # Rotates elements to the right. + CPython interpreter can only run a single thread at a time. + That is why using multiple threads won't result in a faster execution, unless at least one of the threads contains an 1/0 operation. from threading import Thread, Rock, Semaphore, Event, Barrier Thread = Thread(target=.start() = .is_alive() .join() # Use “args=” to set arguments, # Starts the thread. # Checks if thread has finished executing. # Waits for thread to finish. + Use *kwargs=' to pass keyword arguments to the function, = RLock() ,acquire() , retease() Lock = RLack() with Lock: ‘Semaphore, Event, Barrier = Senaphore(value=1) = Event() = Barrier(n_times) rue! or the program will not be able to exit while the thread alive # Waits for lock to be available, # Makes the lock available again. # Lock that can be acquired "value" tines. # Method wait() blocks until set() is called. # Method wait() blocks until it's called ‘i Joins mviews using bytes object as sep. Treats view as a sequence of numbers. “byteorder="big/Little’, signed=False’. \_times'. Thread Pool Executor from concurrent.futures import ThreadPoolExecutor with ThreadPooléxecutor(max_workers-None) as executor: = executor.map(lambda x: x + 1, range(3)) # Does not exit until done, * executor.map(lambda x, y: x+y, ‘abc', '123') # (ta1', tb2!', 'c3!) a (1, 2, 3) = executor.submit( [, ,'..+1) Also Visible outside block. Future: = .done() # Checks if thread has finished executing. = .result() # Waits for thread to finish and returns result. Queue A thread-safe FIFO queue. For LIFO queue use LifoQueue. from queue import Queue = Queue(maxsiz . put() # Blocks until queue stops being full. , put_nowait() # Raises queue.Full exception if full. = .get() # Blocks until queue stops being empty. get_nowait () # Raises queue.Enpty exception if empty. # Operator Module of functions that provide the functionality of operators. from operator import add, sud, mul, truediv, floordiv, mod, pow, neg, abs fron operator inport eq, ne, lt, le, at, ge from operator import and_, or_,'not_ from operator import itengetter, attrgetter, methodcaller import operator as op elenentwise_sun = map(op.add, List_a, Uist_b) sorted by sécond = sorted(, key=op.itengetter(1)) sorted by both = sorted(, key=op.itengetter(1, 0)) product_of_elens = functools.reduce(op-mul, ) Logicop fenumn.Enun("Logicop*, {"AND': op.and_, ‘OR* : op.or_}) Tast_el op.methodcalter( pop!) () # Introspection Inspecting code at runtime. Variables = dir() # Names of local variables (incl, functions). = vars() # Dict of local variables. Also locals(). = globals() # Dict of global variables. Attributes = dir() # Names of object's attributes (incl. methods). = vars() # Dict of object's fields. Also ._dict_. = hasattr(, ‘') # Checks if getattr() raises an error, value = getattr(, ‘') # Raises AttributeError if attribute is missing. setattr(, ‘', value) # Only works on objects with _dict_ attribute. delattr(, ‘') # Equivalent to “del .” Parameters from inspect import signature «sige signature () no_of_params = len(.paraneters) paran-nanes = list(.paraneters.keys()) paran_kinds = [a.kind for a in .paraneters.values()] # Metaprograming Code that generates code. Type ‘Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class. = type(‘', , ) poe Z = type('Z", (), {'at: ‘abcde’, >> z= 20) 12345}) Meta Class A class that creates classes. def my_meta_class(nane, parents, attrs): attrs('a"] = ‘abcde’ return type(name, parents, attrs) class MyMetaClass( type): def _new_(cls, name, parents, attrs): ‘aEtrs[a"] = ‘abcde! return type._new_(cls, name, parents, attrs) + New( is a class method that gets called before init(. If t returns an instance ofits class, then that instance gets passed to initQ as a'self’ argument. ‘+ Itreceives the same arguments as init, except for the first one that specifies the desired type of the returned instance (MyMetaClass in our case). + Like in our case, new() can also be called directly, usually from a new() method of a child class (def __new_(cls): return super().—new_(cls)). ‘+ The only difference between the examples above is that my meta_class() returns a class of type type, while MyMetaClass() returns a class of type MyMetaClass. Metaclass Attribute Right before a class is created it checks if it has the ‘metaclass’ attribute defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type(. Class NyClass(metaclass=MyMetaClass) : b = 12345 >>> MyClass.a, MyClass.b (abcde', 12345) Type Diagram type(MyClass) MyMetaclass —# MyClass is an instance of MyNetaClass. type(MyMetaClass) == type 4H MyMetaClass is an instance of types Classes | Metaclasses MyClass —: MyMetaClass object ——+ type po str ———J n Inheritance Diagram hyCtass._bose_ object —«# HyClass is a subclass of object. Myvetacings.base_ == type # WyMetaClass is 2 subclass of type. Classes | Netaclasses vyctass | Myfetactass abject —— type str # Eval >>> from ast import Literat_eval >>> Literal_eval('1 +2") 3 boo Literaleval('[1, 2, 3]') (1, 2, 3) >>> Literal_eval(‘abs(1)") ValueError: malformed node or string # Coroutines + Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don't use as much memory. + Coroutine definition starts with ' async' and its call with ‘await. + ‘asyncio. run() ' is the main entry point for asynchronous programs. + Functions wait(, gather) and as_completed() can be used when multiple coroutines need to be started at the same time. + Asyncio module also provides its own Queue, Event, Lock and Semaphore classes. Runs a terminal game where you control an asterisk that must avoid numbers: import asyncio, collections, curses, enun, random P D collections.namedtuple('P', 'x y') # Position enum.Enum('D', 'n es w') # Direction def main(screen) curses. curs set (0) screen,nadelay (True) asyncio. run(main_coroutine( screen) ) Makes cursor invisible, Makes getch() non-blocking. Starts running asyncio code. async def main_coroutine(screen): state = ('%': P(®, 0), «id moves = asyncio.Queue() coros = (+(randon_controller(id_, moves) for id_ in range(16)), human_controller(screen, moves) , model(moves, state, *screen.getmaxyx()), view(state, 'screen}) await asyncio,wait(coros, return_when=asyncio. FIRST_COMPLETED) P(30, 10) for id_ in range(10)}} async def random_controUler(id_, moves): while True: moves. put_nowait((id_, randon.choice(1ist(D)))) await asyncio.sleep(random.random() / 2) async def human_controUler(screen, moves) while True: ch = screen.getch() key_mappings = {259: D.n, 261: D.e, 258: Ds, 260: D.w) Af ch in key_mappings moves. put_nowait(('*', key_mappings(ch])} await asyncio-sleep(@.01) async def model(noves, state, height, width) while state('x"] not in {p for id_, p in state.items() if id *) id_, d = await moves.get() P tate Lid] deltas = {D.n: P(8, -1), D.et P(1, 0), Dist P(Q, 1), D.w: P(-1, @)} new.p = P(*{sun(a) fora in 2ip(p, deltas {d])1} Af 0 <= new_p.x < width-1 and @ <= newp.y < height: state[id_] = newp async def view(state, screen) while True screen.clear() for id_, p in state.items() screen,addstr(pay, psx, str(id_)) await asyncio.sleep(@.01) curses. wrapper (main) Libraries # Progress Bar # $ pip3 install tadm from tqdm import tadn from time import steep for et in tadn((1, 2, 31) steep(0.2) # Plot # $ pip3 install matplotlib from natplot lib import pyptot pyplot.ptot( [, label=}) pyplot. plot(, ) pyplot. legend(] # Adds a legend. pyplot. savefig( "') # Saves the figure. pyplot. show() # Displays the figure. pyplot.clt() # Clears the figure. # Table Prints a CSV file as an ASCII table: # $ pip3 install tabulate import csv, tabulate with open('test.csv', encoding="utf-8", newLine=' rows = csv, reader(file) header = [a.title() for a in next(rows)] table = tabulate. tabulate(rows, header) print (table) ) as file: # Curses Clears the terminal, prints a message and waits for the ESC hey press: from curses import wrapper, curs_set, ascii from curses import KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_LEFT def main(): wrapper(draw) def draw(screen) curs_set(0) # Makes cursor invisible. screen.nodelay (True) # Makes getch() non-blocking. screen.clear() screen.addstr(@, 0, ‘Press ESC to quit.') # Coordinates are y, x. while screen.getch() != ascii.€SC: pass def get_border(screen) from collections import nanedtuple P = namedtuple('P*, *x y') height, width = screen.getmaxyx() return’ P(width-1, height-1) if _name_ main() # Logging # $ pip3 install loguru from toguru import logger Logger. add(*debug_{tine}.1og', coloriz, Logger: edd(*error_{time}.log', level logger. (*A Logging message.) True) # Connects a log file. ERROR') # Another file for errors or higher. + Levels: "debug! 'info', 'success', ‘warning’, 'error', "critical Exceptions Exception description, stack trace and values of variables are appended automatically. try: except || datetime, time>| + teint>! - Max file size in bytes. + ‘etimedelta>' - Max age of a file. + ‘etime>' - Time of day. + testr>'- Any of above as a string: 1100 MB', "1 month’, ‘monday at 12:60 Retention Sets a condition which old log files get deleted. retention= || + ‘' - Max number of files. + ‘' - Max age of a file. + ‘! - Max age as a string: '1 week, 3 days','2 months’, # Scraping Serapes Python's URL, version number and logo from Wikipedia page: # $ pip3 install requests beautifulsoup4 import requests, sys from bs4 import Beaut ifulsoup URL = "https: //en.wikipedia.org/wiki/Python_(progranming_language) * try: html = requests.get(URL). text doc = BeautifulSoup(html, ‘html. parser’) table = doc.find(*table', class_=" infobox vevent') rows = table. find all('tr') Link = rows[11].find(‘a') [*hret'] ver = rows(6)-find( ‘div').text-split() [0) url_i = rows[0), find(*ing') {'sre'] image = requests.get(f'https:{url_i}") content with open('test.png', ‘wo') as file: file.write( image} print(Link, ver) except requests .exceptions.ConnectionError: Print("You've got problems with connection.", fil ys.stderr) # Web # $ pip3 install bottle from bottle import run, route, static file, template, post, request, response import json Run run(host=" localhost", por’ # Runs local run(host='0.0.0.0', port=80) # Runs global Static Request @route( '/img/" } def send_image( image): return static _file(image, ‘img_dir/*, mimetype=' image/png") Dynamic Request @route( '/') def send_page(sort): return template( ‘

{{title}}

', title=sport) REST Request @post('/odds/" } def odds_handler(sport): team = request. forms.get(' team") home_odds, away_odds = 2.44, 3.29 response. headers (‘Content-Type ] = ‘application/json* response. headers (*Cache-Controt'] = ‘no-cache’ return json.dunps( (team, home_odds, avay_odds)) # $ pip3 install requests >>> import requests >>> url = thttp://locathost:8080/odds/footbalt* >>> data = {'team': ‘arsenal f.c.'} >>> response = requests.post (url, data=data) >>> response. json() [arsenal f.c.', 2.44, 3.29] # Profiling Stopwatch from tine import tine start_tine = tine() # Seconds since the Epoch. duration = time() - start_tine High performance: from tine import perf_counter start_time = perf_counter() # Seconds since restart. duration = perf_counter() - start_time Timing a Snippet >>> from tineit import timeit po> Cimeit(*"=",join(str(a) for a in range(1@0))" nunber=10000, glovals=globals(), setup: _ pass") 0.34986 Profiling by Line # $ pip3 install Line_profiler menory_profiler @profile def main( a = [*range(10000) | b = {*range(10000) } main() $ kernprof ~lv test. py Line # Hits Time Per Hit % Time Line Contents profile det main(): 1 1128.0 1128.0 27.4 a = [+range(10000)} 1 2994.0 2994.0 © 72.6 b = (rrange(10000) } $ python3 -m menory_profiler test.py Line # Mem usage Increnent Line Contents 1 35,387 MiB 35,387 MIB @profile 2 def main(): 3 35.734 MB 0.348 MiB a = [+range(10000) | 4 36,160 MiB 01826 MiB b = {#range(10000)} all Graph Generates a PNG image ofa call graph with highlighted bottlenecks: # $ pip3 instatt pycaltoraph from pycatlgraph import output, PyCaltcraph from datetine inport datet ine time_str = datetine.now() .strftime( ‘eYamediRARS' ) Filename = f'profite-(tine_str}.png" drawer = output sGraphviz0utput (output_fite=filename) with PycallGraph (drawer) : # NumPy Array manipulation mini-language. It can run up to one hundred times faster than the equivalent Python code. # $ pip3 install numpy import nunpy as np = np.array() = np-arange(from_inclusive, to_exclusive, #step_size) = np-ones() = np. randon. randint(from_inclusive, to_exclusive, ) .shape = eviews = .reshape() eviews = np.broadcast_to(, ) = ,sun(axis) indexes = -argmin(axis) ‘+ Shape is a tuple of dimension sizes. ‘+ Axis isthe index of a dimension that gets collapsed. The leftmost dimension has index 0. Indexing = <2d_array>[0, 0] # First element. [@] # First row. [:, 0) # First column. Also [--+, 0]. <3d_view» = <2d_array>[None, :, ] # Expanded by dimension of size 1. = <2d_array>[, <1d_colunn_indexes>] <2d_array> = <2d_array>[<2d_row_indexes>, <2d_colunn_indexes>] <2d_bools> = <2d_array> > 0 = <2d_array>[<2d_bools>] ‘+ Ifrow and column indexes differ in shape, they are combined with broadcasti Broadcasting Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes. and/or dimensions. lett = [0.1], [0.6], [2.8]] # Shape: (3, 1) right 8) # Shape: (3) 1, 0.6, 8 1. Ifarray shapes iferin length, left-pad the shorter shape with ones: left. = [[0.2], [0.6], [ right = [[0.1, 0.6, # Shape: (3, 1) 0.8]] 0-8]] # Shape: (1, 3) < ! 2. any dimensions differ in size, expand the ones that have size 1 by duplicating their elements: left right (0.1, 0.1, 0.1], (0.6, 0.6, 0.6), (8.8, 0.8, [{@.1, 0.6, 0.8}, [81, 0.6, 8.8], [0.1, 0. 1 08}] # Shape: (3, 3) [1, 2, 1): >>> points = np.array( (0.1, 0.6, @.8]) [@1, 0.6, 048] >>> wrapped_points = points.reshape(3, 1) Tf 9-11, [ 0.6), [0-8}) >>> distances = wrapped_points ~ points I. , -0.5, -0.7], [ 0.5, 0,-0.2], [0.7, 02, @. i] >>> distances = np.abs (distances) [[® , 9.5, O71, [ 0.5, 0. , 0.2), [e.7, 0.2, 0. 3) >>> i £ np-arange(3) [e, 1, 2) bot distances[i, i) = np.inf [L inf, 0.5, 0.7], [0.5, inf, 0.2], [0.7, 0.2, infi] >>> distances. argmin(1) (1, 2, 1) # Image # $ pip3 install pillow from PIL import Image = Inage.new(‘', (width, height)) = Inage.open(*!) = . convert ‘') . save(‘') . show() = .getpixell (x, y)) # Returns a pixel. .putpixel((x, y), ) # Writes a pixel to the image. = .getdata() # Returns a sequence of pixels. . putdata() # Writes a sequence of pixels. .paste(, (x, y)) # Writes an image to the image. <2d_array> = np.array() # Creates NumPy array from greyscale image. <3d_array> = np.array() # Creates NumPy array from color image. = Inage. fromarray() # Creates image from NunPy array of floats. Modes + ‘1! 1-bit pixels, black and white, stored with one pixel per byte. + 'L!. 8:bit pixels, greyscale, ‘RGB! - 3x8-bit pixels, true color. ‘= 'RGBA' - 4x8-bit pixels, true color with transparency mask. + ‘HSV! - 3x8-bit pixels, Hue, Saturation, Value color space. Examples Creates a PNG image of a rainbow gradient: WIDTH, HEIGHT = 100, 100 WIDTH > WEIGHT [255 * i/size for i in range(size)] Tnage.new( "HSV", (WIDTH, HEIGHT)) ng. putdata( [(int(h), 255, 255) for h in hues]) igs convert ("RGB") .save( "test. png") ‘Adds noise toa PNG image: from random import randint add_noise = lambda value: max(®, min(255, value + randint(-20, 20))) 19 = Tnage.open( ‘test. png').convert(*HSV" ) ing. putdata( ((add_noise(h), 5, v) for h, s, v in img.getdata()]} ing, convert (‘RGB') .save( "test. png’) Drawing from PIL import InageDraw = InageDraw.Draw() .point((x, y), fitt-None) .Line((x1, y1, x2, y2 [, +++]), filleNone, width=@, joint-None) .are((x1, yl, x2, y2), fromdeg, to_deg, fill=None, width=6) .rectangle((x1, yl, x2, y2), fillNone, outLine-None, width=0) .polygon( (x1, y1, x2, y2 [,\-.-]), filisNone, outLine-None) .ellipse((x1, yl, x2, y2), filt-None, outline-None, width=0) + Use 'fill=' to set the primary color. + Use outLine=' to set the secondary color. + Golor can be specified as a tuple, int, '#rrqgbb! string or a color name. # Animation Creates a GIF of a bouncing ball: # $ pip3 install pittow imageio from PIL inport Inage, Tnagedraw import inageio WiDTH, R= 126, 10 frames = (] for velocity in range(15) y = sun(range(velocity+1)) frame = Image.new('L*, (WIDTH, WIDTH)) draw = Tnagedraw.Draw(frane) draw.elLipse((WIDTH/2-R, y, WIDTH/2+R, y+Ra2), fitl=‘white') frames.append( frame) frames += reversed( franes(1:-1)) inageio.ninsave('test.gif*, frames, duration=< 03) # Audio import wave = wave.open(*', 'rb") # Opens the WAV Tile. framerate = .getfranerate() # Nunber of franes per second, channels = .getnchanne\s() # Nunber of samples per frane. Sampwidth = -getsampwidth() # Sample size in bytes. nfranes silave_read>-getntranes() # Nunber of franes. sparans> = -getparans() # Inautable collection of above. ,readfranes(nframes) # Returns next *nfranes’ frames. = wave.open( ‘', 'wh!) # Truncates existing file. “Have write>. setfranerate() # 44100 for CD, 48000 for video. ave write>. setnchannels() #1 for mono, 2 for stereo. , setsampwidth() # 2 for Cd quality sound, , setparans ) # Sets all parameters. “Have write>.writeframes () # Aopends frames to the file. + Bytes object contains a sequence of frames, each consisting of one or more samples. + Ima stereo signal, the first sample of a frame belongs to the left channel. + Each sample consists of one or more bytes that, when converted to an integer, indicate the displacement of a speaker membrane at a given moment. ‘+ If sample width is one, then the integer should be encoded unsigned. + For all other sizes, the integer should be encoded signed with little-endian byte order. Sample Values sampwidth min zero max 1 e| 128 255 2 -32768 | 6 32767 3 -8388608 | @ 8388607 4 2147483648 | 0 | 2147483647 Read Float Samples from WAV File def read_wav_file(fitenane): def get_int(a_bytes): an_int = Int. from_bytes(a_bytes, ‘Little’, signed=width!: Feturn an_int - 128 * (width =="1) with wave. open(filename, ‘rb') as file: width = file. getsampwidth() frames = file. readframes(-1) byte_samples = (franes[i: i + width] for i in range(@, len(frames), width)) return [get_int(b) / pow(2, width * 8 - 1) for b in byte samples] Write Float Samples to WAV File def write to_wav_file(filenane, float_samples, nchannels=1, sampwidt! def get_bytes(a_float a_float = max(-1, min(1 - 2e-16, a_float)) alfloat += sanpwidth == 1 afloat += pow(2, sampwidth + 8 - 1) return int(a_float).to_bytes(sampwidth, ‘little’, signed=sampwidth!=1) with wave-open(fitename, ‘'wb') as file: file, setnchannets (nchannets) file, setsampwidth(sampwidth) file. set franerate( framerate) file.writeframes(b''. join(get_bytes(f) for f in float_samples)) » framera} Examples Saves a sine wave to a mono WAV file: from math import pi, sin samples_f = (sin(i + 2 + pi * 440 / 44100) for i in range(100000)) write_t0_wav_file(‘test.wav', samples_f) ‘Adds noise to a mono WAV fie: from random import random add_noise = lambda value: value + (random() - 0.5) + 0.03 samples_f = (add_noise(f) for f in read wav_file('test.wav')) write_t0_wav_file(‘test.wav', samples_f) Plays @ WAV file: # $ pip3 install simpleaudio from sinpleaudio import play_buffer with wave. open(*test.wav', '7b") as file: p= file.getparans() frames = file, readtranes(-1) play_buffer(franes, psnchannels, pasampwidth, p. framerate) Text to Speech # § pip3 install pyttsx3 import pyttsx3 engine = pyttsx3.init() engine. say(*Sally sells seashells by the seashore.") engine. runAndwait() # Synthesizer Plays Popcom by Gershon Kingsley: # $ pip3 install simpleaudio import simpleaudio, math, struct from itertools import chain, repeat 4a100 'TAF 69, ,710,66, ,622,66,,591,,,' STAD, 73, , 74D, 73, , 74) 71, 4730, Thy 73,469, TL 69,472, ,67, 71,4! get_pause’ +" lambda’ seconds:' repeat (@, int(seconds +'F)) sint = lambda i, hz: math.sin(i'* 2 * math.pi x hz / F) get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F))) get_hz = Lambda key: 8.176 * 2% (int (key) / 12) parse_note = lanbda note: (get_hz(note[:2]), 0.25 if ‘2+ in note else 0.125) get_samples = lambda note: get_vave(+parse_ndte(note)) if note else get_pause(0.125) sonples_f = chain. fron_iterable(get_samples(n) for n in £'{P1}{P1}{P2}".split('y")) samplesib = b''.join(struct.pack(' = .subsurtace() # Returns a subsurface. . fill(color) # Fills the whole surface. .blit(, (x, y)) # Draws passed surface to the surface. = pg.transforn.flip(, xbool, ybool) = pg. transform. rotate(, degrees) = pg.transform.scale(, (width, height)) p9-draw. line(, color, (x1, yi), (x2, y2), width) pg-draw.arc(, ‘color, , trom_radians, to_radians) poedraw.rect(, color, ) pg.draw.polygon(, color, points) pg.draw.ellipse(, color, ) Font = pg.font.SysFont(*', size, bold-False, italic-False) = pg.font.Font("', size) = ,render(text, antialias, color {, background)) Sound = pgemixer.Sound( *') # Loads the WAV files . play() # Starts playing the sound. Basic Mario Brothers Example import collections, dataclasses, enum, io, pygame, urtlib.reques: from random import’ randint tertools as P = collections .namedtuple('P', 'x y') # Position D = enum.Enun('D', ‘nes w') # Direction SIZE, MAX_SPEED ='50, P(5, 10) # Screen size, Speed Limit def main(): def get_screen(): pygame.init() return pygane.display.set_node(2 [SIZE+16]) def get_inages( "https: //gto76.github, 1o/python-cheatsheet/web/maric_bros. png! pygane. image, Load( i0.BytesT0(url lib. request.urlopen(url).« read())) return [img. subsurface(get_rect(x, 0)) for x in range(img.get_width() // 16)] def get_mario() Mario = dataclasses.make_dataclass( Mario", ‘rect spd facing left frane_cycle*.split()) return Mario(get_rect(1, 1), P(®, 8), False, it-cycte(range(3))) def get_tiles() positions = [p for p in it.product(range(SIZE), repeat=2) if {#p) & (0, SIZE-1}] + \ [(randint(1, S1ZE-2), randint (2, Siz6-2)) for — in range(SIze*+2 // 10)] return [get_rect(p) for'p in positions] def get_rectix, y) return pygane.Rect(x*16, yx16, 16, 16) run(get_screen(), get_inages(), get_nario(}, get_tites()) def run(screen, images, mario, tiles): clock = pygane.time.Clock() while atl(event. type != pygame.QUIT for event in pygame-event.get()): keys = (pygane.K_UP: Dsn, pygame.K_RIGHT: Dee, pygame.K_DOWN: D.s, pygame.K_LEFT: D.w} pressed = {keyS.get(i) for 2, on in enumerate(pygame.key.get_pressed()) if on} update_speed(mario, tiles, pressed) update_nosition(mario, tiles) draw(screen, images, mario, tiles, pressed) clock. tick(28) def update_speed(nario, tiles, pressed): x, y = mario. spd x's= 2 = (Die in pressed) ~ (D.w in pressed)) x= x // abs(x) if x else @ y = 1 if D.s not in get_boundaries(mario.rect, tiles) else (D.n in pressed) + -10 mario. spd = P(*{max(-Limit, min(Limit, s)} for Limit, s in zip(MAX_SPEED, P(x, y))]) def update_position(nario, tiles): new_p = mario, rect. topleft larger_speed = max(abs(s) for s in mario. spd) for _ In range(larger_speed): Mario.sod = stop_on_collision(mario.spd, get_poundaries(mario.rect, tiles)) new_p = P(x[a + 5/larger_speed for 2, s in zip(new_p, mario.spd)]) mario. rect.topleft = new_p def get_boundaries(rect, tiles! deltas = {D.nz P(@, ~1), Due: P(1, 0), D.s: P(®, 1), D.w: P(-1, 0)} return {d for d, delta in deltas. itens() if rect-move(delta) .collidelist (tiles) ap def stop_on_collision(spd, bounds): return P(x=0 if (D.w in bounds and spd.x < @) or (D.e in bounds and spd.x > 0) else spd.x, y=0 if (D-n in bounds and spd.y < @) or (D.s in bounds and spd.y > 0) else spd.y) def draw(screen, images, mario, tiles, pressed): def get_frame_indexi) if B.s not in get_boundaries(nario. rect, tiles): return 4 return next (mario.frane_cycle) if {0.w, D.e} & pressed else 6 screen.fill((B5, 168, 255)) mario. tacing_lett = (D.w in pressed) if (0.w, 0.e} & pressed else mario. facing left screen. blit(inages (get_frame_index() + nario: facing left * 9}, mario.rect) for rect in tites: screen.bLit(images[18 if {erect.topleft} & {@, (SIZE-1)#16} else 19], rect) pygame. display. flip() if _name_ . main() "ain # Pandas # $ pip3 install pandas import pandas as pd from pandas import Series, DataFrame Series Ordered dictionary with a name. >o> Series((1, 2], index=['x', *y'], name='a') x 1 y 2 Name: a, dtype: intea = Series() # Assigns RangeIndex starting at @ = Series() # Takes dictionary's keys for index. = Series(, index=) # Only keeps items with keys specified in index. = . loc [key] = , loc [keys] = loc[from_key : to_key_inclusive] -iloc [index] -iloc| indexes] t siloc[from_i : to_i_exclusive] sae = [key/index] 1 .key sae = [keys/indexes)] [] = [bools] i/loe [boots] = sess # Returns a Series of bools. = t/ # Non-matching keys get value NaN. = .append() = ,conbine_first() .update() Or: pd. concat () Adds itens that are not yet present. Updates items that are already present. sae ‘Aggregate, Transform, Map: = . sun/max/mean/idxmax/all() = rank/diff/cumsun/fFill/interpl() = ,fillna() 1 .aggregate() ,agg/transform(=trans_func>) .apply/aga/ trans form/map(>> sr = Series([1, 21, index=[%x", 'y']) x 1 y 2 "su Usun'y) | ('st: *sun'y sr-apply(~) 3 sun 3 53 srvagat.-) "rank® Crank) [Crt sreapply(~) rank sr.ag9(.) xu x 1 cxL sr.trans(.) y2 y 2 y 2 ‘+ Last result has a hierarchical index. Use '[key_1, key_2]' to get its values. DataFrame ‘Table with labeled rows and columns. >>> DataFrame([[1, 2], [3, 41], index=['a', xy a12 ba 4 DataFrame() = DataFrane() <> , loc [row_key, colunn_key] = . oc [row_key/s] = .loc[:, column_key/s] <0F> . loc [row_bools, column_bools] = (colunn_key/s] = [row_bodls] [] = >ex= =0F> #-#/ <0F> . set_index(column_key) <0F> / resét_index() <0F> filter('', axis=1) =0F> ,melt (id_vars=colunn_key/s) Merge, Join, Concat: “b'], colums=['x', "y"]) # Rows can be either Lists, dicts or series. # Columns can be either lists, dicts or series. + ,iloc[row_index, colunn_index] .iloc [row_index/es] -iloc[:, column_index/es] ,iloc[rw_pools, colunn_bools) or: Keeps rows as specified by bools. Assigns NaN to False values. ,column_key sae Returns DataFrame Non-matching keys of bools. get value NaN. ae Replaces row keys with values from a colunn. Moves row keys to their own column. only keeps colunns whose key matches the regex. Converts DF from wide to long format, pop | = DataFrame(({1, 2], "b'}, colums=['x", 'y'I) xy a12 b34 poo r = DataFrame(([4, 5], "et], column: yz bas © 67 how/ join outer’ ‘inner “eft* description x y z |x y z |x yz | Joins/merges on column. @1 2 ./3 4 5/1 2 Also accepts left_on and 1304 5 3 4 5 | righton parameters, 2. 67 Uses Tinner' by default. Lijoin(r, Usuffix=' xylyr 2 x yl yr z | Joins/merges on row keys. a 12. .|xytyr z] 12. . | Uses ‘tett* by defauit. b3 44 5/3 445/3 445 cee 67 pd.concat([l, rl, xy 2 y ‘Adds rows at the bottom. axis-0, al2. 2 Uses ‘outer’ by default. joins.) ba 4 2 4 By default works the be. 4 5 4 same as * teappend(r)* cl 6 7 6 pd-concat({l, r], xy ye Adds columns at the axl: al2..Jxyyez right end. Joi b3 445/344 5 Uses ‘outer’ by default, ce 67 Lecombine_first(r) xy 2 Adds missing rows and al2. columns. b3 4 5 cc. 6 7 ‘Aggregate, Transform, Map: = , sum/max/mean/idxmax/alt() # Ort ,apply/agg/transform() = srank/diff/cumsun/ffill/interpl() # Or: .apply/agg/transform() = /fillna() # Or: .applymap() * All operations operate on columns by default. Use 'axis=1' parameter to process the rows instead. >>> df = DataFrame({{1, 2], [3, 41], index=['a', 'b'], colunns=['x", ‘y']) xy a12 b34 “sun Csumty | (txt: tsum'} of -apply(.) xy of.ag9(..) x4 sun 4 6 x4 y 6 "rank* Uirank') | (txts trank'} of apply(.) xy xy x df-agg(..) aii rank rank al dfetrans(.) | b 22 fa a 4 b2 bo2 2 + Use "[col_key_1, col_key_2] [row_key] ' to get the fifth result’s values. Encode, Decode: = pd. read_json/html( *") = pd. read_csv/pickle/excel( '") = pd, read_sql(‘', ) = pd. read_clipboard() = .to_dict (['d/1/s/sp/r/i']) = .to_json/ntml/csv/markdown/Latex( [] ) . to_pickle/excel () ,to_sql(‘", ) GroupBy Object that groups together rows of a dataframe based on the value of the passed column. pom df = DataFrame({{1, 2, 3], (4, 5, 6], (7, 8, 6]], index=List( ‘abet >o> df. groupby('z').get_group(3) + columns=List('xyz")) xy a12 >>> df, groupby(*z"). get_group(6) xy bas c78 = .groupby(column_key/s) # DF is split into groups based on passed column. = ,get_group(group_key) # Selects a group by value of grouping colunn, Aggregate, Transform, Map: = . sun/max/mean/idxmax/all() = :rank/diff/cumsum/ffill() = .fillna() + .apply/agg() - aggregate() ! ,transform() >e> gb = df.groupby(‘z') x ye ai23 rb 45 6 c7 86 "sun" "rank Crank") | (x's " rank") sb-a99(~) xy xy xy x z ait | rankrank| oad 312] cia fanaa] ba 6u3]¢22 {6 1 if e2 <2 2 gb-trans(.) x y xy aid] ail bus] oii eup| cil Rolling Object for rolling window calculations, = . rolling (window_size) *min_periods-None, center=False’. = [column_key/s] <>. colunn_key = . sun/max/nean() . apply/agg() # Plotly Covid Deaths by Continent content Total Deaths per Mi vars Mor 29 pornz poras May 10 ay 24 un? na # $ pip3 install pandas plotly import pandas as pd import plotly. express covid = pd. read_csv( ‘https: //covid.ourworldindata. org/data/owid-covid-data.csv', usecols=['iso_code', ‘date’, ‘total_deaths', ‘population’ ]) continents = pd.read_csv( ‘https: /7datahub. io/JohnSnowLabs/country-and-continent-codes-' + \ “List/r/country-and—cont inent-codes-List-csv. csv", usecols=['Three_Letter_Country_Code', *Continent_Name"]) pdemerge(covid, continents, left_on='iso_code", right_on='Three_Letter_Country_Code') df-groupby({'Continent Name", ‘date']).sum().reset_index() df['Total Deaths per Million'] = df.total_deaths + 1e6 7 df.population df = df[('2020-3-14" < df-date) & (df.date < '2020-06-25")) df = df,renane({'date*: ‘Date’, ‘Continent Name": "Continent*}, axis: plotly.express.line(df, x='Date", y='Total Deaths per Million’, colo ‘columns’ ) Continent!) .show(} Confirmed Covid Cases, Dow Jones, Gold, and Bitcoin Price 10 Gold Mart Mart Marzo Ariz Aprzs May May2t un nz # $ pip3 install pandas plotly import pandas, datetime import plotly-graph_objects as go def main(): display_data(wrangle_data(*scrape_data())) def scrape_data(): def scrape_yahoo( id_) BASE_UAL = ‘https: //query1.finance. yahoo. con/v7/finance/download/' now = int(datetime. datetime. now().tinestamp()) url = #'{BASE_URL}{id_}?period1=157965120a6period2-(now)Sinterval=1déevents-history* return pandas.read_csv(url, usecols=['Date*, ‘Close']).set_index('Date').Close covid = pd-read_csv( *https: //covid. ourworldindata.org/data/owid-covid-data. csv", usecols=['date', 'total_cases*]) covid = covid.groupby('date*). sum() dow, gold, bitcoin = [scrape yahoo(id_) for id_ in (**DII', 'GC=F', *BTC-USD')] dow.name, ‘gold.name, bitcoin.name = ‘Dow Jones", ‘Gold’, ‘Bitcoin return cavid, dow, gold, bitcoin def wrangle_data(covid, dow, gold, bitcoin): df = pandas.concat({covid, dow, gold, bitcoin], axis=1) dt = df. loc{*2020-02-23":].iloct:-2] df = df-interpolate() Gf-iloc[:, 1:] = df-roLling(1@, min_periods=1, center=True) .mean(),iloc{:, 12] dfeiloc[:, 1:] = dfeiloct:, 12] / df-itoc(e, 1:] * 100 return df def display_data(dt def get_trace(col_nane) : Feturn go.Scatter(x=df.index, y=df[cot_name], nant traces = [get_trace(col_name) for col_name in df. columns [: traces. append(go.Scatter(x=df.index, y=df.total_cases, name: figure = go.Figure() figure.add_traces(traces) figure. update_layout ( yaxisi=dict(title='Total Cases", rangemode='tozero'), yaxis2=dict(title="%', rangenode='tozero', overlaying='y', side="right'), ‘L_name, yaxis='y2") 1 "Total Cases", yaxis="yl')) Legend=dict (x=1.1) )«show() if Lnane_ == ‘pain’ main() # Cython Library that compiles Python code into C. # $ pip3 install cython import pyxinport; pyximport.install() import ecython_script>.main() Definitions * All 'cdef' definitions are optional, but they contribute to the speed-up. * Script needs to be saved with a 'pyx' extension. cdet = cdef [n_elements] = [, , - cdef ( , +++) cdef class : cdef public def _init_(self, ) Self, = cdef enum : , , ... # Appendix Pyinstaller $ pip3 install pyinstatler $ pyinstaller script.py # Compiles into './dist/script! directory. $ pyinstaller script.py —-onefile # Compiles into '-/dist/script' console app. $ pyinstaller script.py --windowes # Compiles into './dist/script' windowed app. $ pyinstaller script-py —-add-data ‘:.' # Adds file to the root of the executable. + File paths need to be updated to 'os.path. join(sys._MEIPASS, )'. Basic Script Template #!/usr/bin/env python3 # # Usage: «py # from collections import namedtupte from dataclasses import make_datactass from enum import Enum from sys import argv import re def main): pass 0 fe UTIL * def read_file( filenane| with open( filename, encoding='utf-8") as file: return file. readlines() if _name_ main() saty 22020 te Sorn

You might also like