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.

2613 lines
72 KiB

7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 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
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 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
6 years ago
6 years ago
5 years ago
6 years ago
7 years ago
7 years ago
5 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 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
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
5 years ago
6 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
6 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
6 years ago
6 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
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
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
6 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
5 years ago
5 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
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
6 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
6 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
6 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
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
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 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
6 years ago
6 years ago
5 years ago
6 years ago
7 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
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
5 years ago
5 years ago
7 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
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 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
6 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
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
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
5 years ago
6 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
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 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
6 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
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
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 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
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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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
6 years ago
6 years ago
6 years ago
6 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
6 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
6 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
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 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
6 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
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
6 years ago
6 years ago
7 years ago
7 years ago
7 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
6 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
5 years ago
6 years ago
6 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
6 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
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
6 years ago
6 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
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
5 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 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
6 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
6 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
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
6 years ago
7 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
6 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
6 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
6 years ago
5 years ago
6 years ago
6 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
6 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
6 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
6 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
7 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
6 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
6 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
6 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
6 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
6 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
6 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
6 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
6 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
6 years ago
6 years ago
6 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
5 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 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. ### Copy
  869. ```python
  870. from copy import copy, deepcopy
  871. <object> = copy(<object>)
  872. <object> = deepcopy(<object>)
  873. ```
  874. Duck Types
  875. ----------
  876. **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.**
  877. ### Comparable
  878. * **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`.**
  879. * **That means all objects compare not equal by default.**
  880. * **Only left side object has eq() method called, unless it returns 'NotImplemented', in which case the right object is consulted.**
  881. ```python
  882. class MyComparable:
  883. def __init__(self, a):
  884. self.a = a
  885. def __eq__(self, other):
  886. if isinstance(other, type(self)):
  887. return self.a == other.a
  888. return NotImplemented
  889. ```
  890. ### Hashable
  891. * **Hashable object needs both hash() and eq() methods and its hash value should never change.**
  892. * **Hashable objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do.**
  893. * **That is why Python automatically makes classes unhashable if you only implement eq().**
  894. ```python
  895. class MyHashable:
  896. def __init__(self, a):
  897. self._a = copy.deepcopy(a)
  898. @property
  899. def a(self):
  900. return self._a
  901. def __eq__(self, other):
  902. if isinstance(other, type(self)):
  903. return self.a == other.a
  904. return NotImplemented
  905. def __hash__(self):
  906. return hash(self.a)
  907. ```
  908. ### Sortable
  909. * **With 'total_ordering' decorator you only need to provide eq() and one of lt(), gt(), le() or ge() special methods.**
  910. ```python
  911. from functools import total_ordering
  912. @total_ordering
  913. class MySortable:
  914. def __init__(self, a):
  915. self.a = a
  916. def __eq__(self, other):
  917. if isinstance(other, type(self)):
  918. return self.a == other.a
  919. return NotImplemented
  920. def __lt__(self, other):
  921. if isinstance(other, type(self)):
  922. return self.a < other.a
  923. return NotImplemented
  924. ```
  925. ### Iterator
  926. * **Next() should return next item or raise 'StopIteration'.**
  927. * **Iter() should return 'self'.**
  928. ```python
  929. class Counter:
  930. def __init__(self):
  931. self.i = 0
  932. def __next__(self):
  933. self.i += 1
  934. return self.i
  935. def __iter__(self):
  936. return self
  937. ```
  938. ```python
  939. >>> counter = Counter()
  940. >>> next(counter), next(counter), next(counter)
  941. (1, 2, 3)
  942. ```
  943. ### Callable
  944. ```python
  945. class Counter:
  946. def __init__(self):
  947. self.i = 0
  948. def __call__(self):
  949. self.i += 1
  950. return self.i
  951. ```
  952. ```python
  953. >>> counter = Counter()
  954. >>> counter(), counter(), counter()
  955. (1, 2, 3)
  956. ```
  957. ### Context Manager
  958. ```python
  959. class MyOpen():
  960. def __init__(self, filename):
  961. self.filename = filename
  962. def __enter__(self):
  963. self.file = open(self.filename)
  964. return self.file
  965. def __exit__(self, *args):
  966. self.file.close()
  967. ```
  968. ```python
  969. >>> with open('test.txt', 'w') as file:
  970. ... file.write('Hello World!')
  971. >>> with MyOpen('test.txt') as file:
  972. ... print(file.read())
  973. Hello World!
  974. ```
  975. #### List of existing context managers:
  976. ```python
  977. with open('<path>', ...) as file: ...
  978. with wave.open('<path>', ...) as wave_file: ...
  979. with memoryview(<bytes/bytearray/array>) as view: ...
  980. db = sqlite3.connect('<path>'); with db: db.execute('<insert_query>')
  981. lock = threading.RLock(); with lock: ...
  982. ```
  983. Iterable Duck Types
  984. -------------------
  985. ### Iterable
  986. * **Only required method is iter(). It should return an iterator of object's items.**
  987. * **Contains() automatically works on any object that has iter() defined.**
  988. ```python
  989. class MyIterable:
  990. def __init__(self, a):
  991. self.a = a
  992. def __iter__(self):
  993. for el in self.a:
  994. yield el
  995. ```
  996. ```python
  997. >>> a = MyIterable([1, 2, 3])
  998. >>> iter(a)
  999. <generator object MyIterable.__iter__ at 0x1026c18b8>
  1000. >>> 1 in a
  1001. True
  1002. ```
  1003. ### Collection
  1004. * **Only required methods are iter() and len().**
  1005. * **This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
  1006. * **I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'.**
  1007. ```python
  1008. class MyCollection:
  1009. def __init__(self, a):
  1010. self.a = a
  1011. def __iter__(self):
  1012. for el in self.a:
  1013. yield el
  1014. def __contains__(self, el):
  1015. return el in self.a
  1016. def __len__(self):
  1017. return len(self.a)
  1018. ```
  1019. ### Sequence
  1020. * **Only required methods are len() and getitem(), that should return an item at index or raise 'IndexError'.**
  1021. * **Iter() and contains() automatically work on any object that has getitem() defined.**
  1022. * **Reversed() automatically works on any object that has getitem() and len() defined.**
  1023. ```python
  1024. class MySequence:
  1025. def __init__(self, a):
  1026. self.a = a
  1027. def __iter__(self):
  1028. for el in self.a:
  1029. yield el
  1030. def __contains__(self, el):
  1031. return el in self.a
  1032. def __len__(self):
  1033. return len(self.a)
  1034. def __getitem__(self, i):
  1035. return self.a[i]
  1036. def __reversed__(self):
  1037. return reversed(self.a)
  1038. ```
  1039. ### Collections.abc.Sequence
  1040. * **It's a richer interface than the basic sequence.**
  1041. * **Extending it generates iter(), contains(), reversed(), index(), and count().**
  1042. * **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.**
  1043. ```python
  1044. class MyAbcSequence(collections.abc.Sequence):
  1045. def __init__(self, a):
  1046. self.a = a
  1047. def __len__(self):
  1048. return len(self.a)
  1049. def __getitem__(self, i):
  1050. return self.a[i]
  1051. ```
  1052. #### Table of required and available special methods:
  1053. ```text
  1054. +------------+----------+------------+----------+--------------+
  1055. | | Iterable | Collection | Sequence | abc.Sequence |
  1056. +------------+----------+------------+----------+--------------+
  1057. | iter() | REQ | REQ | yes | yes |
  1058. | contains() | yes | yes | yes | yes |
  1059. | len() | | REQ | REQ | REQ |
  1060. | getitem() | | | REQ | REQ |
  1061. | reversed() | | | yes | yes |
  1062. | index() | | | | yes |
  1063. | count() | | | | yes |
  1064. +------------+----------+------------+----------+--------------+
  1065. ```
  1066. * **Other useful ABCs that automatically generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.**
  1067. Enum
  1068. ----
  1069. ```python
  1070. from enum import Enum, auto
  1071. class <enum_name>(Enum):
  1072. <member_name_1> = <value_1>
  1073. <member_name_2> = <value_2_a>, <value_2_b>
  1074. <member_name_3> = auto()
  1075. @classmethod
  1076. def get_member_names(cls):
  1077. return [a.name for a in cls.__members__.values()]
  1078. ```
  1079. * **If there are no numeric values before auto(), it returns 1.**
  1080. * **Otherwise it returns an increment of last numeric value.**
  1081. ```python
  1082. <member> = <enum>.<member_name>
  1083. <member> = <enum>['<member_name>']
  1084. <member> = <enum>(<value>)
  1085. name = <member>.name
  1086. value = <member>.value
  1087. ```
  1088. ```python
  1089. list_of_members = list(<enum>)
  1090. member_names = [a.name for a in <enum>]
  1091. member_values = [a.value for a in <enum>]
  1092. random_member = random.choice(list(<enum>))
  1093. ```
  1094. ### Inline
  1095. ```python
  1096. Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon'])
  1097. Cutlery = Enum('Cutlery', 'fork knife spoon')
  1098. Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
  1099. ```
  1100. #### Functions can not be values, so they must be wrapped:
  1101. ```python
  1102. from functools import partial
  1103. LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
  1104. 'OR' : partial(lambda l, r: l or r)})
  1105. ```
  1106. * **Another solution in this particular case, is to use `'and_'` and `'or_'` functions from module [Operator](#operator).**
  1107. Exceptions
  1108. ----------
  1109. ### Basic Example
  1110. ```python
  1111. try:
  1112. <code>
  1113. except <exception>:
  1114. <code>
  1115. ```
  1116. ### Complex Example
  1117. ```python
  1118. try:
  1119. <code_1>
  1120. except <exception_a>:
  1121. <code_2_a>
  1122. except <exception_b>:
  1123. <code_2_b>
  1124. else:
  1125. <code_2_c>
  1126. finally:
  1127. <code_3>
  1128. ```
  1129. ### Catching Exceptions
  1130. ```python
  1131. except <exception>:
  1132. except <exception> as <name>:
  1133. except (<exception_1>, <exception_2>, ...):
  1134. except (<exception_1>, <exception_2>, ...) as <name>:
  1135. ```
  1136. * **Also catches subclasses of the exception.**
  1137. ### Raising Exceptions
  1138. ```python
  1139. raise <exception>
  1140. raise <exception>()
  1141. raise <exception>(<el>)
  1142. raise <exception>(<el_1>, <el_2>, ...)
  1143. ```
  1144. #### Useful built-in exceptions:
  1145. ```python
  1146. raise ValueError('Argument is of right type but inappropriate value!')
  1147. raise TypeError('Argument is of wrong type!')
  1148. raise RuntimeError('None of above!')
  1149. ```
  1150. #### Re-raising caught exception:
  1151. ```python
  1152. except <exception>:
  1153. <code>
  1154. raise
  1155. ```
  1156. ### Common Built-in Exceptions
  1157. ```python
  1158. BaseException
  1159. +-- SystemExit # Raised by the sys.exit() function.
  1160. +-- KeyboardInterrupt # Raised when the user hits the interrupt key.
  1161. +-- Exception # User-defined exceptions should be derived from this class.
  1162. +-- StopIteration # Raised by next() when run on an empty iterator.
  1163. +-- ArithmeticError # Base class for arithmetic errors.
  1164. | +-- ZeroDivisionError # Raised when dividing by zero.
  1165. +-- AttributeError # Raised when an attribute is missing.
  1166. +-- EOFError # Raised by input() when it hits end-of-file condition.
  1167. +-- LookupError # Raised when a look-up on sequence or dict fails.
  1168. | +-- IndexError # Raised when a sequence index is out of range.
  1169. | +-- KeyError # Raised when a dictionary key is not found.
  1170. +-- NameError # Raised when a variable name is not found.
  1171. +-- OSError # Failures such as “file not found” or “disk full”.
  1172. | +-- FileNotFoundError # When a file or directory is requested but doesn't exist.
  1173. +-- RuntimeError # Raised by errors that don't fall in other categories.
  1174. | +-- RecursionError # Raised when the the maximum recursion depth is exceeded.
  1175. +-- TypeError # Raised when an argument is of wrong type.
  1176. +-- ValueError # When an argument is of right type but inappropriate value.
  1177. +-- UnicodeError # Raised when encoding/decoding strings from/to bytes fails.
  1178. ```
  1179. ### User-defined Exceptions
  1180. ```python
  1181. class MyError(Exception):
  1182. pass
  1183. class MyInputError(MyError):
  1184. pass
  1185. ```
  1186. Print
  1187. -----
  1188. ```python
  1189. print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  1190. ```
  1191. * **Use `'file=sys.stderr'` for errors.**
  1192. * **Use `'flush=True'` to forcibly flush the stream.**
  1193. ### Pretty Print
  1194. ```python
  1195. >>> from pprint import pprint
  1196. >>> pprint(dir())
  1197. ['__annotations__',
  1198. '__builtins__',
  1199. '__doc__', ...]
  1200. ```
  1201. Input
  1202. -----
  1203. * **Reads a line from user input or pipe if present.**
  1204. * **Trailing newline gets stripped.**
  1205. * **Prompt string is printed to the standard output before reading input.**
  1206. ```python
  1207. <str> = input(prompt=None)
  1208. ```
  1209. #### Prints lines until EOF:
  1210. ```python
  1211. while True:
  1212. try:
  1213. print(input())
  1214. except EOFError:
  1215. break
  1216. ```
  1217. Command Line Arguments
  1218. ----------------------
  1219. ```python
  1220. import sys
  1221. script_name = sys.argv[0]
  1222. arguments = sys.argv[1:]
  1223. ```
  1224. ### Argparse
  1225. ```python
  1226. from argparse import ArgumentParser, FileType
  1227. p = ArgumentParser(description=<str>)
  1228. p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag
  1229. p.add_argument('-<short_name>', '--<name>', type=<type>) # Option
  1230. p.add_argument('<name>', type=<type>, nargs=1) # Argument
  1231. p.add_argument('<name>', type=<type>, nargs='+') # Arguments
  1232. args = p.parse_args()
  1233. value = args.<name>
  1234. ```
  1235. * **Use `'help=<str>'` for argument description.**
  1236. * **Use `'type=FileType(<mode>)'` for files.**
  1237. Open
  1238. ----
  1239. **Opens a file and returns a corresponding file object.**
  1240. ```python
  1241. <file> = open('<path>', mode='r', encoding=None, newline=None)
  1242. ```
  1243. * **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
  1244. * **`'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.**
  1245. * **`'newline=""'` means no conversions take place, but lines are still broken by readline() on either '\n', '\r' or '\r\n'.**
  1246. ### Modes
  1247. * **`'r'` - Read (default).**
  1248. * **`'w'` - Write (truncate).**
  1249. * **`'x'` - Write or fail if the file already exists.**
  1250. * **`'a'` - Append.**
  1251. * **`'w+'` - Read and write (truncate).**
  1252. * **`'r+'` - Read and write from the start.**
  1253. * **`'a+'` - Read and write from the end.**
  1254. * **`'t'` - Text mode (default).**
  1255. * **`'b'` - Binary mode.**
  1256. ### Exceptions
  1257. * **`'FileNotFoundError'` can be risen when reading with `'r'` or `'r+'`.**
  1258. * **`'FileExistsError'` can be risen when writing with `'x'`.**
  1259. * **`'IsADirectoryError'` and `'PermissionError'` can be risen by any.**
  1260. * **`'OSError'` is the parent class of all listed exceptions.**
  1261. ### File
  1262. ```python
  1263. <file>.seek(0) # Moves to the start of the file.
  1264. <file>.seek(offset) # Moves 'offset' chars/bytes from the start.
  1265. <file>.seek(0, 2) # Moves to the end of the file.
  1266. <bin_file>.seek(±offset, <anchor>) # Anchor: 0 start, 1 current pos., 2 end.
  1267. ```
  1268. ```python
  1269. <str/bytes> = <file>.read(size=-1) # Reads 'size' chars/bytes or until EOF.
  1270. <str/bytes> = <file>.readline() # Returns a line or empty string on EOF.
  1271. <list> = <file>.readlines() # Returns a list of lines or empty list.
  1272. <str/bytes> = next(<file>) # Returns a line using buffer. Do not mix.
  1273. ```
  1274. ```python
  1275. <file>.write(<str/bytes>) # Writes a string or bytes object.
  1276. <file>.writelines(<coll.>) # Writes a coll. of strings or bytes objects.
  1277. <file>.flush() # Flushes write buffer.
  1278. ```
  1279. * **Methods do not add or strip trailing newlines, even writelines().**
  1280. ### Read Text from File
  1281. ```python
  1282. def read_file(filename):
  1283. with open(filename, encoding='utf-8') as file:
  1284. return file.readlines()
  1285. ```
  1286. ### Write Text to File
  1287. ```python
  1288. def write_to_file(filename, text):
  1289. with open(filename, 'w', encoding='utf-8') as file:
  1290. file.write(text)
  1291. ```
  1292. Path
  1293. ----
  1294. ```python
  1295. from os import path, listdir
  1296. from glob import glob
  1297. ```
  1298. ```python
  1299. <bool> = path.exists('<path>')
  1300. <bool> = path.isfile('<path>')
  1301. <bool> = path.isdir('<path>')
  1302. ```
  1303. ```python
  1304. <list> = listdir('<path>') # List of filenames located at 'path'.
  1305. <list> = glob('<pattern>') # Filenames matching the wildcard pattern.
  1306. ```
  1307. ### Pathlib
  1308. ```python
  1309. from pathlib import Path
  1310. ```
  1311. ```python
  1312. cwd = Path()
  1313. <Path> = Path('<path>' [, '<path>', <Path>, ...])
  1314. <Path> = <Path> / '<dir>' / '<file>'
  1315. ```
  1316. ```python
  1317. <bool> = <Path>.exists()
  1318. <bool> = <Path>.is_file()
  1319. <bool> = <Path>.is_dir()
  1320. ```
  1321. ```python
  1322. <iter> = <Path>.iterdir() # Iterator of filenames located at path.
  1323. <iter> = <Path>.glob('<pattern>') # Filenames matching the wildcard pattern.
  1324. ```
  1325. ```python
  1326. <str> = str(<Path>) # Returns path as a string.
  1327. <tup.> = <Path>.parts # Returns all components as strings.
  1328. <Path> = <Path>.resolve() # Returns absolute Path without symlinks.
  1329. ```
  1330. ```python
  1331. <str> = <Path>.name # Final component.
  1332. <str> = <Path>.stem # Final component without extension.
  1333. <str> = <Path>.suffix # Final component's extension.
  1334. <Path> = <Path>.parent # Path without final component.
  1335. ```
  1336. Command Execution
  1337. -----------------
  1338. ### Files and Directories Commands
  1339. * **Paths can be either strings or Path objects.**
  1340. * **All exceptions are either 'OSError' or its subclasses.**
  1341. ```python
  1342. import os
  1343. os.chdir(<path>) # Changes the current working directory.
  1344. <str> = os.getcwd() # Returns current working directory.
  1345. ```
  1346. ```python
  1347. os.remove(<path>) # Deletes the file.
  1348. os.rmdir(<path>) # Deletes empty directory.
  1349. shutil.rmtree(<path>) # Deletes an entire directory tree.
  1350. ```
  1351. ```python
  1352. os.rename(from, to) # Renames the file or directory.
  1353. os.replace(from, to) # Same, but overwrites 'to' if it exists.
  1354. ```
  1355. ```python
  1356. os.mkdir(<path>, mode=0o777) # Creates a directory.
  1357. <iter> = os.scandir(path='.') # Returns os.DirEntry objects located at path.
  1358. ```
  1359. #### DirEntry:
  1360. ```python
  1361. <str> = <DirEntry>.name # Final component of the path.
  1362. <str> = <DirEntry>.path # Path with final component.
  1363. <bool> = <DirEntry>.is_file()
  1364. <bool> = <DirEntry>.is_dir()
  1365. <bool> = <DirEntry>.is_symlink()
  1366. <Path> = Path(<DirEntry>)
  1367. ```
  1368. ### Shell Commands
  1369. ```python
  1370. import os
  1371. <str> = os.popen('<shell_command>').read()
  1372. ```
  1373. #### Using subprocess:
  1374. ```python
  1375. >>> import subprocess, shlex
  1376. >>> a = subprocess.run(shlex.split('ls -a'), stdout=subprocess.PIPE)
  1377. >>> a.stdout
  1378. b'.\n..\nfile1.txt\nfile2.txt\n'
  1379. >>> a.returncode
  1380. 0
  1381. ```
  1382. CSV
  1383. ---
  1384. ```python
  1385. import csv
  1386. <reader> = csv.reader(<file>, dialect='excel', delimiter=',', ...)
  1387. <list> = next(<reader>) # Returns a row as list of strings.
  1388. ```
  1389. ```python
  1390. <writer> = csv.writer(<file>, dialect='excel', delimiter=',', ...)
  1391. <writer>.writerow(<collection>)
  1392. <writer>.writerows(<coll_of_coll>)
  1393. ```
  1394. ### Parameters
  1395. * **`'dialect'` - Master parameter that sets the default values.**
  1396. * **`'delimiter'` - A one-character string used to separate fields.**
  1397. * **`'quotechar'` - Character for quoting fields that contain special characters.**
  1398. * **`'doublequote'` - Whether quotechars inside fields get doubled or escaped.**
  1399. * **`'skipinitialspace'` - Whether whitespace after delimiter gets stripped.**
  1400. * **`'lineterminator'` - How does writer terminate lines.**
  1401. * **`'quoting'` - Controls the amount of quoting: 0 - as necessary, 1 - all.**
  1402. * **`'escapechar'` - Character for escaping quotechar if doublequote is false.**
  1403. ### Dialects
  1404. ```python
  1405. +------------------+--------+-----------+--------------+
  1406. | | excel | excel_tab | unix_dialect |
  1407. +------------------+--------+-----------+--------------+
  1408. | delimiter | ',' | '\t' | ',' |
  1409. | quotechar | '"' | '"' | '"' |
  1410. | doublequote | True | True | True |
  1411. | skipinitialspace | False | False | False |
  1412. | lineterminator | '\r\n' | '\r\n' | '\n' |
  1413. | quoting | 0 | 0 | 1 |
  1414. | escapechar | None | None | None |
  1415. +------------------+--------+-----------+--------------+
  1416. ```
  1417. ### Read Rows from CSV File
  1418. ```python
  1419. def read_csv_file(filename):
  1420. with open(filename, encoding='utf-8', newline='') as file:
  1421. return csv.reader(file)
  1422. ```
  1423. * **If `'newline=""'` is not specified, then newlines embedded inside quoted fields will not be interpreted correctly.**
  1424. ### Write Rows to CSV File
  1425. ```python
  1426. def write_to_csv_file(filename, rows):
  1427. with open(filename, 'w', encoding='utf-8', newline='') as file:
  1428. writer = csv.writer(file)
  1429. writer.writerows(rows)
  1430. ```
  1431. JSON
  1432. ----
  1433. ```python
  1434. import json
  1435. <str> = json.dumps(<object>, ensure_ascii=True, indent=None)
  1436. <object> = json.loads(<str>)
  1437. ```
  1438. ### Read Object from JSON File
  1439. ```python
  1440. def read_json_file(filename):
  1441. with open(filename, encoding='utf-8') as file:
  1442. return json.load(file)
  1443. ```
  1444. ### Write Object to JSON File
  1445. ```python
  1446. def write_to_json_file(filename, an_object):
  1447. with open(filename, 'w', encoding='utf-8') as file:
  1448. json.dump(an_object, file, ensure_ascii=False, indent=2)
  1449. ```
  1450. Pickle
  1451. ------
  1452. ```python
  1453. import pickle
  1454. <bytes> = pickle.dumps(<object>)
  1455. <object> = pickle.loads(<bytes>)
  1456. ```
  1457. ### Read Object from File
  1458. ```python
  1459. def read_pickle_file(filename):
  1460. with open(filename, 'rb') as file:
  1461. return pickle.load(file)
  1462. ```
  1463. ### Write Object to File
  1464. ```python
  1465. def write_to_pickle_file(filename, an_object):
  1466. with open(filename, 'wb') as file:
  1467. pickle.dump(an_object, file)
  1468. ```
  1469. SQLite
  1470. ------
  1471. **Server-less database engine that stores each database into separate file.**
  1472. ```python
  1473. import sqlite3
  1474. db = sqlite3.connect('<path>') # Also ':memory:'.
  1475. ...
  1476. db.close()
  1477. ```
  1478. * **New database will be created if path doesn't exist.**
  1479. ### Read
  1480. ```python
  1481. cursor = db.execute('<query>')
  1482. if cursor:
  1483. <tuple> = cursor.fetchone() # First row.
  1484. <list> = cursor.fetchall() # Remaining rows.
  1485. ```
  1486. * **Returned values can be of type str, int, float, bytes or None.**
  1487. ### Write
  1488. ```python
  1489. db.execute('<query>')
  1490. db.commit()
  1491. ```
  1492. ### Placeholders
  1493. ```python
  1494. db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
  1495. db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
  1496. db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
  1497. ```
  1498. * **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
  1499. ### MySQL
  1500. **Has a very similar interface, with differences listed below.**
  1501. ```python
  1502. # $ pip3 install mysql-connector
  1503. from mysql import connector
  1504. db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
  1505. cursor = db.cursor()
  1506. cursor.execute('<query>') # Connector doesn't have execute method.
  1507. cursor.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
  1508. cursor.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.
  1509. ```
  1510. Bytes
  1511. -----
  1512. **Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.**
  1513. ```python
  1514. <bytes> = b'<str>' # Only accepts ASCII characters and \x00 - \xff.
  1515. <int> = <bytes>[<index>] # Returns int in range from 0 to 255.
  1516. <bytes> = <bytes>[<slice>] # Returns bytes even if it has only one element.
  1517. <bytes> = <bytes>.join(<coll_of_bytes>) # Joins elements using bytes object as separator.
  1518. ```
  1519. ### Encode
  1520. ```python
  1521. <bytes> = <str>.encode('utf-8') # Or: bytes(<str>, 'utf-8')
  1522. <bytes> = bytes(<coll_of_ints>) # Ints must be in range from 0 to 255.
  1523. <bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
  1524. <bytes> = bytes.fromhex('<hex>')
  1525. ```
  1526. ### Decode
  1527. ```python
  1528. <str> = <bytes>.decode('utf-8') # Or: str(<bytes>, 'utf-8')
  1529. <list> = list(<bytes>) # Returns ints in range from 0 to 255.
  1530. <int> = int.from_bytes(<bytes>, byteorder='big|little', signed=False)
  1531. '<hex>' = <bytes>.hex()
  1532. ```
  1533. ### Read Bytes from File
  1534. ```python
  1535. def read_bytes(filename):
  1536. with open(filename, 'rb') as file:
  1537. return file.read()
  1538. ```
  1539. ### Write Bytes to File
  1540. ```python
  1541. def write_bytes(filename, bytes_obj):
  1542. with open(filename, 'wb') as file:
  1543. file.write(bytes_obj)
  1544. ```
  1545. Struct
  1546. ------
  1547. * **Module that performs conversions between a sequence of numbers and a C struct, represented as a Python bytes object.**
  1548. * **Machine’s native type sizes and byte order are used by default.**
  1549. ```python
  1550. from struct import pack, unpack, iter_unpack, calcsize
  1551. <bytes> = pack('<format>', <num_1> [, <num_2>, ...])
  1552. <tuple> = unpack('<format>', <bytes>)
  1553. <tuples> = iter_unpack('<format>', <bytes>)
  1554. ```
  1555. ### Example
  1556. ```python
  1557. >>> pack('>hhl', 1, 2, 3)
  1558. b'\x00\x01\x00\x02\x00\x00\x00\x03'
  1559. >>> unpack('>hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
  1560. (1, 2, 3)
  1561. >>> calcsize('>hhl')
  1562. 8
  1563. ```
  1564. ### Format
  1565. #### For standard sizes start format string with:
  1566. * **`'='` - native byte order**
  1567. * **`'<'` - little-endian**
  1568. * **`'>'` - big-endian**
  1569. #### Integer types. Use capital letter for unsigned type. Standard sizes are in brackets:
  1570. * **`'x'` - pad byte**
  1571. * **`'b'` - char (1)**
  1572. * **`'h'` - short (2)**
  1573. * **`'i'` - int (4)**
  1574. * **`'l'` - long (4)**
  1575. * **`'q'` - long long (8)**
  1576. #### Floating point types:
  1577. * **`'f'` - float (4)**
  1578. * **`'d'` - double (8)**
  1579. Array
  1580. -----
  1581. **List that can only hold numbers of predefined type. Available types and their sizes in bytes are listed above.**
  1582. ```python
  1583. from array import array
  1584. <array> = array('<typecode>' [, <collection>])
  1585. ```
  1586. Memory View
  1587. -----------
  1588. **Used for accessing the internal data of an object that supports the buffer protocol.**
  1589. ```python
  1590. <memoryview> = memoryview(<bytes> / <bytearray> / <array>)
  1591. <memoryview>.release()
  1592. ```
  1593. Deque
  1594. -----
  1595. **A thread-safe list with efficient appends and pops from either side. Pronounced "deck".**
  1596. ```python
  1597. from collections import deque
  1598. <deque> = deque(<collection>, maxlen=None)
  1599. ```
  1600. ```python
  1601. <deque>.appendleft(<el>)
  1602. <el> = <deque>.popleft()
  1603. <deque>.extendleft(<collection>) # Collection gets reversed.
  1604. <deque>.rotate(n=1) # Rotates elements to the right.
  1605. ```
  1606. ```python
  1607. >>> a = deque([1, 2, 3], maxlen=3)
  1608. >>> a.append(4)
  1609. [2, 3, 4]
  1610. >>> a.appendleft(5)
  1611. [5, 2, 3]
  1612. >>> a.insert(1, 6)
  1613. IndexError: deque already at its maximum size
  1614. ```
  1615. Threading
  1616. ---------
  1617. ```python
  1618. from threading import Thread, RLock
  1619. ```
  1620. ### Thread
  1621. ```python
  1622. thread = Thread(target=<function>, args=(<first_arg>, ))
  1623. thread.start()
  1624. ...
  1625. thread.join()
  1626. ```
  1627. ### Lock
  1628. ```python
  1629. lock = RLock()
  1630. lock.acquire()
  1631. ...
  1632. lock.release()
  1633. ```
  1634. #### Or:
  1635. ```python
  1636. lock = RLock()
  1637. with lock:
  1638. ...
  1639. ```
  1640. Introspection
  1641. -------------
  1642. **Inspecting code at runtime.**
  1643. ### Variables
  1644. ```python
  1645. <list> = dir() # Names of variables in current scope.
  1646. <dict> = locals() # Dict of local variables. Also vars().
  1647. <dict> = globals() # Dict of global variables.
  1648. ```
  1649. ### Attributes
  1650. ```python
  1651. <dict> = vars(<object>)
  1652. <bool> = hasattr(<object>, '<attr_name>')
  1653. value = getattr(<object>, '<attr_name>')
  1654. setattr(<object>, '<attr_name>', value)
  1655. ```
  1656. ### Parameters
  1657. ```python
  1658. from inspect import signature
  1659. <sig> = signature(<function>)
  1660. no_of_params = len(<sig>.parameters)
  1661. param_names = list(<sig>.parameters.keys())
  1662. ```
  1663. Metaprograming
  1664. --------------
  1665. **Code that generates code.**
  1666. ### Type
  1667. **Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.**
  1668. ```python
  1669. <class> = type(<class_name>, <parents_tuple>, <attributes_dict>)
  1670. ```
  1671. ```python
  1672. >>> Z = type('Z', (), {'a': 'abcde', 'b': 12345})
  1673. >>> z = Z()
  1674. ```
  1675. ### Meta Class
  1676. **Class that creates class.**
  1677. ```python
  1678. def my_meta_class(name, parents, attrs):
  1679. attrs['a'] = 'abcde'
  1680. return type(name, parents, attrs)
  1681. ```
  1682. #### Or:
  1683. ```python
  1684. class MyMetaClass(type):
  1685. def __new__(cls, name, parents, attrs):
  1686. attrs['a'] = 'abcde'
  1687. return type.__new__(cls, name, parents, attrs)
  1688. ```
  1689. * **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.**
  1690. * **It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance (**`'MyMetaClass'` **in our case).**
  1691. * **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.**
  1692. ### Metaclass Attribute
  1693. **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().**
  1694. ```python
  1695. class MyClass(metaclass=MyMetaClass):
  1696. b = 12345
  1697. ```
  1698. ```python
  1699. >>> MyClass.a, MyClass.b
  1700. ('abcde', 12345)
  1701. ```
  1702. ### Type Diagram
  1703. ```python
  1704. type(MyClass) == MyMetaClass # MyClass is an instance of MyMetaClass.
  1705. type(MyMetaClass) == type # MyMetaClass is an instance of type.
  1706. ```
  1707. ```text
  1708. +---------+-------------+
  1709. | Classes | Metaclasses |
  1710. +---------+-------------|
  1711. | MyClass > MyMetaClass |
  1712. | | v |
  1713. | object ---> type <+ |
  1714. | | ^ +---+ |
  1715. | str -------+ |
  1716. +---------+-------------+
  1717. ```
  1718. ### Inheritance Diagram
  1719. ```python
  1720. MyClass.__base__ == object # MyClass is a subclass of object.
  1721. MyMetaClass.__base__ == type # MyMetaClass is a subclass of type.
  1722. ```
  1723. ```text
  1724. +---------+-------------+
  1725. | Classes | Metaclasses |
  1726. +---------+-------------|
  1727. | MyClass | MyMetaClass |
  1728. | v | v |
  1729. | object <--- type |
  1730. | ^ | |
  1731. | str | |
  1732. +---------+-------------+
  1733. ```
  1734. Operator
  1735. --------
  1736. ```python
  1737. from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
  1738. from operator import eq, ne, lt, le, gt, ge
  1739. from operator import and_, or_, not_
  1740. from operator import itemgetter, attrgetter, methodcaller
  1741. ```
  1742. ```python
  1743. import operator as op
  1744. sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
  1745. sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
  1746. product_of_elems = functools.reduce(op.mul, <collection>)
  1747. LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_})
  1748. last_el = op.methodcaller('pop')(<list>)
  1749. ```
  1750. Eval
  1751. ----
  1752. ```python
  1753. >>> from ast import literal_eval
  1754. >>> literal_eval('1 + 2')
  1755. 3
  1756. >>> literal_eval('[1, 2, 3]')
  1757. [1, 2, 3]
  1758. >>> literal_eval('abs(1)')
  1759. ValueError: malformed node or string
  1760. ```
  1761. Coroutine
  1762. ---------
  1763. * **Similar to generator, but generator pulls data through the pipe with iteration, while coroutine pushes data into the pipeline with send().**
  1764. * **Coroutines provide more powerful data routing possibilities than iterators.**
  1765. * **If you build a collection of simple data processing components, you can glue them together into complex arrangements of pipes, branches, merging, etc.**
  1766. ### Helper Decorator
  1767. * **All coroutines must be "primed" by first calling next().**
  1768. * **Remembering to call next() is easy to forget.**
  1769. * **Solved by wrapping coroutines with a decorator:**
  1770. ```python
  1771. def coroutine(func):
  1772. def out(*args, **kwargs):
  1773. cr = func(*args, **kwargs)
  1774. next(cr)
  1775. return cr
  1776. return out
  1777. ```
  1778. ### Pipeline Example
  1779. ```python
  1780. def reader(target):
  1781. for i in range(10):
  1782. target.send(i)
  1783. target.close()
  1784. @coroutine
  1785. def adder(target):
  1786. while True:
  1787. value = (yield)
  1788. target.send(value + 100)
  1789. @coroutine
  1790. def printer():
  1791. while True:
  1792. value = (yield)
  1793. print(value)
  1794. reader(adder(printer())) # 100, 101, ..., 109
  1795. ```
  1796. <br><br>
  1797. Libraries
  1798. =========
  1799. Progress Bar
  1800. ------------
  1801. ```python
  1802. # $ pip3 install tqdm
  1803. from tqdm import tqdm
  1804. from time import sleep
  1805. for i in tqdm([1, 2, 3]):
  1806. sleep(0.2)
  1807. for i in tqdm(range(100)):
  1808. sleep(0.02)
  1809. ```
  1810. Plot
  1811. ----
  1812. ```python
  1813. # $ pip3 install matplotlib
  1814. from matplotlib import pyplot
  1815. pyplot.plot(<data_1> [, <data_2>, ...]) # Or: hist, pie, ...
  1816. pyplot.savefig(<filename>)
  1817. pyplot.show()
  1818. pyplot.clf() # Clears figure.
  1819. ```
  1820. Table
  1821. -----
  1822. #### Prints a CSV file as an ASCII table:
  1823. ```python
  1824. # $ pip3 install tabulate
  1825. from tabulate import tabulate
  1826. import csv
  1827. with open(<filename>, encoding='utf-8', newline='') as file:
  1828. lines = csv.reader(file)
  1829. headers = [header.title() for header in next(lines)]
  1830. table = tabulate(lines, headers)
  1831. print(table)
  1832. ```
  1833. Curses
  1834. ------
  1835. ```python
  1836. from curses import wrapper, ascii
  1837. def main():
  1838. wrapper(draw)
  1839. def draw(screen):
  1840. screen.clear()
  1841. screen.addstr(0, 0, 'Press ESC to quit.')
  1842. while screen.getch() != ascii.ESC:
  1843. pass
  1844. def get_border(screen):
  1845. from collections import namedtuple
  1846. P = namedtuple('P', 'y x')
  1847. height, width = screen.getmaxyx()
  1848. return P(height-1, width-1)
  1849. if __name__ == '__main__':
  1850. main()
  1851. ```
  1852. Logging
  1853. -------
  1854. ```python
  1855. # $ pip3 install loguru
  1856. from loguru import logger
  1857. ```
  1858. ```python
  1859. logger.add('debug_{time}.log', colorize=True) # Connects a log file.
  1860. logger.add('error_{time}.log', level='ERROR') # Another file for errors or higher.
  1861. logger.<level>('A logging message.')
  1862. ```
  1863. * **Levels: `'debug'`, `'info'`, `'success'`, `'warning'`, `'error'`, `'critical'`.**
  1864. ### Exceptions
  1865. **Error description, stack trace and values of variables are appended automatically.**
  1866. ```python
  1867. try:
  1868. ...
  1869. except <exception>:
  1870. logger.exception('An error happened.')
  1871. ```
  1872. ### Rotation
  1873. **Argument that sets a condition when a new log file is created.**
  1874. ```python
  1875. rotation=<int>|<datetime.timedelta>|<datetime.time>|<str>
  1876. ```
  1877. * **`'<int>'` - Max file size in bytes.**
  1878. * **`'<timedelta>'` - Max age of a file.**
  1879. * **`'<time>'` - Time of day.**
  1880. * **`'<str>'` - Any of above as a string: `'100 MB'`, `'1 month'`, `'monday at 12:00'`, ...**
  1881. ### Retention
  1882. **Sets a condition which old log files are deleted.**
  1883. ```python
  1884. retention=<int>|<datetime.timedelta>|<str>
  1885. ```
  1886. * **`'<int>'` - Max number of files.**
  1887. * **`'<timedelta>'` - Max age of a file.**
  1888. * **`'<str>'` - Max age as a string: `'1 week, 3 days'`, `'2 months'`, ...**
  1889. Scraping
  1890. --------
  1891. #### Scrapes and prints Python's URL and version number from Wikipedia:
  1892. ```python
  1893. # $ pip3 install requests beautifulsoup4
  1894. import requests
  1895. from bs4 import BeautifulSoup
  1896. url = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
  1897. page = requests.get(url)
  1898. doc = BeautifulSoup(page.text, 'html.parser')
  1899. table = doc.find('table', class_='infobox vevent')
  1900. rows = table.find_all('tr')
  1901. link = rows[11].find('a')['href']
  1902. ver = rows[6].find('div').text.split()[0]
  1903. print(link, ver)
  1904. ```
  1905. Web
  1906. ---
  1907. ```python
  1908. # $ pip3 install bottle
  1909. from bottle import run, route, post, template, request, response
  1910. import json
  1911. ```
  1912. ### Run
  1913. ```python
  1914. run(host='localhost', port=8080)
  1915. run(host='0.0.0.0', port=80, server='cherrypy')
  1916. ```
  1917. ### Static Request
  1918. ```python
  1919. @route('/img/<image>')
  1920. def send_image(image):
  1921. return static_file(image, 'images/', mimetype='image/png')
  1922. ```
  1923. ### Dynamic Request
  1924. ```python
  1925. @route('/<sport>')
  1926. def send_page(sport):
  1927. return template('<h1>{{title}}</h1>', title=sport)
  1928. ```
  1929. ### REST Request
  1930. ```python
  1931. @post('/odds/<sport>')
  1932. def odds_handler(sport):
  1933. team = request.forms.get('team')
  1934. home_odds, away_odds = 2.44, 3.29
  1935. response.headers['Content-Type'] = 'application/json'
  1936. response.headers['Cache-Control'] = 'no-cache'
  1937. return json.dumps([team, home_odds, away_odds])
  1938. ```
  1939. #### Test:
  1940. ```python
  1941. # $ pip3 install requests
  1942. >>> import requests
  1943. >>> url = 'http://localhost:8080/odds/football'
  1944. >>> data = {'team': 'arsenal f.c.'}
  1945. >>> response = requests.post(url, data=data)
  1946. >>> response.json()
  1947. ['arsenal f.c.', 2.44, 3.29]
  1948. ```
  1949. Profile
  1950. -------
  1951. ### Basic
  1952. ```python
  1953. from time import time
  1954. start_time = time() # Seconds since Epoch.
  1955. ...
  1956. duration = time() - start_time
  1957. ```
  1958. ### High Performance
  1959. ```python
  1960. from time import perf_counter as pc
  1961. start_time = pc() # Seconds since restart.
  1962. ...
  1963. duration = pc() - start_time
  1964. ```
  1965. ### Timing a Snippet
  1966. ```python
  1967. >>> from timeit import timeit
  1968. >>> timeit('"-".join(str(a) for a in range(100))',
  1969. ... number=10000, globals=globals(), setup='pass')
  1970. 0.34986
  1971. ```
  1972. ### Line Profiler
  1973. ```python
  1974. # $ pip3 install line_profiler
  1975. @profile
  1976. def main():
  1977. a = [*range(10000)]
  1978. b = {*range(10000)}
  1979. main()
  1980. ```
  1981. #### Usage:
  1982. ```text
  1983. $ kernprof -lv test.py
  1984. Line # Hits Time Per Hit % Time Line Contents
  1985. ==============================================================
  1986. 1 @profile
  1987. 2 def main():
  1988. 3 1 1128.0 1128.0 27.4 a = [*range(10000)]
  1989. 4 1 2994.0 2994.0 72.6 b = {*range(10000)}
  1990. ```
  1991. ### Call Graph
  1992. #### Generates a PNG image of a call graph with highlighted bottlenecks:
  1993. ```python
  1994. # $ pip3 install pycallgraph
  1995. from pycallgraph import output, PyCallGraph
  1996. from datetime import datetime
  1997. time_str = datetime.now().strftime('%Y%m%d%H%M%S')
  1998. filename = f'profile-{time_str}.png'
  1999. drawer = output.GraphvizOutput(output_file=filename)
  2000. with PyCallGraph(drawer):
  2001. <code_to_be_profiled>
  2002. ```
  2003. NumPy
  2004. -----
  2005. **Array manipulation mini language. Can run up to one hundred times faster than equivalent Python code.**
  2006. ```python
  2007. # $ pip3 install numpy
  2008. import numpy as np
  2009. ```
  2010. ```python
  2011. <array> = np.array(<list>)
  2012. <array> = np.arange(from_inclusive, to_exclusive, ±step_size)
  2013. <array> = np.ones(<shape>)
  2014. <array> = np.random.randint(from_inclusive, to_exclusive, <shape>)
  2015. ```
  2016. ```python
  2017. <array>.shape = <shape>
  2018. <view> = <array>.reshape(<shape>)
  2019. <view> = np.broadcast_to(<array>, <shape>)
  2020. ```
  2021. ```python
  2022. <array> = <array>.sum(axis)
  2023. indexes = <array>.argmin(axis)
  2024. ```
  2025. * **Shape is a tuple of dimension sizes.**
  2026. * **Axis is an index of dimension that gets collapsed. Leftmost dimension has index 0.**
  2027. ### Indexing
  2028. ```bash
  2029. <el> = <2d_array>[0, 0] # First element.
  2030. <1d_view> = <2d_array>[0] # First row.
  2031. <1d_view> = <2d_array>[:, 0] # First column. Also [..., 0].
  2032. <3d_view> = <2d_array>[None, :, :] # Expanded by dimension of size 1.
  2033. ```
  2034. ```bash
  2035. <1d_array> = <2d_array>[<1d_row_indexes>, <1d_column_indexes>]
  2036. <2d_array> = <2d_array>[<2d_row_indexes>, <2d_column_indexes>]
  2037. ```
  2038. ```bash
  2039. <2d_bools> = <2d_array> > 0
  2040. <1d_array> = <2d_array>[<2d_bools>]
  2041. ```
  2042. * **If row and column indexes differ in shape, they are combined with broadcasting.**
  2043. ### Broadcasting
  2044. **Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.**
  2045. ```python
  2046. left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
  2047. right = [ 0.1 , 0.6 , 0.8 ] # Shape: (3)
  2048. ```
  2049. #### 1. If array shapes differ in length, left-pad the shorter shape with ones:
  2050. ```python
  2051. left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
  2052. right = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- !
  2053. ```
  2054. #### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
  2055. ```python
  2056. left = [[0.1, 0.1, 0.1], [0.6, 0.6, 0.6], [0.8, 0.8, 0.8]] # Shape: (3, 3) <- !
  2057. right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] # Shape: (3, 3) <- !
  2058. ```
  2059. #### 3. If neither non-matching dimension has size 1, rise an error.
  2060. ### Example
  2061. #### For each point returns index of its nearest point (`[0.1, 0.6, 0.8] => [1, 2, 1]`):
  2062. ```python
  2063. >>> points = np.array([0.1, 0.6, 0.8])
  2064. [ 0.1, 0.6, 0.8]
  2065. >>> wrapped_points = points.reshape(3, 1)
  2066. [[ 0.1],
  2067. [ 0.6],
  2068. [ 0.8]]
  2069. >>> distances = wrapped_points - points
  2070. [[ 0. , -0.5, -0.7],
  2071. [ 0.5, 0. , -0.2],
  2072. [ 0.7, 0.2, 0. ]]
  2073. >>> distances = np.abs(distances)
  2074. [[ 0. , 0.5, 0.7],
  2075. [ 0.5, 0. , 0.2],
  2076. [ 0.7, 0.2, 0. ]]
  2077. >>> i = np.arange(3)
  2078. [0, 1, 2]
  2079. >>> distances[i, i] = np.inf
  2080. [[ inf, 0.5, 0.7],
  2081. [ 0.5, inf, 0.2],
  2082. [ 0.7, 0.2, inf]]
  2083. >>> distances.argmin(1)
  2084. [1, 2, 1]
  2085. ```
  2086. Image
  2087. -----
  2088. ```python
  2089. # $ pip3 install pillow
  2090. from PIL import Image
  2091. ```
  2092. #### Creates a PNG image of a rainbow gradient:
  2093. ```python
  2094. width = 100
  2095. height = 100
  2096. size = width * height
  2097. pixels = [255 * i/size for i in range(size)]
  2098. img = Image.new('HSV', (width, height))
  2099. img.putdata([(int(a), 255, 255) for a in pixels])
  2100. img.convert(mode='RGB').save('test.png')
  2101. ```
  2102. #### Adds noise to a PNG image:
  2103. ```python
  2104. from random import randint
  2105. add_noise = lambda value: max(0, min(255, value + randint(-20, 20)))
  2106. img = Image.open('test.png').convert(mode='HSV')
  2107. img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
  2108. img.convert(mode='RGB').save('test.png')
  2109. ```
  2110. ### Modes
  2111. * **`'1'` - 1-bit pixels, black and white, stored with one pixel per byte.**
  2112. * **`'L'` - 8-bit pixels, greyscale.**
  2113. * **`'RGB'` - 3x8-bit pixels, true color.**
  2114. * **`'RGBA'` - 4x8-bit pixels, true color with transparency mask.**
  2115. * **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.**
  2116. Audio
  2117. -----
  2118. ```python
  2119. import wave
  2120. from struct import pack, iter_unpack
  2121. ```
  2122. ### Read Frames from WAV File
  2123. ```python
  2124. def read_wav_file(filename):
  2125. with wave.open(filename, 'rb') as wf:
  2126. frames = wf.readframes(wf.getnframes())
  2127. return [a[0] for a in iter_unpack('<h', frames)]
  2128. ```
  2129. ### Write Frames to WAV File
  2130. ```python
  2131. def write_to_wav_file(filename, frames_int, mono=True):
  2132. frames_short = (pack('<h', a) for a in frames_int)
  2133. with wave.open(filename, 'wb') as wf:
  2134. wf.setnchannels(1 if mono else 2)
  2135. wf.setsampwidth(2)
  2136. wf.setframerate(44100)
  2137. wf.writeframes(b''.join(frames_short))
  2138. ```
  2139. ### Examples
  2140. #### Saves a sine wave to a mono WAV file:
  2141. ```python
  2142. from math import pi, sin
  2143. frames_f = (sin(i * 2 * pi * 440 / 44100) for i in range(100000))
  2144. frames_i = (int(a * 30000) for a in frames_f)
  2145. write_to_wav_file('test.wav', frames_i)
  2146. ```
  2147. #### Adds noise to a mono WAV file:
  2148. ```python
  2149. from random import randint
  2150. add_noise = lambda value: max(-32768, min(32767, value + randint(-500, 500)))
  2151. frames_i = (add_noise(a) for a in read_wav_file('test.wav'))
  2152. write_to_wav_file('test.wav', frames_i)
  2153. ```
  2154. ### Synthesizer
  2155. ```python
  2156. # $ pip3 install simpleaudio
  2157. import simpleaudio, math, struct
  2158. from itertools import chain, repeat
  2159. F = 44100
  2160. P1 = '71♪,69,,71♪,66,,62♪,66,,59♪,,,'
  2161. P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
  2162. get_pause = lambda seconds: repeat(0, int(seconds * F))
  2163. sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
  2164. get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
  2165. get_hz = lambda key: 8.176 * 2 ** (int(key) / 12)
  2166. parse_note = lambda note: (get_hz(note[:2]), 0.25 if '♪' in note else 0.125)
  2167. get_frames = lambda note: get_wave(*parse_note(note)) if note else get_pause(0.125)
  2168. frames_f = chain.from_iterable(get_frames(n) for n in f'{P1}{P1}{P2}'.split(','))
  2169. frames_b = b''.join(struct.pack('<h', int(f * 30000)) for f in frames_f)
  2170. simpleaudio.play_buffer(frames_b, 1, 2, F)
  2171. ```
  2172. Basic Script Template
  2173. ---------------------
  2174. ```python
  2175. #!/usr/bin/env python3
  2176. #
  2177. # Usage: .py
  2178. #
  2179. from collections import namedtuple
  2180. from dataclasses import make_dataclass
  2181. from enum import Enum
  2182. import re
  2183. import sys
  2184. def main():
  2185. pass
  2186. ###
  2187. ## UTIL
  2188. #
  2189. def read_file(filename):
  2190. with open(filename, encoding='utf-8') as file:
  2191. return file.readlines()
  2192. if __name__ == '__main__':
  2193. main()
  2194. ```