You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2625 lines
72 KiB

6 years ago
7 years ago
5 years ago
7 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
7 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
7 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
  1. Comprehensive Python Cheatsheet
  2. ===============================
  3. <sup>[Download text file](https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md), [Fork me on GitHub](https://github.com/gto76/python-cheatsheet) or [Check out FAQ](https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions).
  4. </sup>
  5. ![Monty Python](web/image_888.jpeg)
  6. Contents
  7. --------
  8. **&nbsp;&nbsp;&nbsp;** **1. Collections:** **&nbsp;** **[`List`](#list)**__,__ **[`Dictionary `](#dictionary)**__,__ **[`Set`](#set)**__,__ **[`Tuple`](#tuple)**__,__ **[`Range`](#range)**__,__ **[`Enumerate`](#enumerate)**__,__ **[`Iterator`](#iterator)**__,__ **[`Generator`](#generator)**__.__
  9. **&nbsp;&nbsp;&nbsp;** **2. Types:** **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** **[`Type`](#type)**__,__ **[`String`](#string)**__,__ **[`Regular_Exp`](#regex)**__,__ **[`Format`](#format)**__,__ **[`Numbers`](#numbers)**__,__ **[`Combinatorics`](#combinatorics)**__,__ **[`Datetime`](#datetime)**__.__
  10. **&nbsp;&nbsp;&nbsp;** **3. Syntax:** **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** **[`Args`](#arguments)**__,__ **[`Inline`](#inline)**__,__ **[`Closure`](#closure)**__,__ **[`Decorator`](#decorator)**__,__ **[`Class`](#class)**__,__ **[`Duck_Types`](#duck-types)**__,__ **[`Enum`](#enum)**__,__ **[`Exceptions`](#exceptions)**__.__
  11. **&nbsp;&nbsp;&nbsp;** **4. System:** **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#path)**__,__ **[`Command_Execution`](#command-execution)**__.__
  12. **&nbsp;&nbsp;&nbsp;** **5. Data:** **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** **[`CSV`](#csv)**__,__ **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`MemoryView`](#memory-view)**__,__ **[`Deque`](#deque)**__.__
  13. **&nbsp;&nbsp;&nbsp;** **6. Advanced:** **&nbsp;&nbsp;&nbsp;** **[`Threading`](#threading)**__,__ **[`Introspection`](#introspection)**__,__ **[`Metaprograming`](#metaprograming)**__,__ **[`Operator`](#operator)**__,__ **[`Eval`](#eval)**__,__ **[`Coroutine`](#coroutine)**__.__
  14. **&nbsp;&nbsp;&nbsp;** **7. Libraries:** **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profile)**__,__
  15. **&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;** **[`NumPy`](#numpy)**__,__ **[`Image`](#image)**__,__ **[`Audio`](#audio)**__.__
  16. Main
  17. ----
  18. ```python
  19. if __name__ == '__main__': # Runs main() if file wasn't imported.
  20. main()
  21. ```
  22. List
  23. ----
  24. ```python
  25. <list> = <list>[from_inclusive : to_exclusive : ±step_size]
  26. ```
  27. ```python
  28. <list>.append(<el>) # Or: <list> += [<el>]
  29. <list>.extend(<collection>) # Or: <list> += <collection>
  30. ```
  31. ```python
  32. <list>.sort()
  33. <list>.reverse()
  34. <list> = sorted(<collection>)
  35. <iter> = reversed(<list>)
  36. ```
  37. ```python
  38. sum_of_elements = sum(<collection>)
  39. elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)]
  40. sorted_by_second = sorted(<collection>, key=lambda el: el[1])
  41. sorted_by_both = sorted(<collection>, key=lambda el: (el[1], el[0]))
  42. flatter_list = list(itertools.chain.from_iterable(<list>))
  43. product_of_elems = functools.reduce(lambda out, x: out * x, <collection>)
  44. list_of_chars = list(<str>)
  45. ```
  46. ```python
  47. index = <list>.index(<el>) # Returns index of first occurrence or raises ValueError.
  48. <list>.insert(index, <el>) # Inserts item at index and moves the rest to the right.
  49. <el> = <list>.pop([index]) # Removes and returns item at index or from the end.
  50. <list>.remove(<el>) # Removes first occurrence of item or raises ValueError.
  51. <list>.clear() # Removes all items. Also works on dict and set.
  52. ```
  53. Dictionary
  54. ----------
  55. ```python
  56. <view> = <dict>.keys() # Coll. of keys that reflects changes.
  57. <view> = <dict>.values() # Coll. of values that reflects changes.
  58. <view> = <dict>.items() # Coll. of key-value tuples.
  59. ```
  60. ```python
  61. value = <dict>.get(key, default=None) # Returns default if key does not exist.
  62. value = <dict>.setdefault(key, default=None) # Same, but also adds default to dict.
  63. <dict> = collections.defaultdict(<type>) # Creates a dict with default value of type.
  64. <dict> = collections.defaultdict(lambda: 1) # Creates a dict with default value 1.
  65. ```
  66. ```python
  67. <dict>.update(<dict>)
  68. <dict> = dict(<collection>) # Creates a dict from coll. of key-value pairs.
  69. <dict> = dict(zip(keys, values)) # Creates a dict from two collections.
  70. <dict> = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys.
  71. ```
  72. ```python
  73. value = <dict>.pop(key) # Removes item from dictionary.
  74. {k: v for k, v in <dict>.items() if k in keys} # Filters dictionary by keys.
  75. ```
  76. ### Counter
  77. ```python
  78. >>> from collections import Counter
  79. >>> colors = ['red', 'blue', 'yellow', 'blue', 'red', 'blue']
  80. >>> counter = Counter(colors)
  81. Counter({'blue': 3, 'red': 2, 'yellow': 1})
  82. >>> counter.most_common()[0]
  83. ('blue', 3)
  84. ```
  85. Set
  86. ---
  87. ```python
  88. <set> = set()
  89. ```
  90. ```python
  91. <set>.add(<el>) # Or: <set> |= {<el>}
  92. <set>.update(<collection>) # Or: <set> |= <set>
  93. ```
  94. ```python
  95. <set> = <set>.union(<coll.>) # Or: <set> | <set>
  96. <set> = <set>.intersection(<coll.>) # Or: <set> & <set>
  97. <set> = <set>.difference(<coll.>) # Or: <set> - <set>
  98. <set> = <set>.symmetric_difference(<coll.>) # Or: <set> ^ <set>
  99. <bool> = <set>.issubset(<coll.>) # Or: <set> <= <set>
  100. <bool> = <set>.issuperset(<coll.>) # Or: <set> >= <set>
  101. ```
  102. ```python
  103. <set>.remove(<el>) # Raises KeyError.
  104. <set>.discard(<el>) # Doesn't raise an error.
  105. ```
  106. ### Frozen Set
  107. * **Is immutable and hashable.**
  108. * **That means it can be used as a key in a dictionary or as an element in a set.**
  109. ```python
  110. <frozenset> = frozenset(<collection>)
  111. ```
  112. Tuple
  113. -----
  114. **Tuple is an immutable and hashable list.**
  115. ```python
  116. <tuple> = ()
  117. <tuple> = (<el>, )
  118. <tuple> = (<el_1>, <el_2>, ...)
  119. ```
  120. ### Named Tuple
  121. **Tuple's subclass with named elements.**
  122. ```python
  123. >>> from collections import namedtuple
  124. >>> Point = namedtuple('Point', 'x y')
  125. >>> p = Point(1, y=2)
  126. Point(x=1, y=2)
  127. >>> p[0]
  128. 1
  129. >>> p.x
  130. 1
  131. >>> getattr(p, 'y')
  132. 2
  133. >>> p._fields # Or: Point._fields
  134. ('x', 'y')
  135. ```
  136. Range
  137. -----
  138. ```python
  139. <range> = range(to_exclusive)
  140. <range> = range(from_inclusive, to_exclusive)
  141. <range> = range(from_inclusive, to_exclusive, ±step_size)
  142. ```
  143. ```python
  144. from_inclusive = <range>.start
  145. to_exclusive = <range>.stop
  146. ```
  147. Enumerate
  148. ---------
  149. ```python
  150. for i, el in enumerate(<collection> [, i_start]):
  151. ...
  152. ```
  153. Iterator
  154. --------
  155. ```python
  156. <iter> = iter(<collection>) # Calling `iter(<iter>)` returns unmodified iterator.
  157. <iter> = iter(<function>, to_exclusive) # Sequence of return values until 'to_exclusive'.
  158. <el> = next(<iter> [, default]) # Raises StopIteration or returns 'default' on end.
  159. ```
  160. ### Itertools
  161. ```python
  162. from itertools import count, repeat, cycle, chain, islice
  163. ```
  164. ```python
  165. <iter> = count(start=0, step=1) # Returns incremented value endlessly.
  166. <iter> = repeat(<el> [, times]) # Returns element endlessly or 'times' times.
  167. <iter> = cycle(<collection>) # Repeats the sequence indefinitely.
  168. ```
  169. ```python
  170. <iter> = chain(<coll.>, <coll.> [, ...]) # Empties collections in order.
  171. <iter> = chain.from_iterable(<collection>) # Empties collections inside a collection in order.
  172. ```
  173. ```python
  174. <iter> = islice(<collection>, to_exclusive)
  175. <iter> = islice(<collection>, from_inclusive, to_exclusive)
  176. <iter> = islice(<collection>, from_inclusive, to_exclusive, +step_size)
  177. ```
  178. Generator
  179. ---------
  180. * **Convenient way to implement the iterator protocol.**
  181. * **Any function that contains a yield statement returns a generator object.**
  182. * **Generators and iterators are interchangeable.**
  183. ```python
  184. def count(start, step):
  185. while True:
  186. yield start
  187. start += step
  188. ```
  189. ```python
  190. >>> counter = count(10, 2)
  191. >>> next(counter), next(counter), next(counter)
  192. (10, 12, 14)
  193. ```
  194. Type
  195. ----
  196. * **Everything is an object.**
  197. * **Every object has a type.**
  198. * **Type and class are synonymous.**
  199. ```python
  200. <type> = type(<el>) # Or: <el>.__class__
  201. <bool> = isinstance(<el>, <type>) # Or: issubclass(type(<el>), <type>)
  202. ```
  203. ```python
  204. >>> type('a'), 'a'.__class__, str
  205. (<class 'str'>, <class 'str'>, <class 'str'>)
  206. ```
  207. #### Some types do not have builtin names, so they must be imported:
  208. ```python
  209. from types import FunctionType, MethodType, LambdaType, GeneratorType
  210. ```
  211. ### ABC
  212. **An abstract base class introduces virtual subclasses, that don’t inherit from it but are still recognized by isinstance() and issubclass().**
  213. ```python
  214. >>> from collections.abc import Sequence, Collection, Iterable
  215. >>> isinstance([1, 2, 3], Iterable)
  216. True
  217. ```
  218. ```text
  219. +------------------+----------+------------+----------+
  220. | | Sequence | Collection | Iterable |
  221. +------------------+----------+------------+----------+
  222. | list, range, str | yes | yes | yes |
  223. | dict, set | | yes | yes |
  224. | iter | | | yes |
  225. +------------------+----------+------------+----------+
  226. ```
  227. ```python
  228. >>> from numbers import Integral, Rational, Real, Complex, Number
  229. >>> isinstance(123, Number)
  230. True
  231. ```
  232. ```text
  233. +--------------------+----------+----------+------+---------+--------+
  234. | | Integral | Rational | Real | Complex | Number |
  235. +--------------------+----------+----------+------+---------+--------+
  236. | int | yes | yes | yes | yes | yes |
  237. | fractions.Fraction | | yes | yes | yes | yes |
  238. | float | | | yes | yes | yes |
  239. | complex | | | | yes | yes |
  240. +--------------------+----------+----------+------+---------+--------+
  241. ```
  242. String
  243. ------
  244. ```python
  245. <str> = <str>.strip() # Strips all whitespace characters from both ends.
  246. <str> = <str>.strip('<chars>') # Strips all passed characters from both ends.
  247. ```
  248. ```python
  249. <list> = <str>.split() # Splits on one or more whitespace characters.
  250. <list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times.
  251. <list> = <str>.splitlines(keepends=False) # Splits on line breaks. Keeps them if 'keepends'.
  252. <str> = <str>.join(<coll_of_strings>) # Joins elements using string as separator.
  253. ```
  254. ```python
  255. <bool> = <sub_str> in <str> # Checks if string contains a substring.
  256. <bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
  257. <bool> = <str>.endswith(<sub_str>) # Pass tuple of strings for multiple options.
  258. <int> = <str>.find(<sub_str>) # Returns start index of first match or -1.
  259. <int> = <str>.index(<sub_str>) # Same but raises ValueError.
  260. ```
  261. ```python
  262. <str> = <str>.replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times.
  263. <bool> = <str>.isnumeric() # True if str contains only numeric characters.
  264. <list> = textwrap.wrap(<str>, width) # Nicely breaks string into lines.
  265. ```
  266. * **Also: `'lstrip()'`, `'rstrip()'`.**
  267. * **Also: `'lower()'`, `'upper()'`, `'capitalize()'` and `'title()'`.**
  268. ### Char
  269. ```python
  270. <str> = chr(<int>) # Converts int to unicode char.
  271. <int> = ord(<str>) # Converts unicode char to int.
  272. ```
  273. ```python
  274. >>> ord('0'), ord('9')
  275. (48, 57)
  276. >>> ord('A'), ord('Z')
  277. (65, 90)
  278. >>> ord('a'), ord('z')
  279. (97, 122)
  280. ```
  281. Regex
  282. -----
  283. ```python
  284. import re
  285. <str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences.
  286. <list> = re.findall(<regex>, text) # Returns all occurrences.
  287. <list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to keep the matches.
  288. <Match> = re.search(<regex>, text) # Searches for first occurrence of pattern.
  289. <Match> = re.match(<regex>, text) # Searches only at the beginning of the text.
  290. <iter> = re.finditer(<regex>, text) # Returns all occurrences as match objects.
  291. ```
  292. * **Argument `'flags=re.IGNORECASE'` can be used with all functions.**
  293. * **Argument `'flags=re.MULTILINE'` makes `'^'` and `'$'` match the start/end of each line.**
  294. * **Argument `'flags=re.DOTALL'` makes dot also accept newline.**
  295. * **Use `r'\1'` or `'\\1'` for backreference.**
  296. * **Use `'?'` to make an operator non-greedy.**
  297. ### Match Object
  298. ```python
  299. <str> = <Match>.group() # Whole match. Also group(0).
  300. <str> = <Match>.group(1) # Part in first bracket.
  301. <tuple> = <Match>.groups() # All bracketed parts.
  302. <int> = <Match>.start() # Start index of a match.
  303. <int> = <Match>.end() # Exclusive end index of a match.
  304. ```
  305. ### Special Sequences
  306. * **By default digits, whitespaces and alphanumerics from all alphabets are matched, unless `'flags=re.ASCII'` argument is used.**
  307. * **Use capital letters for negation.**
  308. ```python
  309. '\d' == '[0-9]' # Digit
  310. '\s' == '[ \t\n\r\f\v]' # Whitespace
  311. '\w' == '[a-zA-Z0-9_]' # Alphanumeric
  312. ```
  313. Format
  314. ------
  315. ```python
  316. <str> = f'{<el_1>}, {<el_2>}'
  317. <str> = '{}, {}'.format(<el_1>, <el_2>)
  318. ```
  319. ### Attributes
  320. ```python
  321. >>> from collections import namedtuple
  322. >>> Person = namedtuple('Person', 'name height')
  323. >>> person = Person('Jean-Luc', 187)
  324. >>> f'{person.height}'
  325. '187'
  326. >>> '{p.height}'.format(p=person)
  327. '187'
  328. ```
  329. ### General Options
  330. ```python
  331. {<el>:<10} # '<el> '
  332. {<el>:^10} # ' <el> '
  333. {<el>:>10} # ' <el>'
  334. ```
  335. ```python
  336. {<el>:.<10} # '<el>......'
  337. {<el>:>0} # '<el>'
  338. ```
  339. ### Strings
  340. **`'!r'` calls object's repr() method, instead of str(), to get a string.**
  341. ```python
  342. {'abcde'!r:<10} # "'abcde' "
  343. {'abcde':.3} # 'abc'
  344. {'abcde':10.3} # 'abc '
  345. ```
  346. ### Numbers
  347. ```python
  348. { 123456:10,} # ' 123,456'
  349. { 123456:10_} # ' 123_456'
  350. { 123456:+10} # ' +123456'
  351. {-123456:=10} # '- 123456'
  352. { 123456: } # ' 123456'
  353. {-123456: } # '-123456'
  354. ```
  355. ### Floats
  356. ```python
  357. {1.23456:10.3} # ' 1.23'
  358. {1.23456:10.3f} # ' 1.235'
  359. {1.23456:10.3e} # ' 1.235e+00'
  360. {1.23456:10.3%} # ' 123.456%'
  361. ```
  362. #### Comparison of float presentation types:
  363. ```text
  364. +----------------+----------------+---------------+----------------+-----------------+
  365. | | {<float>} | {<float>:f} | {<float>:e} | {<float>:%} |
  366. +----------------+----------------+---------------+----------------+-----------------+
  367. | 0.000056789 | '5.6789e-05' | '0.000057' | '5.678900e-05' | '0.005679%' |
  368. | 0.00056789 | '0.00056789' | '0.000568' | '5.678900e-04' | '0.056789%' |
  369. | 0.0056789 | '0.0056789' | '0.005679' | '5.678900e-03' | '0.567890%' |
  370. | 0.056789 | '0.056789' | '0.056789' | '5.678900e-02' | '5.678900%' |
  371. | 0.56789 | '0.56789' | '0.567890' | '5.678900e-01' | '56.789000%' |
  372. | 5.6789 | '5.6789' | '5.678900' | '5.678900e+00' | '567.890000%' |
  373. | 56.789 | '56.789' | '56.789000' | '5.678900e+01' | '5678.900000%' |
  374. | 567.89 | '567.89' | '567.890000' | '5.678900e+02' | '56789.000000%' |
  375. +----------------+----------------+---------------+----------------+-----------------+
  376. ```
  377. ```text
  378. +----------------+----------------+---------------+----------------+-----------------+
  379. | | {<float>:.2} | {<float>:.2f} | {<float>:.2e} | {<float>:.2%} |
  380. +----------------+----------------+---------------+----------------+-----------------+
  381. | 0.000056789 | '5.7e-05' | '0.00' | '5.68e-05' | '0.01%' |
  382. | 0.00056789 | '0.00057' | '0.00' | '5.68e-04' | '0.06%' |
  383. | 0.0056789 | '0.0057' | '0.01' | '5.68e-03' | '0.57%' |
  384. | 0.056789 | '0.057' | '0.06' | '5.68e-02' | '5.68%' |
  385. | 0.56789 | '0.57' | '0.57' | '5.68e-01' | '56.79%' |
  386. | 5.6789 | '5.7' | '5.68' | '5.68e+00' | '567.89%' |
  387. | 56.789 | '5.7e+01' | '56.79' | '5.68e+01' | '5678.90%' |
  388. | 567.89 | '5.7e+02' | '567.89' | '5.68e+02' | '56789.00%' |
  389. +----------------+----------------+---------------+----------------+-----------------+
  390. ```
  391. ### Ints
  392. ```python
  393. {90:c} # 'Z'
  394. {90:X} # '5A'
  395. {90:b} # '1011010'
  396. ```
  397. Numbers
  398. -------
  399. ```python
  400. <int> = int(<float/str/bool>) # Or: math.floor(<float>)
  401. <float> = float(<int/str/bool>)
  402. <complex> = complex(real=0, imag=0) # Or: <real> + <real>j
  403. <Fraction> = fractions.Fraction(numerator=0, denominator=1)
  404. ```
  405. * **`'int(<str>)'` and `'float(<str>)'` raise 'ValueError' on malformed strings.**
  406. ### Basic Functions
  407. ```python
  408. <num> = pow(<num>, <num>) # Or: <num> ** <num>
  409. <real> = abs(<num>)
  410. <int> = round(<real>)
  411. <real> = round(<real>, ±ndigits) # `round(126, -1) == 130`
  412. ```
  413. ### Math
  414. ```python
  415. from math import e, pi, inf, nan
  416. from math import cos, acos, sin, asin, tan, atan, degrees, radians
  417. from math import log, log10, log2
  418. ```
  419. ### Statistics
  420. ```python
  421. from statistics import mean, median, variance, pvariance, pstdev
  422. ```
  423. ### Random
  424. ```python
  425. from random import random, randint, choice, shuffle
  426. <float> = random()
  427. <int> = randint(from_inclusive, to_inclusive)
  428. <el> = choice(<list>)
  429. shuffle(<list>)
  430. ```
  431. ### Bin, Hex
  432. ```python
  433. <int> = 0b<bin> # Or: 0x<hex>
  434. <int> = int('0b<bin>', 0) # Or: int('0x<hex>', 0)
  435. <int> = int('<bin>', 2) # Or: int('<hex>', 16)
  436. '0b<bin>' = bin(<int>) # Or: '0x<hex>' = hex(<int>)
  437. ```
  438. ### Bitwise Operators
  439. ```python
  440. <int> = <int> & <int> # And
  441. <int> = <int> | <int> # Or
  442. <int> = <int> ^ <int> # Xor (0 if both bits equal)
  443. <int> = <int> << n_bits # Shift left
  444. <int> = <int> >> n_bits # Shift right
  445. <int> = ~<int> # Compliment (flips bits)
  446. ```
  447. Combinatorics
  448. -------------
  449. * **Every function returns an iterator.**
  450. * **If you want to print the iterator, you need to pass it to the list() function!**
  451. ```python
  452. from itertools import product, combinations, combinations_with_replacement, permutations
  453. ```
  454. ```python
  455. >>> product([0, 1], repeat=3)
  456. [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
  457. (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
  458. ```
  459. ```python
  460. >>> product('ab', '12')
  461. [('a', '1'), ('a', '2'),
  462. ('b', '1'), ('b', '2')]
  463. ```
  464. ```python
  465. >>> combinations('abc', 2)
  466. [('a', 'b'), ('a', 'c'), ('b', 'c')]
  467. ```
  468. ```python
  469. >>> combinations_with_replacement('abc', 2)
  470. [('a', 'a'), ('a', 'b'), ('a', 'c'),
  471. ('b', 'b'), ('b', 'c'),
  472. ('c', 'c')]
  473. ```
  474. ```python
  475. >>> permutations('abc', 2)
  476. [('a', 'b'), ('a', 'c'),
  477. ('b', 'a'), ('b', 'c'),
  478. ('c', 'a'), ('c', 'b')]
  479. ```
  480. Datetime
  481. --------
  482. * **Module 'datetime' provides 'date' `<D>`, 'time' `<T>`, 'datetime' `<DT>` and 'timedelta' `<TD>` classes. All are immutable and hashable.**
  483. * **Time and datetime can be 'aware' `<a>`, meaning they have defined timezone, or 'naive' `<n>`, meaning they don't.**
  484. * **If object is naive it is presumed to be in system's timezone.**
  485. ```python
  486. from datetime import date, time, datetime, timedelta
  487. from dateutil.tz import UTC, tzlocal, gettz
  488. ```
  489. ### Constructors
  490. ```python
  491. <D> = date(year, month, day)
  492. <T> = time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, fold=0)
  493. <DT> = datetime(year, month, day, hour=0, minute=0, second=0, ...)
  494. <TD> = timedelta(days=0, seconds=0, microseconds=0, milliseconds=0,
  495. minutes=0, hours=0, weeks=0)
  496. ```
  497. * **Use `'<D/DT>.weekday()'` to get the day of the week (Mon == 0).**
  498. * **`'fold=1'` means second pass in case of time jumping back for one hour.**
  499. ### Now
  500. ```python
  501. <D/DTn> = D/DT.today() # Current local date or naive datetime.
  502. <DTn> = DT.utcnow() # Naive datetime from current UTC time.
  503. <DTa> = DT.now(<tzinfo>) # Aware datetime from current tz time.
  504. ```
  505. * **To extract time use `'<DTn>.time()'`, `'<DTa>.time()'` or `'<DTa>.timetz()'`.**
  506. ### Timezone
  507. ```python
  508. <tzinfo> = UTC # UTC timezone. London without DST.
  509. <tzinfo> = tzlocal() # Local timezone. Also gettz().
  510. <tzinfo> = gettz('<Cont.>/<City>') # Timezone from 'Continent/City_Name' str.
  511. ```
  512. ```python
  513. <DTa> = <DT>.astimezone(<tzinfo>) # Datetime, converted to passed timezone.
  514. <Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Unconverted object with new timezone.
  515. ```
  516. ### Encode
  517. ```python
  518. <D/T/DT> = D/T/DT.fromisoformat('<iso>') # Object from ISO string.
  519. <DT> = DT.strptime(<str>, '<format>') # Datetime from str, according to format.
  520. <D/DTn> = D/DT.fromordinal(<int>) # D/DTn from days since Christ, at midnight.
  521. <DTn> = DT.fromtimestamp(<real>) # Local time DTn from seconds since Epoch.
  522. <DTa> = DT.fromtimestamp(<real>, <tz.>) # Aware datetime from seconds since Epoch.
  523. ```
  524. * **ISO strings come in following forms: `'YYYY-MM-DD'`, `'HH:MM:SS.ffffff[±<offset>]'`, or both separated by `'T'`. Offset is formatted as: `'HH:MM'`.**
  525. * **On Unix systems Epoch is `'1970-01-01 00:00 UTC'`, `'1970-01-01 01:00 CET'`, ...**
  526. ### Decode
  527. ```python
  528. <str> = <D/T/DT>.isoformat() # ISO string representation.
  529. <str> = <D/T/DT>.strftime('<format>') # Custom string representation.
  530. <int> = <D/DT>.toordinal() # Days since Christ, ignoring time and tz.
  531. <float> = <DTn>.timestamp() # Seconds since Epoch from DTn in local time.
  532. <float> = <DTa>.timestamp() # Seconds since Epoch from DTa.
  533. ```
  534. ### Format
  535. ```python
  536. >>> from datetime import datetime
  537. >>> dt = datetime.strptime('2015-05-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z')
  538. >>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z")
  539. "Thursday, 14th of May '15, 11:39PM UTC+02:00"
  540. ```
  541. * **For abbreviated weekday and month use `'%a'` and `'%b'`.**
  542. * **When parsing, `'%z'` also accepts `'±HH:MM'`.**
  543. ### Arithmetics
  544. ```python
  545. <D/DT> = <D/DT> ± <TD>
  546. <TD> = <TD> ± <TD>
  547. <TD> = <TD> */ <real>
  548. <float> = <TD> / <TD>
  549. ```
  550. Arguments
  551. ---------
  552. ### Inside Function Call
  553. ```python
  554. <function>(<positional_args>) # f(0, 0)
  555. <function>(<keyword_args>) # f(x=0, y=0)
  556. <function>(<positional_args>, <keyword_args>) # f(0, y=0)
  557. ```
  558. ### Inside Function Definition
  559. ```python
  560. def f(<nondefault_args>): # def f(x, y):
  561. def f(<default_args>): # def f(x=0, y=0):
  562. def f(<nondefault_args>, <default_args>): # def f(x, y=0):
  563. ```
  564. Splat Operator
  565. --------------
  566. ### Inside Function Call
  567. **Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.**
  568. ```python
  569. args = (1, 2)
  570. kwargs = {'x': 3, 'y': 4, 'z': 5}
  571. func(*args, **kwargs)
  572. ```
  573. #### Is the same as:
  574. ```python
  575. func(1, 2, x=3, y=4, z=5)
  576. ```
  577. ### Inside Function Definition
  578. **Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.**
  579. ```python
  580. def add(*a):
  581. return sum(a)
  582. ```
  583. ```python
  584. >>> add(1, 2, 3)
  585. 6
  586. ```
  587. #### Legal argument combinations:
  588. ```python
  589. def f(x, y, z): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
  590. def f(*, x, y, z): # f(x=1, y=2, z=3)
  591. def f(x, *, y, z): # f(x=1, y=2, z=3) | f(1, y=2, z=3)
  592. def f(x, y, *, z): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)
  593. ```
  594. ```python
  595. def f(*args): # f(1, 2, 3)
  596. def f(x, *args): # f(1, 2, 3)
  597. def f(*args, z): # f(1, 2, z=3)
  598. def f(x, *args, z): # f(1, 2, z=3)
  599. ```
  600. ```python
  601. def f(**kwargs): # f(x=1, y=2, z=3)
  602. def f(x, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3)
  603. def f(*, x, **kwargs): # f(x=1, y=2, z=3)
  604. ```
  605. ```python
  606. def f(*args, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
  607. def f(x, *args, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
  608. def f(*args, y, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3)
  609. def f(x, *args, z, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)
  610. ```
  611. ### Other Uses
  612. ```python
  613. <list> = [*<collection> [, ...]]
  614. <set> = {*<collection> [, ...]}
  615. <tuple> = (*<collection>, [...])
  616. <dict> = {**<dict> [, ...]}
  617. ```
  618. ```python
  619. head, *body, tail = <collection>
  620. ```
  621. Inline
  622. ------
  623. ### Lambda
  624. ```python
  625. <function> = lambda: <return_value>
  626. <function> = lambda <argument_1>, <argument_2>: <return_value>
  627. ```
  628. ### Comprehension
  629. ```python
  630. <list> = [i+1 for i in range(10)] # [1, 2, ..., 10]
  631. <set> = {i for i in range(10) if i > 5} # {6, 7, 8, 9}
  632. <iter> = (i+5 for i in range(10)) # (5, 6, ..., 14)
  633. <dict> = {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18}
  634. ```
  635. ```python
  636. out = [i+j for i in range(10) for j in range(10)]
  637. ```
  638. #### Is the same as:
  639. ```python
  640. out = []
  641. for i in range(10):
  642. for j in range(10):
  643. out.append(i+j)
  644. ```
  645. ### Map, Filter, Reduce
  646. ```python
  647. from functools import reduce
  648. <iter> = map(lambda x: x + 1, range(10)) # (1, 2, ..., 10)
  649. <iter> = filter(lambda x: x > 5, range(10)) # (6, 7, 8, 9)
  650. <int> = reduce(lambda out, x: out + x, range(10)) # 45
  651. ```
  652. ### Any, All
  653. ```python
  654. <bool> = any(<collection>) # False if empty.
  655. <bool> = all(el[1] for el in <collection>) # True if empty.
  656. ```
  657. ### If - Else
  658. ```python
  659. <expression_if_true> if <condition> else <expression_if_false>
  660. ```
  661. ```python
  662. >>> [a if a else 'zero' for a in (0, 1, 0, 3)]
  663. ['zero', 1, 'zero', 3]
  664. ```
  665. ### Namedtuple, Enum, Dataclass
  666. ```python
  667. from collections import namedtuple
  668. Point = namedtuple('Point', 'x y')
  669. point = Point(0, 0)
  670. ```
  671. ```python
  672. from enum import Enum
  673. Direction = Enum('Direction', 'n e s w')
  674. direction = Direction.n
  675. ```
  676. ```python
  677. from dataclasses import make_dataclass
  678. Creature = make_dataclass('Creature', ['location', 'direction'])
  679. creature = Creature(Point(0, 0), Direction.n)
  680. ```
  681. Closure
  682. -------
  683. **We have a closure in Python when:**
  684. * **A nested function references a value of its enclosing function and then**
  685. * **the enclosing function returns the nested function.**
  686. ```python
  687. def get_multiplier(a):
  688. def out(b):
  689. return a * b
  690. return out
  691. ```
  692. ```python
  693. >>> multiply_by_3 = get_multiplier(3)
  694. >>> multiply_by_3(10)
  695. 30
  696. ```
  697. * **If multiple nested functions within enclosing function reference the same value, that value gets shared.**
  698. * **To dynamically access function's first free variable use `'<function>.__closure__[0].cell_contents'`.**
  699. ### Partial
  700. ```python
  701. from functools import partial
  702. <function> = partial(<function> [, <arg_1>, <arg_2>, ...])
  703. ```
  704. ```python
  705. >>> import operator as op
  706. >>> multiply_by_3 = partial(op.mul, 3)
  707. >>> multiply_by_3(10)
  708. 30
  709. ```
  710. * **Partial is also useful in cases when a function needs to be passed as an argument, because it enables us to set its arguments beforehand.**
  711. * **A few examples being `'defaultdict(<function>)'`, `'iter(<function>, to_exclusive)'` and dataclass's `'field(default_factory=<function>)'`.**
  712. ### Nonlocal
  713. **If variable is being assigned to anywhere in the scope, it is regarded as a local variable, unless it is declared as a 'global' or a 'nonlocal'.**
  714. ```python
  715. def get_counter():
  716. i = 0
  717. def out():
  718. nonlocal i
  719. i += 1
  720. return i
  721. return out
  722. ```
  723. ```python
  724. >>> counter = get_counter()
  725. >>> counter(), counter(), counter()
  726. (1, 2, 3)
  727. ```
  728. Decorator
  729. ---------
  730. **A decorator takes a function, adds some functionality and returns it.**
  731. ```python
  732. @decorator_name
  733. def function_that_gets_passed_to_decorator():
  734. ...
  735. ```
  736. ### Debugger Example
  737. **Decorator that prints function's name every time it gets called.**
  738. ```python
  739. from functools import wraps
  740. def debug(func):
  741. @wraps(func)
  742. def out(*args, **kwargs):
  743. print(func.__name__)
  744. return func(*args, **kwargs)
  745. return out
  746. @debug
  747. def add(x, y):
  748. return x + y
  749. ```
  750. * **Wraps is a helper decorator that copies metadata of function add() to function out().**
  751. * **Without it `'add.__name__'` would return `'out'`.**
  752. ### LRU Cache
  753. **Decorator that caches function's return values. All function's arguments must be hashable.**
  754. ```python
  755. from functools import lru_cache
  756. @lru_cache(maxsize=None)
  757. def fib(n):
  758. return n if n < 2 else fib(n-2) + fib(n-1)
  759. ```
  760. * **Recursion depth is limited to 1000 by default. To increase it use `'sys.setrecursionlimit(<depth>)'`.**
  761. ### Parametrized Decorator
  762. **A decorator that accepts arguments and returns a normal decorator that accepts a function.**
  763. ```python
  764. from functools import wraps
  765. def debug(print_result=False):
  766. def decorator(func):
  767. @wraps(func)
  768. def out(*args, **kwargs):
  769. result = func(*args, **kwargs)
  770. print(func.__name__, result if print_result else '')
  771. return result
  772. return out
  773. return decorator
  774. @debug(print_result=True)
  775. def add(x, y):
  776. return x + y
  777. ```
  778. Class
  779. -----
  780. ```python
  781. class <name>:
  782. def __init__(self, a):
  783. self.a = a
  784. def __repr__(self):
  785. class_name = self.__class__.__name__
  786. return f'{class_name}({self.a!r})'
  787. def __str__(self):
  788. return str(self.a)
  789. @classmethod
  790. def get_class_name(cls):
  791. return cls.__name__
  792. ```
  793. * **Return value of repr() should be unambiguous and of str() readable.**
  794. * **If only repr() is defined, it will be also used for str().**
  795. #### Str() use cases:
  796. ```python
  797. print(<el>)
  798. print(f'{<el>}')
  799. raise Exception(<el>)
  800. logging.debug(<el>)
  801. csv.writer(<file>).writerow([<el>])
  802. ```
  803. #### Repr() use cases:
  804. ```python
  805. print([<el>])
  806. print(f'{<el>!r}')
  807. >>> <el>
  808. loguru.logger.exception()
  809. Z = dataclasses.make_dataclass('Z', ['a']); print(Z(<el>))
  810. ```
  811. ### Constructor Overloading
  812. ```python
  813. class <name>:
  814. def __init__(self, a=None):
  815. self.a = a
  816. ```
  817. ### Inheritance
  818. ```python
  819. class Person:
  820. def __init__(self, name, age):
  821. self.name = name
  822. self.age = age
  823. class Employee(Person):
  824. def __init__(self, name, age, staff_num):
  825. super().__init__(name, age)
  826. self.staff_num = staff_num
  827. ```
  828. ### Multiple Inheritance
  829. ```python
  830. class A: pass
  831. class B: pass
  832. class C(A, B): pass
  833. ```
  834. **MRO determines the order in which parent classes are traversed when searching for a method:**
  835. ```python
  836. >>> C.mro()
  837. [<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>]
  838. ```
  839. ### Property
  840. ```python
  841. class MyClass:
  842. @property
  843. def a(self):
  844. return self._a
  845. @a.setter
  846. def a(self, value):
  847. self._a = value
  848. ```
  849. ```python
  850. >>> el = MyClass()
  851. >>> el.a = 123
  852. >>> el.a
  853. 123
  854. ```
  855. ### Dataclass
  856. **Decorator that automatically generates init(), repr() and eq() special methods.**
  857. ```python
  858. from dataclasses import dataclass, field
  859. @dataclass(order=False, frozen=False)
  860. class <class_name>:
  861. <attr_name_1>: <type>
  862. <attr_name_2>: <type> = <default_value>
  863. <attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
  864. ```
  865. * **An object can be made sortable with `'order=True'` or immutable with `'frozen=True'`.**
  866. * **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
  867. * **Default_factory can be any callable.**
  868. ### Slots
  869. **Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.**
  870. ```python
  871. class MyClassWithSlots:
  872. __slots__ = ['a']
  873. def __init__(self):
  874. self.a = 1
  875. ```
  876. ### Copy
  877. ```python
  878. from copy import copy, deepcopy
  879. <object> = copy(<object>)
  880. <object> = deepcopy(<object>)
  881. ```
  882. Duck Types
  883. ----------
  884. **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.**
  885. ### Comparable
  886. * **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`.**
  887. * **That means all objects compare not equal by default.**
  888. * **Only left side object has eq() method called, unless it returns 'NotImplemented', in which case the right object is consulted.**
  889. ```python
  890. class MyComparable:
  891. def __init__(self, a):
  892. self.a = a
  893. def __eq__(self, other):
  894. if isinstance(other, type(self)):
  895. return self.a == other.a
  896. return NotImplemented
  897. ```
  898. ### Hashable
  899. * **Hashable object needs both hash() and eq() methods and its hash value should never change.**
  900. * **Hashable objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do.**
  901. * **That is why Python automatically makes classes unhashable if you only implement eq().**
  902. ```python
  903. class MyHashable:
  904. def __init__(self, a):
  905. self._a = copy.deepcopy(a)
  906. @property
  907. def a(self):
  908. return self._a
  909. def __eq__(self, other):
  910. if isinstance(other, type(self)):
  911. return self.a == other.a
  912. return NotImplemented
  913. def __hash__(self):
  914. return hash(self.a)
  915. ```
  916. ### Sortable
  917. * **With 'total_ordering' decorator you only need to provide eq() and one of lt(), gt(), le() or ge() special methods.**
  918. ```python
  919. from functools import total_ordering
  920. @total_ordering
  921. class MySortable:
  922. def __init__(self, a):
  923. self.a = a
  924. def __eq__(self, other):
  925. if isinstance(other, type(self)):
  926. return self.a == other.a
  927. return NotImplemented
  928. def __lt__(self, other):
  929. if isinstance(other, type(self)):
  930. return self.a < other.a
  931. return NotImplemented
  932. ```
  933. ### Iterator
  934. * **Next() should return next item or raise 'StopIteration'.**
  935. * **Iter() should return 'self'.**
  936. ```python
  937. class Counter:
  938. def __init__(self):
  939. self.i = 0
  940. def __next__(self):
  941. self.i += 1
  942. return self.i
  943. def __iter__(self):
  944. return self
  945. ```
  946. ```python
  947. >>> counter = Counter()
  948. >>> next(counter), next(counter), next(counter)
  949. (1, 2, 3)
  950. ```
  951. ### Callable
  952. ```python
  953. class Counter:
  954. def __init__(self):
  955. self.i = 0
  956. def __call__(self):
  957. self.i += 1
  958. return self.i
  959. ```
  960. ```python
  961. >>> counter = Counter()
  962. >>> counter(), counter(), counter()
  963. (1, 2, 3)
  964. ```
  965. ### Context Manager
  966. ```python
  967. class MyOpen():
  968. def __init__(self, filename):
  969. self.filename = filename
  970. def __enter__(self):
  971. self.file = open(self.filename)
  972. return self.file
  973. def __exit__(self, *args):
  974. self.file.close()
  975. ```
  976. ```python
  977. >>> with open('test.txt', 'w') as file:
  978. ... file.write('Hello World!')
  979. >>> with MyOpen('test.txt') as file:
  980. ... print(file.read())
  981. Hello World!
  982. ```
  983. #### List of existing context managers:
  984. ```python
  985. with open('<path>', ...) as file: ...
  986. with wave.open('<path>', ...) as wave_file: ...
  987. with memoryview(<bytes/bytearray/array>) as view: ...
  988. db = sqlite3.connect('<path>'); with db: db.execute('<insert_query>')
  989. lock = threading.RLock(); with lock: ...
  990. ```
  991. Iterable Duck Types
  992. -------------------
  993. ### Iterable
  994. * **Only required method is iter(). It should return an iterator of object's items.**
  995. * **Contains() automatically works on any object that has iter() defined.**
  996. ```python
  997. class MyIterable:
  998. def __init__(self, a):
  999. self.a = a
  1000. def __iter__(self):
  1001. for el in self.a:
  1002. yield el
  1003. ```
  1004. ```python
  1005. >>> a = MyIterable([1, 2, 3])
  1006. >>> iter(a)
  1007. <generator object MyIterable.__iter__ at 0x1026c18b8>
  1008. >>> 1 in a
  1009. True
  1010. ```
  1011. ### Collection
  1012. * **Only required methods are iter() and len().**
  1013. * **This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
  1014. * **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'.**
  1015. ```python
  1016. class MyCollection:
  1017. def __init__(self, a):
  1018. self.a = a
  1019. def __iter__(self):
  1020. for el in self.a:
  1021. yield el
  1022. def __contains__(self, el):
  1023. return el in self.a
  1024. def __len__(self):
  1025. return len(self.a)
  1026. ```
  1027. ### Sequence
  1028. * **Only required methods are len() and getitem(), that should return an item at index or raise 'IndexError'.**
  1029. * **Iter() and contains() automatically work on any object that has getitem() defined.**
  1030. * **Reversed() automatically works on any object that has getitem() and len() defined.**
  1031. ```python
  1032. class MySequence:
  1033. def __init__(self, a):
  1034. self.a = a
  1035. def __iter__(self):
  1036. for el in self.a:
  1037. yield el
  1038. def __contains__(self, el):
  1039. return el in self.a
  1040. def __len__(self):
  1041. return len(self.a)
  1042. def __getitem__(self, i):
  1043. return self.a[i]
  1044. def __reversed__(self):
  1045. return reversed(self.a)
  1046. ```
  1047. ### Collections.abc.Sequence
  1048. * **It's a richer interface than the basic sequence.**
  1049. * **Extending it generates iter(), contains(), reversed(), index(), and count().**
  1050. * **Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, collections.abc.Sequence)'` would return 'False' even if it had all the methods defined.**
  1051. ```python
  1052. class MyAbcSequence(collections.abc.Sequence):
  1053. def __init__(self, a):
  1054. self.a = a
  1055. def __len__(self):
  1056. return len(self.a)
  1057. def __getitem__(self, i):
  1058. return self.a[i]
  1059. ```
  1060. #### Table of required and available special methods:
  1061. ```text
  1062. +------------+----------+------------+----------+--------------+
  1063. | | Iterable | Collection | Sequence | abc.Sequence |
  1064. +------------+----------+------------+----------+--------------+
  1065. | iter() | REQ | REQ | yes | yes |
  1066. | contains() | yes | yes | yes | yes |
  1067. | len() | | REQ | REQ | REQ |
  1068. | getitem() | | | REQ | REQ |
  1069. | reversed() | | | yes | yes |
  1070. | index() | | | | yes |
  1071. | count() | | | | yes |
  1072. +------------+----------+------------+----------+--------------+
  1073. ```
  1074. * **Other useful ABCs that automatically generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.**
  1075. Enum
  1076. ----
  1077. ```python
  1078. from enum import Enum, auto
  1079. class <enum_name>(Enum):
  1080. <member_name_1> = <value_1>
  1081. <member_name_2> = <value_2_a>, <value_2_b>
  1082. <member_name_3> = auto()
  1083. @classmethod
  1084. def get_member_names(cls):
  1085. return [a.name for a in cls.__members__.values()]
  1086. ```
  1087. * **If there are no numeric values before auto(), it returns 1.**
  1088. * **Otherwise it returns an increment of last numeric value.**
  1089. ```python
  1090. <member> = <enum>.<member_name>
  1091. <member> = <enum>['<member_name>']
  1092. <member> = <enum>(<value>)
  1093. name = <member>.name
  1094. value = <member>.value
  1095. ```
  1096. ```python
  1097. list_of_members = list(<enum>)
  1098. member_names = [a.name for a in <enum>]
  1099. member_values = [a.value for a in <enum>]
  1100. random_member = random.choice(list(<enum>))
  1101. ```
  1102. ### Inline
  1103. ```python
  1104. Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon'])
  1105. Cutlery = Enum('Cutlery', 'fork knife spoon')
  1106. Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
  1107. ```
  1108. #### Functions can not be values, so they must be wrapped:
  1109. ```python
  1110. from functools import partial
  1111. LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
  1112. 'OR' : partial(lambda l, r: l or r)})
  1113. ```
  1114. * **Another solution in this particular case, is to use `'and_'` and `'or_'` functions from module [Operator](#operator).**
  1115. Exceptions
  1116. ----------
  1117. ### Basic Example
  1118. ```python
  1119. try:
  1120. <code>
  1121. except <exception>:
  1122. <code>
  1123. ```
  1124. ### Complex Example
  1125. ```python
  1126. try:
  1127. <code_1>
  1128. except <exception_a>:
  1129. <code_2_a>
  1130. except <exception_b>:
  1131. <code_2_b>
  1132. else:
  1133. <code_2_c>
  1134. finally:
  1135. <code_3>
  1136. ```
  1137. ### Catching Exceptions
  1138. ```python
  1139. except <exception>:
  1140. except <exception> as <name>:
  1141. except (<exception_1>, <exception_2>, ...):
  1142. except (<exception_1>, <exception_2>, ...) as <name>:
  1143. ```
  1144. * **Also catches subclasses of the exception.**
  1145. ### Raising Exceptions
  1146. ```python
  1147. raise <exception>
  1148. raise <exception>()
  1149. raise <exception>(<el>)
  1150. raise <exception>(<el_1>, <el_2>, ...)
  1151. ```
  1152. #### Useful built-in exceptions:
  1153. ```python
  1154. raise ValueError('Argument is of right type but inappropriate value!')
  1155. raise TypeError('Argument is of wrong type!')
  1156. raise RuntimeError('None of above!')
  1157. ```
  1158. #### Re-raising caught exception:
  1159. ```python
  1160. except <exception>:
  1161. <code>
  1162. raise
  1163. ```
  1164. ### Common Built-in Exceptions
  1165. ```text
  1166. BaseException
  1167. +-- SystemExit # Raised by the sys.exit() function.
  1168. +-- KeyboardInterrupt # Raised when the user hits the interrupt key.
  1169. +-- Exception # User-defined exceptions should be derived from this class.
  1170. +-- StopIteration # Raised by next() when run on an empty iterator.
  1171. +-- ArithmeticError # Base class for arithmetic errors.
  1172. | +-- ZeroDivisionError # Raised when dividing by zero.
  1173. +-- AttributeError # Raised when an attribute is missing.
  1174. +-- EOFError # Raised by input() when it hits end-of-file condition.
  1175. +-- LookupError # Raised when a look-up on sequence or dict fails.
  1176. | +-- IndexError # Raised when a sequence index is out of range.
  1177. | +-- KeyError # Raised when a dictionary key is not found.
  1178. +-- NameError # Raised when a variable name is not found.
  1179. +-- OSError # Failures such as “file not found” or “disk full”.
  1180. | +-- FileNotFoundError # When a file or directory is requested but doesn't exist.
  1181. +-- RuntimeError # Raised by errors that don't fall in other categories.
  1182. | +-- RecursionError # Raised when the the maximum recursion depth is exceeded.
  1183. +-- TypeError # Raised when an argument is of wrong type.
  1184. +-- ValueError # When an argument is of right type but inappropriate value.
  1185. +-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
  1186. ```
  1187. ### User-defined Exceptions
  1188. ```python
  1189. class MyError(Exception):
  1190. pass
  1191. class MyInputError(MyError):
  1192. pass
  1193. ```
  1194. Print
  1195. -----
  1196. ```python
  1197. print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  1198. ```
  1199. * **Use `'file=sys.stderr'` for errors.**
  1200. * **Use `'flush=True'` to forcibly flush the stream.**
  1201. ### Pretty Print
  1202. ```python
  1203. >>> from pprint import pprint
  1204. >>> pprint(dir())
  1205. ['__annotations__',
  1206. '__builtins__',
  1207. '__doc__', ...]
  1208. ```
  1209. Input
  1210. -----
  1211. * **Reads a line from user input or pipe if present.**
  1212. * **Trailing newline gets stripped.**
  1213. * **Prompt string is printed to the standard output before reading input.**
  1214. ```python
  1215. <str> = input(prompt=None)
  1216. ```
  1217. #### Prints lines until EOF:
  1218. ```python
  1219. while True:
  1220. try:
  1221. print(input())
  1222. except EOFError:
  1223. break
  1224. ```
  1225. Command Line Arguments
  1226. ----------------------
  1227. ```python
  1228. import sys
  1229. script_name = sys.argv[0]
  1230. arguments = sys.argv[1:]
  1231. ```
  1232. ### Argparse
  1233. ```python
  1234. from argparse import ArgumentParser, FileType
  1235. p = ArgumentParser(description=<str>)
  1236. p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag
  1237. p.add_argument('-<short_name>', '--<name>', type=<type>) # Option
  1238. p.add_argument('<name>', type=<type>, nargs=1) # Argument
  1239. p.add_argument('<name>', type=<type>, nargs='+') # Arguments
  1240. args = p.parse_args()
  1241. value = args.<name>
  1242. ```
  1243. * **Use `'help=<str>'` for argument description.**
  1244. * **Use `'type=FileType(<mode>)'` for files.**
  1245. Open
  1246. ----
  1247. **Opens a file and returns a corresponding file object.**
  1248. ```python
  1249. <file> = open('<path>', mode='r', encoding=None, newline=None)
  1250. ```
  1251. * **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
  1252. * **`'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.**
  1253. * **`'newline=""'` means no conversions take place, but lines are still broken by readline() on either '\n', '\r' or '\r\n'.**
  1254. ### Modes
  1255. * **`'r'` - Read (default).**
  1256. * **`'w'` - Write (truncate).**
  1257. * **`'x'` - Write or fail if the file already exists.**
  1258. * **`'a'` - Append.**
  1259. * **`'w+'` - Read and write (truncate).**
  1260. * **`'r+'` - Read and write from the start.**
  1261. * **`'a+'` - Read and write from the end.**
  1262. * **`'t'` - Text mode (default).**
  1263. * **`'b'` - Binary mode.**
  1264. ### Exceptions
  1265. * **`'FileNotFoundError'` can be risen when reading with `'r'` or `'r+'`.**
  1266. * **`'FileExistsError'` can be risen when writing with `'x'`.**
  1267. * **`'IsADirectoryError'` and `'PermissionError'` can be risen by any.**
  1268. * **`'OSError'` is the parent class of all listed exceptions.**
  1269. ### File
  1270. ```python
  1271. <file>.seek(0) # Moves to the start of the file.
  1272. <file>.seek(offset) # Moves 'offset' chars/bytes from the start.
  1273. <file>.seek(0, 2) # Moves to the end of the file.
  1274. <bin_file>.seek(±offset, <anchor>) # Anchor: 0 start, 1 current pos., 2 end.
  1275. ```
  1276. ```python
  1277. <str/bytes> = <file>.read(size=-1) # Reads 'size' chars/bytes or until EOF.
  1278. <str/bytes> = <file>.readline() # Returns a line or empty string on EOF.
  1279. <list> = <file>.readlines() # Returns a list of lines or empty list.
  1280. <str/bytes> = next(<file>) # Returns a line using buffer. Do not mix.
  1281. ```
  1282. ```python
  1283. <file>.write(<str/bytes>) # Writes a string or bytes object.
  1284. <file>.writelines(<coll.>) # Writes a coll. of strings or bytes objects.
  1285. <file>.flush() # Flushes write buffer.
  1286. ```
  1287. * **Methods do not add or strip trailing newlines, even writelines().**
  1288. ### Read Text from File
  1289. ```python
  1290. def read_file(filename):
  1291. with open(filename, encoding='utf-8') as file:
  1292. return file.readlines()
  1293. ```
  1294. ### Write Text to File
  1295. ```python
  1296. def write_to_file(filename, text):
  1297. with open(filename, 'w', encoding='utf-8') as file:
  1298. file.write(text)
  1299. ```
  1300. Path
  1301. ----
  1302. ```python
  1303. from os import path, listdir
  1304. from glob import glob
  1305. ```
  1306. ```python
  1307. <bool> = path.exists('<path>')
  1308. <bool> = path.isfile('<path>')
  1309. <bool> = path.isdir('<path>')
  1310. ```
  1311. ```python
  1312. <list> = listdir('<path>') # List of filenames located at 'path'.
  1313. <list> = glob('<pattern>') # Filenames matching the wildcard pattern.
  1314. ```
  1315. ### Pathlib
  1316. ```python
  1317. from pathlib import Path
  1318. ```
  1319. ```python
  1320. cwd = Path()
  1321. <Path> = Path('<path>' [, '<path>', <Path>, ...])
  1322. <Path> = <Path> / '<dir>' / '<file>'
  1323. ```
  1324. ```python
  1325. <bool> = <Path>.exists()
  1326. <bool> = <Path>.is_file()
  1327. <bool> = <Path>.is_dir()
  1328. ```
  1329. ```python
  1330. <iter> = <Path>.iterdir() # Iterator of filenames located at path.
  1331. <iter> = <Path>.glob('<pattern>') # Filenames matching the wildcard pattern.
  1332. ```
  1333. ```python
  1334. <str> = str(<Path>) # Returns path as a string.
  1335. <tup.> = <Path>.parts # Returns all components as strings.
  1336. <Path> = <Path>.resolve() # Returns absolute Path without symlinks.
  1337. ```
  1338. ```python
  1339. <str> = <Path>.name # Final component.
  1340. <str> = <Path>.stem # Final component without extension.
  1341. <str> = <Path>.suffix # Final component's extension.
  1342. <Path> = <Path>.parent # Path without final component.
  1343. ```
  1344. Command Execution
  1345. -----------------
  1346. ### Files and Directories Commands
  1347. * **Paths can be either strings or Path objects.**
  1348. * **All exceptions are either 'OSError' or its subclasses.**
  1349. ```python
  1350. import os
  1351. os.chdir(<path>) # Changes the current working directory.
  1352. <str> = os.getcwd() # Returns current working directory.
  1353. ```
  1354. ```python
  1355. os.remove(<path>) # Deletes the file.
  1356. os.rmdir(<path>) # Deletes empty directory.
  1357. shutil.rmtree(<path>) # Deletes an entire directory tree.
  1358. ```
  1359. ```python
  1360. os.rename(from, to) # Renames the file or directory.
  1361. os.replace(from, to) # Same, but overwrites 'to' if it exists.
  1362. ```
  1363. ```python
  1364. os.mkdir(<path>, mode=0o777) # Creates a directory.
  1365. <iter> = os.scandir(path='.') # Returns os.DirEntry objects located at path.
  1366. ```
  1367. #### DirEntry:
  1368. ```python
  1369. <str> = <DirEntry>.name # Final component of the path.
  1370. <str> = <DirEntry>.path # Path with final component.
  1371. ```
  1372. ```python
  1373. <bool> = <DirEntry>.is_file()
  1374. <bool> = <DirEntry>.is_dir()
  1375. <bool> = <DirEntry>.is_symlink()
  1376. <Path> = Path(<DirEntry>)
  1377. ```
  1378. ### Shell Commands
  1379. ```python
  1380. import os
  1381. <str> = os.popen('<shell_command>').read()
  1382. ```
  1383. #### Using subprocess:
  1384. ```python
  1385. >>> import subprocess, shlex
  1386. >>> a = subprocess.run(shlex.split('ls -a'), stdout=subprocess.PIPE)
  1387. >>> a.stdout
  1388. b'.\n..\nfile1.txt\nfile2.txt\n'
  1389. >>> a.returncode
  1390. 0
  1391. ```
  1392. CSV
  1393. ---
  1394. ```python
  1395. import csv
  1396. <reader> = csv.reader(<file>, dialect='excel', delimiter=',', ...)
  1397. <list> = next(<reader>) # Returns a row as list of strings.
  1398. ```
  1399. ```python
  1400. <writer> = csv.writer(<file>, dialect='excel', delimiter=',', ...)
  1401. <writer>.writerow(<collection>)
  1402. <writer>.writerows(<coll_of_coll>)
  1403. ```
  1404. ### Parameters
  1405. * **`'dialect'` - Master parameter that sets the default values.**
  1406. * **`'delimiter'` - A one-character string used to separate fields.**
  1407. * **`'quotechar'` - Character for quoting fields that contain special characters.**
  1408. * **`'doublequote'` - Whether quotechars inside fields get doubled or escaped.**
  1409. * **`'skipinitialspace'` - Whether whitespace after delimiter gets stripped.**
  1410. * **`'lineterminator'` - How does writer terminate lines.**
  1411. * **`'quoting'` - Controls the amount of quoting: 0 - as necessary, 1 - all.**
  1412. * **`'escapechar'` - Character for escaping quotechar if doublequote is false.**
  1413. ### Dialects
  1414. ```text
  1415. +------------------+-----------+-----------+--------------+
  1416. | | excel | excel_tab | unix_dialect |
  1417. +------------------+-----------+-----------+--------------+
  1418. | delimiter | ',' | '\t' | ',' |
  1419. | quotechar | '"' | '"' | '"' |
  1420. | doublequote | True | True | True |
  1421. | skipinitialspace | False | False | False |
  1422. | lineterminator | '\r\n' | '\r\n' | '\n' |
  1423. | quoting | 0 | 0 | 1 |
  1424. | escapechar | None | None | None |
  1425. +------------------+-----------+-----------+--------------+
  1426. ```
  1427. ### Read Rows from CSV File
  1428. ```python
  1429. def read_csv_file(filename):
  1430. with open(filename, encoding='utf-8', newline='') as file:
  1431. return csv.reader(file)
  1432. ```
  1433. * **If `'newline=""'` is not specified, then newlines embedded inside quoted fields will not be interpreted correctly.**
  1434. ### Write Rows to CSV File
  1435. ```python
  1436. def write_to_csv_file(filename, rows):
  1437. with open(filename, 'w', encoding='utf-8', newline='') as file:
  1438. writer = csv.writer(file)
  1439. writer.writerows(rows)
  1440. ```
  1441. JSON
  1442. ----
  1443. ```python
  1444. import json
  1445. <str> = json.dumps(<object>, ensure_ascii=True, indent=None)
  1446. <object> = json.loads(<str>)
  1447. ```
  1448. ### Read Object from JSON File
  1449. ```python
  1450. def read_json_file(filename):
  1451. with open(filename, encoding='utf-8') as file:
  1452. return json.load(file)
  1453. ```
  1454. ### Write Object to JSON File
  1455. ```python
  1456. def write_to_json_file(filename, an_object):
  1457. with open(filename, 'w', encoding='utf-8') as file:
  1458. json.dump(an_object, file, ensure_ascii=False, indent=2)
  1459. ```
  1460. Pickle
  1461. ------
  1462. ```python
  1463. import pickle
  1464. <bytes> = pickle.dumps(<object>)
  1465. <object> = pickle.loads(<bytes>)
  1466. ```
  1467. ### Read Object from File
  1468. ```python
  1469. def read_pickle_file(filename):
  1470. with open(filename, 'rb') as file:
  1471. return pickle.load(file)
  1472. ```
  1473. ### Write Object to File
  1474. ```python
  1475. def write_to_pickle_file(filename, an_object):
  1476. with open(filename, 'wb') as file:
  1477. pickle.dump(an_object, file)
  1478. ```
  1479. SQLite
  1480. ------
  1481. **Server-less database engine that stores each database into separate file.**
  1482. ```python
  1483. import sqlite3
  1484. db = sqlite3.connect('<path>') # Also ':memory:'.
  1485. ...
  1486. db.close()
  1487. ```
  1488. * **New database will be created if path doesn't exist.**
  1489. ### Read
  1490. ```python
  1491. cursor = db.execute('<query>')
  1492. if cursor:
  1493. <tuple> = cursor.fetchone() # First row.
  1494. <list> = cursor.fetchall() # Remaining rows.
  1495. ```
  1496. * **Returned values can be of type str, int, float, bytes or None.**
  1497. ### Write
  1498. ```python
  1499. db.execute('<query>')
  1500. db.commit()
  1501. ```
  1502. ### Placeholders
  1503. ```python
  1504. db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
  1505. db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
  1506. db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
  1507. ```
  1508. * **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
  1509. ### MySQL
  1510. **Has a very similar interface, with differences listed below.**
  1511. ```python
  1512. # $ pip3 install mysql-connector
  1513. from mysql import connector
  1514. db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
  1515. cursor = db.cursor()
  1516. cursor.execute('<query>') # Connector doesn't have execute method.
  1517. cursor.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
  1518. cursor.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.
  1519. ```
  1520. Bytes
  1521. -----
  1522. **Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.**
  1523. ```python
  1524. <bytes> = b'<str>' # Only accepts ASCII characters and \x00 - \xff.
  1525. <int> = <bytes>[<index>] # Returns int in range from 0 to 255.
  1526. <bytes> = <bytes>[<slice>] # Returns bytes even if it has only one element.
  1527. <bytes> = <bytes>.join(<coll_of_bytes>) # Joins elements using bytes object as separator.
  1528. ```
  1529. ### Encode
  1530. ```python
  1531. <bytes> = <str>.encode('utf-8') # Or: bytes(<str>, 'utf-8')
  1532. <bytes> = bytes(<coll_of_ints>) # Ints must be in range from 0 to 255.
  1533. <bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
  1534. <bytes> = bytes.fromhex('<hex>')
  1535. ```
  1536. ### Decode
  1537. ```python
  1538. <str> = <bytes>.decode('utf-8') # Or: str(<bytes>, 'utf-8')
  1539. <list> = list(<bytes>) # Returns ints in range from 0 to 255.
  1540. <int> = int.from_bytes(<bytes>, byteorder='big|little', signed=False)
  1541. '<hex>' = <bytes>.hex()
  1542. ```
  1543. ### Read Bytes from File
  1544. ```python
  1545. def read_bytes(filename):
  1546. with open(filename, 'rb') as file:
  1547. return file.read()
  1548. ```
  1549. ### Write Bytes to File
  1550. ```python
  1551. def write_bytes(filename, bytes_obj):
  1552. with open(filename, 'wb') as file:
  1553. file.write(bytes_obj)
  1554. ```
  1555. Struct
  1556. ------
  1557. * **Module that performs conversions between a sequence of numbers and a C struct, represented as a Python bytes object.**
  1558. * **Machine’s native type sizes and byte order are used by default.**
  1559. ```python
  1560. from struct import pack, unpack, iter_unpack, calcsize
  1561. <bytes> = pack('<format>', <num_1> [, <num_2>, ...])
  1562. <tuple> = unpack('<format>', <bytes>)
  1563. <tuples> = iter_unpack('<format>', <bytes>)
  1564. ```
  1565. ### Example
  1566. ```python
  1567. >>> pack('>hhl', 1, 2, 3)
  1568. b'\x00\x01\x00\x02\x00\x00\x00\x03'
  1569. >>> unpack('>hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
  1570. (1, 2, 3)
  1571. >>> calcsize('>hhl')
  1572. 8
  1573. ```
  1574. ### Format
  1575. #### For standard sizes start format string with:
  1576. * **`'='` - native byte order**
  1577. * **`'<'` - little-endian**
  1578. * **`'>'` - big-endian**
  1579. #### Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:
  1580. * **`'x'` - pad byte**
  1581. * **`'b'` - char (1)**
  1582. * **`'h'` - short (2)**
  1583. * **`'i'` - int (4)**
  1584. * **`'l'` - long (4)**
  1585. * **`'q'` - long long (8)**
  1586. #### Floating point types:
  1587. * **`'f'` - float (4)**
  1588. * **`'d'` - double (8)**
  1589. Array
  1590. -----
  1591. **List that can only hold numbers of predefined type. Available types and their sizes in bytes are listed above.**
  1592. ```python
  1593. from array import array
  1594. <array> = array('<typecode>' [, <collection>])
  1595. ```
  1596. Memory View
  1597. -----------
  1598. **Used for accessing the internal data of an object that supports the buffer protocol.**
  1599. ```python
  1600. <memoryview> = memoryview(<bytes> / <bytearray> / <array>)
  1601. <memoryview>.release()
  1602. ```
  1603. Deque
  1604. -----
  1605. **A thread-safe list with efficient appends and pops from either side. Pronounced "deck".**
  1606. ```python
  1607. from collections import deque
  1608. <deque> = deque(<collection>, maxlen=None)
  1609. ```
  1610. ```python
  1611. <deque>.appendleft(<el>)
  1612. <el> = <deque>.popleft()
  1613. <deque>.extendleft(<collection>) # Collection gets reversed.
  1614. <deque>.rotate(n=1) # Rotates elements to the right.
  1615. ```
  1616. ```python
  1617. >>> a = deque([1, 2, 3], maxlen=3)
  1618. >>> a.append(4)
  1619. [2, 3, 4]
  1620. >>> a.appendleft(5)
  1621. [5, 2, 3]
  1622. >>> a.insert(1, 6)
  1623. IndexError: deque already at its maximum size
  1624. ```
  1625. Threading
  1626. ---------
  1627. ```python
  1628. from threading import Thread, RLock
  1629. ```
  1630. ### Thread
  1631. ```python
  1632. thread = Thread(target=<function>, args=(<first_arg>, ))
  1633. thread.start()
  1634. ...
  1635. thread.join()
  1636. ```
  1637. ### Lock
  1638. ```python
  1639. lock = RLock()
  1640. lock.acquire()
  1641. ...
  1642. lock.release()
  1643. ```
  1644. #### Or:
  1645. ```python
  1646. lock = RLock()
  1647. with lock:
  1648. ...
  1649. ```
  1650. Introspection
  1651. -------------
  1652. **Inspecting code at runtime.**
  1653. ### Variables
  1654. ```python
  1655. <list> = dir() # Names of variables in current scope.
  1656. <dict> = locals() # Dict of local variables. Also vars().
  1657. <dict> = globals() # Dict of global variables.
  1658. ```
  1659. ### Attributes
  1660. ```python
  1661. <dict> = vars(<object>)
  1662. <bool> = hasattr(<object>, '<attr_name>')
  1663. value = getattr(<object>, '<attr_name>')
  1664. setattr(<object>, '<attr_name>', value)
  1665. ```
  1666. ### Parameters
  1667. ```python
  1668. from inspect import signature
  1669. <sig> = signature(<function>)
  1670. no_of_params = len(<sig>.parameters)
  1671. param_names = list(<sig>.parameters.keys())
  1672. ```
  1673. Metaprograming
  1674. --------------
  1675. **Code that generates code.**
  1676. ### Type
  1677. **Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.**
  1678. ```python
  1679. <class> = type(<class_name>, <parents_tuple>, <attributes_dict>)
  1680. ```
  1681. ```python
  1682. >>> Z = type('Z', (), {'a': 'abcde', 'b': 12345})
  1683. >>> z = Z()
  1684. ```
  1685. ### Meta Class
  1686. **Class that creates class.**
  1687. ```python
  1688. def my_meta_class(name, parents, attrs):
  1689. attrs['a'] = 'abcde'
  1690. return type(name, parents, attrs)
  1691. ```
  1692. #### Or:
  1693. ```python
  1694. class MyMetaClass(type):
  1695. def __new__(cls, name, parents, attrs):
  1696. attrs['a'] = 'abcde'
  1697. return type.__new__(cls, name, parents, attrs)
  1698. ```
  1699. * **New() is a class method that gets called before init(). If it returns an instance of its class, then that instance gets passed to init() as a 'self' argument.**
  1700. * **It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance (**`'MyMetaClass'` **in our case).**
  1701. * **New() can also be called directly, usually from a new() method of a child class (**`def __new__(cls): return super().__new__(cls)`**), in which case init() is not called.**
  1702. ### Metaclass Attribute
  1703. **Right before a class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
  1704. ```python
  1705. class MyClass(metaclass=MyMetaClass):
  1706. b = 12345
  1707. ```
  1708. ```python
  1709. >>> MyClass.a, MyClass.b
  1710. ('abcde', 12345)
  1711. ```
  1712. ### Type Diagram
  1713. ```python
  1714. type(MyClass) == MyMetaClass # MyClass is an instance of MyMetaClass.
  1715. type(MyMetaClass) == type # MyMetaClass is an instance of type.
  1716. ```
  1717. ```text
  1718. +---------+-------------+
  1719. | Classes | Metaclasses |
  1720. +---------+-------------|
  1721. | MyClass > MyMetaClass |
  1722. | | v |
  1723. | object ---> type <+ |
  1724. | | ^ +---+ |
  1725. | str -------+ |
  1726. +---------+-------------+
  1727. ```
  1728. ### Inheritance Diagram
  1729. ```python
  1730. MyClass.__base__ == object # MyClass is a subclass of object.
  1731. MyMetaClass.__base__ == type # MyMetaClass is a subclass of type.
  1732. ```
  1733. ```text
  1734. +---------+-------------+
  1735. | Classes | Metaclasses |
  1736. +---------+-------------|
  1737. | MyClass | MyMetaClass |
  1738. | v | v |
  1739. | object <--- type |
  1740. | ^ | |
  1741. | str | |
  1742. +---------+-------------+
  1743. ```
  1744. Operator
  1745. --------
  1746. ```python
  1747. from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
  1748. from operator import eq, ne, lt, le, gt, ge
  1749. from operator import and_, or_, not_
  1750. from operator import itemgetter, attrgetter, methodcaller
  1751. ```
  1752. ```python
  1753. import operator as op
  1754. sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
  1755. sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
  1756. product_of_elems = functools.reduce(op.mul, <collection>)
  1757. LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_})
  1758. last_el = op.methodcaller('pop')(<list>)
  1759. ```
  1760. Eval
  1761. ----
  1762. ```python
  1763. >>> from ast import literal_eval
  1764. >>> literal_eval('1 + 2')
  1765. 3
  1766. >>> literal_eval('[1, 2, 3]')
  1767. [1, 2, 3]
  1768. >>> literal_eval('abs(1)')
  1769. ValueError: malformed node or string
  1770. ```
  1771. Coroutine
  1772. ---------
  1773. * **Similar to generator, but generator pulls data through the pipe with iteration, while coroutine pushes data into the pipeline with send().**
  1774. * **Coroutines provide more powerful data routing possibilities than iterators.**
  1775. * **If you build a collection of simple data processing components, you can glue them together into complex arrangements of pipes, branches, merging, etc.**
  1776. ### Helper Decorator
  1777. * **All coroutines must be "primed" by first calling next().**
  1778. * **Remembering to call next() is easy to forget.**
  1779. * **Solved by wrapping coroutines with a decorator:**
  1780. ```python
  1781. def coroutine(func):
  1782. def out(*args, **kwargs):
  1783. cr = func(*args, **kwargs)
  1784. next(cr)
  1785. return cr
  1786. return out
  1787. ```
  1788. ### Pipeline Example
  1789. ```python
  1790. def reader(target):
  1791. for i in range(10):
  1792. target.send(i)
  1793. target.close()
  1794. @coroutine
  1795. def adder(target):
  1796. while True:
  1797. value = (yield)
  1798. target.send(value + 100)
  1799. @coroutine
  1800. def printer():
  1801. while True:
  1802. value = (yield)
  1803. print(value)
  1804. reader(adder(printer())) # 100, 101, ..., 109
  1805. ```
  1806. <br><br>
  1807. Libraries
  1808. =========
  1809. Progress Bar
  1810. ------------
  1811. ```python
  1812. # $ pip3 install tqdm
  1813. from tqdm import tqdm
  1814. from time import sleep
  1815. for i in tqdm([1, 2, 3]):
  1816. sleep(0.2)
  1817. for i in tqdm(range(100)):
  1818. sleep(0.02)
  1819. ```
  1820. Plot
  1821. ----
  1822. ```python
  1823. # $ pip3 install matplotlib
  1824. from matplotlib import pyplot
  1825. pyplot.plot(<data_1> [, <data_2>, ...]) # Or: hist(<data>).
  1826. pyplot.savefig(<filename>)
  1827. pyplot.show()
  1828. pyplot.clf() # Clears figure.
  1829. ```
  1830. Table
  1831. -----
  1832. #### Prints a CSV file as an ASCII table:
  1833. ```python
  1834. # $ pip3 install tabulate
  1835. from tabulate import tabulate
  1836. import csv
  1837. with open(<filename>, encoding='utf-8', newline='') as file:
  1838. lines = csv.reader(file)
  1839. headers = [header.title() for header in next(lines)]
  1840. table = tabulate(lines, headers)
  1841. print(table)
  1842. ```
  1843. Curses
  1844. ------
  1845. ```python
  1846. from curses import wrapper, ascii
  1847. def main():
  1848. wrapper(draw)
  1849. def draw(screen):
  1850. screen.clear()
  1851. screen.addstr(0, 0, 'Press ESC to quit.')
  1852. while screen.getch() != ascii.ESC:
  1853. pass
  1854. def get_border(screen):
  1855. from collections import namedtuple
  1856. P = namedtuple('P', 'y x')
  1857. height, width = screen.getmaxyx()
  1858. return P(height-1, width-1)
  1859. if __name__ == '__main__':
  1860. main()
  1861. ```
  1862. Logging
  1863. -------
  1864. ```python
  1865. # $ pip3 install loguru
  1866. from loguru import logger
  1867. ```
  1868. ```python
  1869. logger.add('debug_{time}.log', colorize=True) # Connects a log file.
  1870. logger.add('error_{time}.log', level='ERROR') # Another file for errors or higher.
  1871. logger.<level>('A logging message.')
  1872. ```
  1873. * **Levels: `'debug'`, `'info'`, `'success'`, `'warning'`, `'error'`, `'critical'`.**
  1874. ### Exceptions
  1875. **Error description, stack trace and values of variables are appended automatically.**
  1876. ```python
  1877. try:
  1878. ...
  1879. except <exception>:
  1880. logger.exception('An error happened.')
  1881. ```
  1882. ### Rotation
  1883. **Argument that sets a condition when a new log file is created.**
  1884. ```python
  1885. rotation=<int>|<datetime.timedelta>|<datetime.time>|<str>
  1886. ```
  1887. * **`'<int>'` - Max file size in bytes.**
  1888. * **`'<timedelta>'` - Max age of a file.**
  1889. * **`'<time>'` - Time of day.**
  1890. * **`'<str>'` - Any of above as a string: `'100 MB'`, `'1 month'`, `'monday at 12:00'`, ...**
  1891. ### Retention
  1892. **Sets a condition which old log files are deleted.**
  1893. ```python
  1894. retention=<int>|<datetime.timedelta>|<str>
  1895. ```
  1896. * **`'<int>'` - Max number of files.**
  1897. * **`'<timedelta>'` - Max age of a file.**
  1898. * **`'<str>'` - Max age as a string: `'1 week, 3 days'`, `'2 months'`, ...**
  1899. Scraping
  1900. --------
  1901. #### Scrapes and prints Python's URL and version number from Wikipedia:
  1902. ```python
  1903. # $ pip3 install requests beautifulsoup4
  1904. import requests
  1905. from bs4 import BeautifulSoup
  1906. url = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
  1907. page = requests.get(url)
  1908. doc = BeautifulSoup(page.text, 'html.parser')
  1909. table = doc.find('table', class_='infobox vevent')
  1910. rows = table.find_all('tr')
  1911. link = rows[11].find('a')['href']
  1912. ver = rows[6].find('div').text.split()[0]
  1913. print(link, ver)
  1914. ```
  1915. Web
  1916. ---
  1917. ```python
  1918. # $ pip3 install bottle
  1919. from bottle import run, route, post, template, request, response
  1920. import json
  1921. ```
  1922. ### Run
  1923. ```python
  1924. run(host='localhost', port=8080)
  1925. run(host='0.0.0.0', port=80, server='cherrypy')
  1926. ```
  1927. ### Static Request
  1928. ```python
  1929. @route('/img/<image>')
  1930. def send_image(image):
  1931. return static_file(image, 'images/', mimetype='image/png')
  1932. ```
  1933. ### Dynamic Request
  1934. ```python
  1935. @route('/<sport>')
  1936. def send_page(sport):
  1937. return template('<h1>{{title}}</h1>', title=sport)
  1938. ```
  1939. ### REST Request
  1940. ```python
  1941. @post('/odds/<sport>')
  1942. def odds_handler(sport):
  1943. team = request.forms.get('team')
  1944. home_odds, away_odds = 2.44, 3.29
  1945. response.headers['Content-Type'] = 'application/json'
  1946. response.headers['Cache-Control'] = 'no-cache'
  1947. return json.dumps([team, home_odds, away_odds])
  1948. ```
  1949. #### Test:
  1950. ```python
  1951. # $ pip3 install requests
  1952. >>> import requests
  1953. >>> url = 'http://localhost:8080/odds/football'
  1954. >>> data = {'team': 'arsenal f.c.'}
  1955. >>> response = requests.post(url, data=data)
  1956. >>> response.json()
  1957. ['arsenal f.c.', 2.44, 3.29]
  1958. ```
  1959. Profile
  1960. -------
  1961. ### Basic
  1962. ```python
  1963. from time import time
  1964. start_time = time() # Seconds since Epoch.
  1965. ...
  1966. duration = time() - start_time
  1967. ```
  1968. ### High Performance
  1969. ```python
  1970. from time import perf_counter as pc
  1971. start_time = pc() # Seconds since restart.
  1972. ...
  1973. duration = pc() - start_time
  1974. ```
  1975. ### Timing a Snippet
  1976. ```python
  1977. >>> from timeit import timeit
  1978. >>> timeit('"-".join(str(a) for a in range(100))',
  1979. ... number=10000, globals=globals(), setup='pass')
  1980. 0.34986
  1981. ```
  1982. ### Line Profiler
  1983. ```python
  1984. # $ pip3 install line_profiler
  1985. @profile
  1986. def main():
  1987. a = [*range(10000)]
  1988. b = {*range(10000)}
  1989. main()
  1990. ```
  1991. #### Usage:
  1992. ```text
  1993. $ kernprof -lv test.py
  1994. Line # Hits Time Per Hit % Time Line Contents
  1995. ==============================================================
  1996. 1 @profile
  1997. 2 def main():
  1998. 3 1 1128.0 1128.0 27.4 a = [*range(10000)]
  1999. 4 1 2994.0 2994.0 72.6 b = {*range(10000)}
  2000. ```
  2001. ### Call Graph
  2002. #### Generates a PNG image of a call graph with highlighted bottlenecks:
  2003. ```python
  2004. # $ pip3 install pycallgraph
  2005. from pycallgraph import output, PyCallGraph
  2006. from datetime import datetime
  2007. time_str = datetime.now().strftime('%Y%m%d%H%M%S')
  2008. filename = f'profile-{time_str}.png'
  2009. drawer = output.GraphvizOutput(output_file=filename)
  2010. with PyCallGraph(drawer):
  2011. <code_to_be_profiled>
  2012. ```
  2013. NumPy
  2014. -----
  2015. **Array manipulation mini language. Can run up to one hundred times faster than equivalent Python code.**
  2016. ```python
  2017. # $ pip3 install numpy
  2018. import numpy as np
  2019. ```
  2020. ```python
  2021. <array> = np.array(<list>)
  2022. <array> = np.arange(from_inclusive, to_exclusive, ±step_size)
  2023. <array> = np.ones(<shape>)
  2024. <array> = np.random.randint(from_inclusive, to_exclusive, <shape>)
  2025. ```
  2026. ```python
  2027. <array>.shape = <shape>
  2028. <view> = <array>.reshape(<shape>)
  2029. <view> = np.broadcast_to(<array>, <shape>)
  2030. ```
  2031. ```python
  2032. <array> = <array>.sum(axis)
  2033. indexes = <array>.argmin(axis)
  2034. ```
  2035. * **Shape is a tuple of dimension sizes.**
  2036. * **Axis is an index of dimension that gets collapsed. Leftmost dimension has index 0.**
  2037. ### Indexing
  2038. ```bash
  2039. <el> = <2d_array>[0, 0] # First element.
  2040. <1d_view> = <2d_array>[0] # First row.
  2041. <1d_view> = <2d_array>[:, 0] # First column. Also [..., 0].
  2042. <3d_view> = <2d_array>[None, :, :] # Expanded by dimension of size 1.
  2043. ```
  2044. ```bash
  2045. <1d_array> = <2d_array>[<1d_row_indexes>, <1d_column_indexes>]
  2046. <2d_array> = <2d_array>[<2d_row_indexes>, <2d_column_indexes>]
  2047. ```
  2048. ```bash
  2049. <2d_bools> = <2d_array> > 0
  2050. <1d_array> = <2d_array>[<2d_bools>]
  2051. ```
  2052. * **If row and column indexes differ in shape, they are combined with broadcasting.**
  2053. ### Broadcasting
  2054. **Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.**
  2055. ```python
  2056. left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
  2057. right = [ 0.1 , 0.6 , 0.8 ] # Shape: (3)
  2058. ```
  2059. #### 1. If array shapes differ in length, left-pad the shorter shape with ones:
  2060. ```python
  2061. left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
  2062. right = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- !
  2063. ```
  2064. #### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
  2065. ```python
  2066. left = [[0.1, 0.1, 0.1], [0.6, 0.6, 0.6], [0.8, 0.8, 0.8]] # Shape: (3, 3) <- !
  2067. right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] # Shape: (3, 3) <- !
  2068. ```
  2069. #### 3. If neither non-matching dimension has size 1, rise an error.
  2070. ### Example
  2071. #### For each point returns index of its nearest point (`[0.1, 0.6, 0.8] => [1, 2, 1]`):
  2072. ```python
  2073. >>> points = np.array([0.1, 0.6, 0.8])
  2074. [ 0.1, 0.6, 0.8]
  2075. >>> wrapped_points = points.reshape(3, 1)
  2076. [[ 0.1],
  2077. [ 0.6],
  2078. [ 0.8]]
  2079. >>> distances = wrapped_points - points
  2080. [[ 0. , -0.5, -0.7],
  2081. [ 0.5, 0. , -0.2],
  2082. [ 0.7, 0.2, 0. ]]
  2083. >>> distances = np.abs(distances)
  2084. [[ 0. , 0.5, 0.7],
  2085. [ 0.5, 0. , 0.2],
  2086. [ 0.7, 0.2, 0. ]]
  2087. >>> i = np.arange(3)
  2088. [0, 1, 2]
  2089. >>> distances[i, i] = np.inf
  2090. [[ inf, 0.5, 0.7],
  2091. [ 0.5, inf, 0.2],
  2092. [ 0.7, 0.2, inf]]
  2093. >>> distances.argmin(1)
  2094. [1, 2, 1]
  2095. ```
  2096. Image
  2097. -----
  2098. ```python
  2099. # $ pip3 install pillow
  2100. from PIL import Image
  2101. ```
  2102. #### Creates a PNG image of a rainbow gradient:
  2103. ```python
  2104. width = 100
  2105. height = 100
  2106. size = width * height
  2107. pixels = [255 * i/size for i in range(size)]
  2108. img = Image.new('HSV', (width, height))
  2109. img.putdata([(int(a), 255, 255) for a in pixels])
  2110. img.convert(mode='RGB').save('test.png')
  2111. ```
  2112. #### Adds noise to a PNG image:
  2113. ```python
  2114. from random import randint
  2115. add_noise = lambda value: max(0, min(255, value + randint(-20, 20)))
  2116. img = Image.open('test.png').convert(mode='HSV')
  2117. img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
  2118. img.convert(mode='RGB').save('test.png')
  2119. ```
  2120. ### Modes
  2121. * **`'1'` - 1-bit pixels, black and white, stored with one pixel per byte.**
  2122. * **`'L'` - 8-bit pixels, greyscale.**
  2123. * **`'RGB'` - 3x8-bit pixels, true color.**
  2124. * **`'RGBA'` - 4x8-bit pixels, true color with transparency mask.**
  2125. * **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.**
  2126. Audio
  2127. -----
  2128. ```python
  2129. import wave
  2130. from struct import pack, iter_unpack
  2131. ```
  2132. ### Read Frames from WAV File
  2133. ```python
  2134. def read_wav_file(filename):
  2135. with wave.open(filename, 'rb') as wf:
  2136. frames = wf.readframes(wf.getnframes())
  2137. return [a[0] for a in iter_unpack('<h', frames)]
  2138. ```
  2139. ### Write Frames to WAV File
  2140. ```python
  2141. def write_to_wav_file(filename, frames_int, mono=True):
  2142. frames_short = (pack('<h', a) for a in frames_int)
  2143. with wave.open(filename, 'wb') as wf:
  2144. wf.setnchannels(1 if mono else 2)
  2145. wf.setsampwidth(2)
  2146. wf.setframerate(44100)
  2147. wf.writeframes(b''.join(frames_short))
  2148. ```
  2149. ### Examples
  2150. #### Saves a sine wave to a mono WAV file:
  2151. ```python
  2152. from math import pi, sin
  2153. frames_f = (sin(i * 2 * pi * 440 / 44100) for i in range(100000))
  2154. frames_i = (int(a * 30000) for a in frames_f)
  2155. write_to_wav_file('test.wav', frames_i)
  2156. ```
  2157. #### Adds noise to a mono WAV file:
  2158. ```python
  2159. from random import randint
  2160. add_noise = lambda value: max(-32768, min(32767, value + randint(-500, 500)))
  2161. frames_i = (add_noise(a) for a in read_wav_file('test.wav'))
  2162. write_to_wav_file('test.wav', frames_i)
  2163. ```
  2164. ### Synthesizer
  2165. ```python
  2166. # $ pip3 install simpleaudio
  2167. import simpleaudio, math, struct
  2168. from itertools import chain, repeat
  2169. F = 44100
  2170. P1 = '71♪,69,,71♪,66,,62♪,66,,59♪,,,'
  2171. P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
  2172. get_pause = lambda seconds: repeat(0, int(seconds * F))
  2173. sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
  2174. get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
  2175. get_hz = lambda key: 8.176 * 2 ** (int(key) / 12)
  2176. parse_note = lambda note: (get_hz(note[:2]), 0.25 if '♪' in note else 0.125)
  2177. get_frames = lambda note: get_wave(*parse_note(note)) if note else get_pause(0.125)
  2178. frames_f = chain.from_iterable(get_frames(n) for n in f'{P1}{P1}{P2}'.split(','))
  2179. frames_b = b''.join(struct.pack('<h', int(f * 30000)) for f in frames_f)
  2180. simpleaudio.play_buffer(frames_b, 1, 2, F)
  2181. ```
  2182. Basic Script Template
  2183. ---------------------
  2184. ```python
  2185. #!/usr/bin/env python3
  2186. #
  2187. # Usage: .py
  2188. #
  2189. from collections import namedtuple
  2190. from dataclasses import make_dataclass
  2191. from enum import Enum
  2192. import re
  2193. import sys
  2194. def main():
  2195. pass
  2196. ###
  2197. ## UTIL
  2198. #
  2199. def read_file(filename):
  2200. with open(filename, encoding='utf-8') as file:
  2201. return file.readlines()
  2202. if __name__ == '__main__':
  2203. main()
  2204. ```