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.

2076 lines
49 KiB

7 years ago
7 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
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
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
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
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 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
6 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
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
7 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
7 years ago
6 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
7 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
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
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
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
6 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
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
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
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
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
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
  1. Comprehensive Python Cheatsheet
  2. ===============================
  3. <sup>[Download text file](https://raw.githubusercontent.com/gto76/python-cheatsheet/master/README.md)
  4. or [Fork me on GitHub](https://github.com/gto76/python-cheatsheet).
  5. </sup>
  6. ![Monty Python](web/image_888.jpeg)
  7. Main
  8. ----
  9. ```python
  10. if __name__ == '__main__':
  11. main()
  12. ```
  13. List
  14. ----
  15. ```python
  16. <list> = <list>[from_inclusive : to_exclusive : step_size]
  17. ```
  18. ```python
  19. <list>.append(<el>)
  20. <list>.extend(<collection>)
  21. <list> += [<el>]
  22. <list> += <collection>
  23. ```
  24. ```python
  25. <list>.sort()
  26. <list>.reverse()
  27. <list> = sorted(<collection>)
  28. <iter> = reversed(<list>)
  29. ```
  30. ```python
  31. sum_of_elements = sum(<collection>)
  32. elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)]
  33. sorted_by_second = sorted(<collection>, key=lambda el: el[1])
  34. sorted_by_both = sorted(<collection>, key=lambda el: (el[1], el[0]))
  35. flatter_list = list(itertools.chain.from_iterable(<list>))
  36. product_of_elems = functools.reduce(lambda out, x: out * x, <collection>)
  37. list_of_chars = list(<str>)
  38. ```
  39. ```python
  40. index = <list>.index(<el>) # Returns first index of item.
  41. <list>.insert(index, <el>) # Inserts item at index and moves the rest to the right.
  42. <el> = <list>.pop([index]) # Removes and returns item at index or from the end.
  43. <list>.remove(<el>) # Removes first occurrence of item or raises ValueError.
  44. <list>.clear() # Removes all items.
  45. ```
  46. Dictionary
  47. ----------
  48. ```python
  49. <view> = <dict>.keys()
  50. <view> = <dict>.values()
  51. <view> = <dict>.items()
  52. ```
  53. ```python
  54. value = <dict>.get(key, default=None) # Returns default if key does not exist.
  55. value = <dict>.setdefault(key, default=None) # Same, but also adds default to dict.
  56. <dict> = collections.defaultdict(<type>) # Creates a dict with default value of type.
  57. <dict> = collections.defaultdict(lambda: 1) # Creates a dict with default value 1.
  58. ```
  59. ```python
  60. <dict>.update(<dict>) # Or: dict_a = {**dict_a, **dict_b}.
  61. <dict> = dict(<collection>) # Creates a dict from coll. of key-value pairs.
  62. <dict> = dict(zip(keys, values)) # Creates a dict from two collections.
  63. <dict> = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys.
  64. ```
  65. ```python
  66. value = <dict>.pop(key) # Removes item from dictionary.
  67. {k: v for k, v in <dict>.items() if k in keys} # Filters dictionary by keys.
  68. ```
  69. ### Counter
  70. ```python
  71. >>> from collections import Counter
  72. >>> colors = ['red', 'blue', 'yellow', 'blue', 'red', 'blue']
  73. >>> counter = Counter(colors)
  74. Counter({'blue': 3, 'red': 2, 'yellow': 1})
  75. >>> counter.most_common()[0]
  76. ('blue', 3)
  77. ```
  78. Set
  79. ---
  80. ```python
  81. <set> = set()
  82. ```
  83. ```python
  84. <set>.add(<el>)
  85. <set>.update(<collection>)
  86. <set> |= {<el>}
  87. <set> |= <set>
  88. ```
  89. ```python
  90. <set> = <set>.union(<coll.>) # Or: <set> | <set>
  91. <set> = <set>.intersection(<coll.>) # Or: <set> & <set>
  92. <set> = <set>.difference(<coll.>) # Or: <set> - <set>
  93. <set> = <set>.symmetric_difference(<coll.>) # Or: <set> ^ <set>
  94. <bool> = <set>.issubset(<coll.>) # Or: <set> <= <set>
  95. <bool> = <set>.issuperset(<coll.>) # Or: <set> >= <set>
  96. ```
  97. ```python
  98. <set>.remove(<el>) # Throws error.
  99. <set>.discard(<el>) # Doesn't throw error.
  100. ```
  101. ### Frozenset
  102. #### Is hashable, meaning it can be used as a key in dictionary or as an element in set.
  103. ```python
  104. <frozenset> = frozenset(<collection>)
  105. ```
  106. Range
  107. -----
  108. ```python
  109. <range> = range(to_exclusive)
  110. <range> = range(from_inclusive, to_exclusive)
  111. <range> = range(from_inclusive, to_exclusive, ±step_size)
  112. ```
  113. ```python
  114. from_inclusive = <range>.start
  115. to_exclusive = <range>.stop
  116. ```
  117. Enumerate
  118. ---------
  119. ```python
  120. for i, el in enumerate(<collection> [, i_start]):
  121. ...
  122. ```
  123. Named Tuple
  124. -----------
  125. * **Tuple is an immutable and hashable list.**
  126. * **Named tuple is its subclass with named elements.**
  127. ```python
  128. >>> from collections import namedtuple
  129. >>> Point = namedtuple('Point', 'x y')
  130. >>> p = Point(1, y=2)
  131. Point(x=1, y=2)
  132. >>> p[0]
  133. 1
  134. >>> p.x
  135. 1
  136. >>> getattr(p, 'y')
  137. 2
  138. >>> p._fields # Or: Point._fields
  139. ('x', 'y')
  140. ```
  141. Iterator
  142. --------
  143. **In this cheatsheet `'<collection>'` can also mean an iterator.**
  144. ```python
  145. from itertools import count, repeat, cycle, chain, islice
  146. ```
  147. ```python
  148. <iter> = iter(<collection>)
  149. <iter> = iter(<function>, to_exclusive) # Sequence of return values until 'to_exclusive'.
  150. <el> = next(<iter> [, default]) # Raises StopIteration or returns 'default' on end.
  151. ```
  152. ```python
  153. <iter> = count(start=0, step=1) # Returns incremented value endlessly.
  154. <iter> = repeat(<el> [, times]) # Returns element endlessly or 'times' times.
  155. <iter> = cycle(<collection>) # Repeats the sequence indefinitely.
  156. ```
  157. ```python
  158. <iter> = chain(<coll.>, <coll.>, ...) # Empties collections in order.
  159. <iter> = chain.from_iterable(<collection>) # Empties collections inside a collection in order.
  160. ```
  161. ```python
  162. <iter> = islice(<collection>, to_exclusive)
  163. <iter> = islice(<collection>, from_inclusive, to_exclusive)
  164. <iter> = islice(<collection>, from_inclusive, to_exclusive, step_size)
  165. ```
  166. Generator
  167. ---------
  168. **Convenient way to implement the iterator protocol.**
  169. ```python
  170. def count(start, step):
  171. while True:
  172. yield start
  173. start += step
  174. ```
  175. ```python
  176. >>> counter = count(10, 2)
  177. >>> next(counter), next(counter), next(counter)
  178. (10, 12, 14)
  179. ```
  180. Type
  181. ----
  182. ```python
  183. <type> = type(<el>) # <class 'int'> / <class 'str'> / ...
  184. ```
  185. ```python
  186. from numbers import Integral, Rational, Real, Complex, Number
  187. <bool> = isinstance(<el>, Number)
  188. ```
  189. ```python
  190. <bool> = callable(<el>)
  191. ```
  192. String
  193. ------
  194. ```python
  195. <str> = <str>.strip() # Strips all whitespace characters from both ends.
  196. <str> = <str>.strip('<chars>') # Strips all passed characters from both ends.
  197. ```
  198. ```python
  199. <list> = <str>.split() # Splits on any whitespace character.
  200. <list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times.
  201. <str> = <str>.join(<collection>) # Joins elements using string as separator.
  202. ```
  203. ```python
  204. <str> = <str>.replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times.
  205. <bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
  206. <bool> = <str>.endswith(<sub_str>) # Pass tuple of strings for multiple options.
  207. <int> = <str>.index(<sub_str>) # Returns start index of first match.
  208. ```
  209. ```python
  210. <bool> = <str>.isnumeric() # True if str contains only numeric characters.
  211. <list> = textwrap.wrap(<str>, width) # Nicely breaks string into lines.
  212. ```
  213. ### Char
  214. ```python
  215. <str> = chr(<int>) # Converts int to unicode char.
  216. <int> = ord(<str>) # Converts unicode char to int.
  217. ```
  218. ```python
  219. >>> ord('0'), ord('9')
  220. (48, 57)
  221. >>> ord('A'), ord('Z')
  222. (65, 90)
  223. >>> ord('a'), ord('z')
  224. (97, 122)
  225. ```
  226. Regex
  227. -----
  228. ```python
  229. import re
  230. <str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences.
  231. <list> = re.findall(<regex>, text) # Returns all occurrences.
  232. <list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to keep the matches.
  233. <Match> = re.search(<regex>, text) # Searches for first occurrence of pattern.
  234. <Match> = re.match(<regex>, text) # Searches only at the beginning of the text.
  235. <iter> = re.finditer(<regex>, text) # Returns all occurrences as match objects.
  236. ```
  237. * **Parameter `'flags=re.IGNORECASE'` can be used with all functions.**
  238. * **Parameter `'flags=re.DOTALL'` makes dot also accept newline.**
  239. * **Use `r'\1'` or `'\\1'` for backreference.**
  240. * **Use `'?'` to make operator non-greedy.**
  241. ### Match Object
  242. ```python
  243. <str> = <Match>.group() # Whole match.
  244. <str> = <Match>.group(1) # Part in first bracket.
  245. <tuple> = <Match>.groups() # All bracketed parts.
  246. <int> = <Match>.start() # Start index of a match.
  247. <int> = <Match>.end() # Exclusive end index of a match.
  248. ```
  249. ### Special Sequences
  250. **Expressions below hold true for strings that contain only ASCII characters. Use capital letters for negation.**
  251. ```python
  252. '\d' == '[0-9]' # Digit
  253. '\s' == '[ \t\n\r\f\v]' # Whitespace
  254. '\w' == '[a-zA-Z0-9_]' # Alphanumeric
  255. ```
  256. Format
  257. ------
  258. ```python
  259. <str> = f'{<el_1>}, {<el_2>}'
  260. <str> = '{}, {}'.format(<el_1>, <el_2>)
  261. ```
  262. ```python
  263. >>> Person = collections.namedtuple('Person', 'name height')
  264. >>> person = Person('Jean-Luc', 187)
  265. >>> f'{person.height}'
  266. '187'
  267. >>> '{p.height}'.format(p=person)
  268. '187'
  269. ```
  270. ### General Options
  271. ```python
  272. {<el>:<10} # '<el> '
  273. {<el>:>10} # ' <el>'
  274. {<el>:^10} # ' <el> '
  275. {<el>:->10} # '------<el>'
  276. {<el>:>0} # '<el>'
  277. ```
  278. ### String Options
  279. **`'!r'` calls object's repr() method, instead of format(), to get a string.**
  280. ```python
  281. {'abcde'!r:<10} # "'abcde' "
  282. {'abcde':.3} # 'abc'
  283. {'abcde':10.3} # 'abc '
  284. ```
  285. ### Float Options
  286. ```python
  287. {1.23456:.3f} # '1.235'
  288. {1.23456:10.3f} # ' 1.235'
  289. {1.23456:10.3e} # ' 1.235e+00'
  290. {1.23456:10.3%} # ' 123.456%'
  291. ```
  292. ### Int Options
  293. ```python
  294. { 123456:10,} # ' 123,456'
  295. { 123456:10_} # ' 123_456'
  296. { 123456:+10} # ' +123456'
  297. {-123456:=10} # '- 123456'
  298. { 123456: } # ' 123456'
  299. {-123456: } # '-123456'
  300. ```
  301. ```python
  302. {90:c} # 'Z'
  303. {90:X} # '5A'
  304. {3:08b} # '00000011'
  305. ```
  306. Numbers
  307. -------
  308. ### Basic Functions
  309. ```python
  310. <num> = pow(<num>, <num>) # Or: <num> ** <num>
  311. <real> = abs(<num>)
  312. <int> = round(<real>)
  313. <real> = round(<real>, ±ndigits)
  314. ```
  315. ### Math
  316. ```python
  317. from math import e, pi
  318. from math import cos, acos, sin, asin, tan, atan, degrees, radians
  319. from math import log, log10, log2
  320. from math import inf, nan, isinf, isnan
  321. ```
  322. ### Statistics
  323. ```python
  324. from statistics import mean, median, variance, pvariance, pstdev
  325. ```
  326. ### Random
  327. ```python
  328. from random import random, randint, choice, shuffle
  329. <float> = random()
  330. <int> = randint(from_inclusive, to_inclusive)
  331. <el> = choice(<list>)
  332. shuffle(<list>)
  333. ```
  334. Combinatorics
  335. -------------
  336. * **Every function returns an iterator.**
  337. * **If you want to print the iterator, you need to pass it to the list() function!**
  338. ```python
  339. from itertools import product, combinations, combinations_with_replacement, permutations
  340. ```
  341. ```python
  342. >>> product([0, 1], repeat=3)
  343. [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
  344. (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
  345. ```
  346. ```python
  347. >>> product('ab', '12')
  348. [('a', '1'), ('a', '2'),
  349. ('b', '1'), ('b', '2')]
  350. ```
  351. ```python
  352. >>> combinations('abc', 2)
  353. [('a', 'b'), ('a', 'c'), ('b', 'c')]
  354. ```
  355. ```python
  356. >>> combinations_with_replacement('abc', 2)
  357. [('a', 'a'), ('a', 'b'), ('a', 'c'),
  358. ('b', 'b'), ('b', 'c'),
  359. ('c', 'c')]
  360. ```
  361. ```python
  362. >>> permutations('abc', 2)
  363. [('a', 'b'), ('a', 'c'),
  364. ('b', 'a'), ('b', 'c'),
  365. ('c', 'a'), ('c', 'b')]
  366. ```
  367. Datetime
  368. --------
  369. * **Module 'datetime' provides 'date' `<D>`, 'time' `<T>`, 'datetime' `<DT>` and 'timedelta' `<TD>` classes. All are immutable and hashable.**
  370. * **Time and datetime can be 'aware' `<a>`, meaning they have defined timezone, or 'naive' `<n>`, meaning they don't.**
  371. * **If object is naive it is presumed to be in system's timezone.**
  372. ```python
  373. from datetime import date, time, datetime, timedelta
  374. from dateutil.tz import UTC, tzlocal, gettz
  375. ```
  376. ### Constructors
  377. ```python
  378. <D> = date(year, month, day)
  379. <T> = time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, fold=0)
  380. <DT> = datetime(year, month, day, hour=0, minute=0, second=0, ...)
  381. <TD> = timedelta(days=0, seconds=0, microseconds=0, milliseconds=0,
  382. minutes=0, hours=0, weeks=0)
  383. ```
  384. * **Use `'<D/DT>.weekday()'` to get the day of the week (Mon == 0).**
  385. * **`'fold=1'` means second pass in case of time jumping back for one hour.**
  386. ### Now
  387. ```python
  388. <D/DTn> = D/DT.today() # Current local date or naive datetime.
  389. <DTn> = DT.utcnow() # Naive datetime from current UTC time.
  390. <DTa> = DT.now(<tz>) # Aware datetime from current tz time.
  391. ```
  392. ### Timezone
  393. ```python
  394. <tz> = UTC # UTC timezone.
  395. <tz> = tzlocal() # Local timezone.
  396. <tz> = gettz('<Cont.>/<City>') # Timezone from 'Continent/City_Name' str.
  397. ```
  398. ```python
  399. <DTa> = <DT>.astimezone(<tz>) # Datetime, converted to passed timezone.
  400. <Ta/DTa> = <T/DT>.replace(tzinfo=<tz>) # Unconverted object with new timezone.
  401. ```
  402. ### Encode
  403. ```python
  404. <D/T/DT> = D/T/DT.fromisoformat('<iso>') # Object from ISO string.
  405. <DT> = DT.strptime(<str>, '<format>') # Datetime from str, according to format.
  406. <D/DTn> = D/DT.fromordinal(<int>) # D/DTn from days since Christ.
  407. <DTa> = DT.fromtimestamp(<real>, <tz>) # DTa from seconds since Epoch in tz time.
  408. ```
  409. * **ISO strings come in following forms: `'YYYY-MM-DD'`, `'HH:MM:SS.ffffff[±<offset>]'`, or both separated by `'T'`.**
  410. * **On Unix systems Epoch is `'1970-01-01 00:00 UTC'`, `'1970-01-01 01:00 CET'`, ...**
  411. ### Decode
  412. ```python
  413. <str> = <D/T/DT>.isoformat() # ISO string representation.
  414. <str> = <D/T/DT>.strftime('<format>') # Custom string representation.
  415. <int> = <D/DT>.toordinal() # Days since Christ, ignoring time and tz.
  416. <float> = <DT>.timestamp() # Seconds since Epoch in local time or tz.
  417. ```
  418. ### Format
  419. ```python
  420. >>> dt = datetime.strptime('2015-05-14 23:39:00.00 +0200', '%Y-%m-%d %H:%M:%S.%f %z')
  421. >>> dt.strftime("%A %dth %B '%y, %I:%M%p %Z")
  422. "Thursday 14th May '15, 11:39PM UTC+02:00"
  423. ```
  424. #### Rest of the codes:
  425. * **`'a'` - Weekday, abbreviated name.**
  426. * **`'b'` - Month, abbreviated name.**
  427. Arguments
  428. ---------
  429. ### Inside Function Call
  430. ```python
  431. <function>(<positional_args>) # f(0, 0)
  432. <function>(<keyword_args>) # f(x=0, y=0)
  433. <function>(<positional_args>, <keyword_args>) # f(0, y=0)
  434. ```
  435. ### Inside Function Definition
  436. ```python
  437. def f(<nondefault_args>): # def f(x, y)
  438. def f(<default_args>): # def f(x=0, y=0)
  439. def f(<nondefault_args>, <default_args>): # def f(x, y=0)
  440. ```
  441. Splat Operator
  442. --------------
  443. ### Inside Function Call
  444. **Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.**
  445. ```python
  446. args = (1, 2)
  447. kwargs = {'x': 3, 'y': 4, 'z': 5}
  448. func(*args, **kwargs)
  449. ```
  450. #### Is the same as:
  451. ```python
  452. func(1, 2, x=3, y=4, z=5)
  453. ```
  454. ### Inside Function Definition
  455. **Splat combines zero or more positional arguments into a tuple, while splatty-splat combines zero or more keyword arguments into a dictionary.**
  456. ```python
  457. def add(*a):
  458. return sum(a)
  459. ```
  460. ```python
  461. >>> add(1, 2, 3)
  462. 6
  463. ```
  464. #### Legal argument combinations:
  465. ```python
  466. def f(*args): # f(1, 2, 3)
  467. def f(x, *args): # f(1, 2, 3)
  468. def f(*args, z): # f(1, 2, z=3)
  469. def f(x, *args, z): # f(1, 2, z=3)
  470. ```
  471. ```python
  472. def f(**kwargs): # f(x=1, y=2, z=3)
  473. def f(x, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3)
  474. ```
  475. ```python
  476. 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)
  477. 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)
  478. def f(*args, y, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3)
  479. def f(x, *args, z, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)
  480. ```
  481. ### Other Uses
  482. ```python
  483. <list> = [*<collection> [, ...]]
  484. <set> = {*<collection> [, ...]}
  485. <tuple> = (*<collection>, [...])
  486. <dict> = {**<dict> [, ...]}
  487. ```
  488. ```python
  489. head, *body, tail = <collection>
  490. ```
  491. Inline
  492. ------
  493. ### Lambda
  494. ```python
  495. <function> = lambda: <return_value>
  496. <function> = lambda <argument_1>, <argument_2>: <return_value>
  497. ```
  498. ### Comprehension
  499. ```python
  500. <list> = [i+1 for i in range(10)] # [1, 2, ..., 10]
  501. <set> = {i for i in range(10) if i > 5} # {6, 7, 8, 9}
  502. <iter> = (i+5 for i in range(10)) # (5, 6, ..., 14)
  503. <dict> = {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18}
  504. ```
  505. ```python
  506. out = [i+j for i in range(10) for j in range(10)]
  507. ```
  508. #### Is the same as:
  509. ```python
  510. out = []
  511. for i in range(10):
  512. for j in range(10):
  513. out.append(i+j)
  514. ```
  515. ### Map, Filter, Reduce
  516. ```python
  517. from functools import reduce
  518. <iter> = map(lambda x: x + 1, range(10)) # (1, 2, ..., 10)
  519. <iter> = filter(lambda x: x > 5, range(10)) # (6, 7, 8, 9)
  520. <int> = reduce(lambda out, x: out + x, range(10)) # 45
  521. ```
  522. ### Any, All
  523. ```python
  524. <bool> = any(<collection>) # False if empty.
  525. <bool> = all(el[1] for el in <collection>) # True if empty.
  526. ```
  527. ### If - Else
  528. ```python
  529. <expression_if_true> if <condition> else <expression_if_false>
  530. ```
  531. ```python
  532. >>> [a if a else 'zero' for a in (0, 1, 0, 3)]
  533. ['zero', 1, 'zero', 3]
  534. ```
  535. ### Namedtuple, Enum, Class
  536. ```python
  537. from collections import namedtuple
  538. Point = namedtuple('Point', 'x y')
  539. point = Point(0, 0)
  540. ```
  541. ```python
  542. from enum import Enum
  543. Direction = Enum('Direction', 'n e s w')
  544. Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
  545. ```
  546. ```python
  547. # Warning: Objects will share the objects that are initialized in the dictionary!
  548. Creature = type('Creature', (), {'p': Point(0, 0), 'd': Direction.n})
  549. creature = Creature()
  550. ```
  551. Closure
  552. -------
  553. **We have a closure in Python when:**
  554. * **A nested function references a value of its enclosing function and then**
  555. * **the enclosing function returns the nested function.**
  556. ```python
  557. def get_multiplier(a):
  558. def out(b):
  559. return a * b
  560. return out
  561. ```
  562. ```python
  563. >>> multiply_by_3 = get_multiplier(3)
  564. >>> multiply_by_3(10)
  565. 30
  566. ```
  567. * **If multiple nested functions within enclosing function reference the same value, that value gets shared.**
  568. * **To dynamically access function's first free variable use `'<function>.__closure__[0].cell_contents'`.**
  569. ### Partial
  570. ```python
  571. from functools import partial
  572. <function> = partial(<function> [, <arg_1>, <arg_2>, ...])
  573. ```
  574. ```python
  575. >>> import operator as op
  576. >>> multiply_by_3 = partial(op.mul, 3)
  577. >>> multiply_by_3(10)
  578. 30
  579. ```
  580. ### Nonlocal
  581. **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 'nonlocal'.**
  582. ```python
  583. def get_counter():
  584. i = 0
  585. def out():
  586. nonlocal i
  587. i += 1
  588. return i
  589. return out
  590. ```
  591. ```python
  592. >>> counter = get_counter()
  593. >>> counter(), counter(), counter()
  594. (1, 2, 3)
  595. ```
  596. Decorator
  597. ---------
  598. **A decorator takes a function, adds some functionality and returns it.**
  599. ```python
  600. @decorator_name
  601. def function_that_gets_passed_to_decorator():
  602. ...
  603. ```
  604. ### Debugger Example
  605. **Decorator that prints function's name every time it gets called.**
  606. ```python
  607. from functools import wraps
  608. def debug(func):
  609. @wraps(func)
  610. def out(*args, **kwargs):
  611. print(func.__name__)
  612. return func(*args, **kwargs)
  613. return out
  614. @debug
  615. def add(x, y):
  616. return x + y
  617. ```
  618. * **Wraps is a helper decorator that copies metadata of function add() to function out().**
  619. * **Without it `'add.__name__'` would return `'out'`.**
  620. ### LRU Cache
  621. **Decorator that caches function's return values. All function's arguments must be hashable.**
  622. ```python
  623. from functools import lru_cache
  624. @lru_cache(maxsize=None)
  625. def fib(n):
  626. return n if n < 2 else fib(n-2) + fib(n-1)
  627. ```
  628. * **Recursion depth is limited to 1000 by default. To increase it use `'sys.setrecursionlimit(<depth>)'`.**
  629. ### Parametrized Decorator
  630. **A decorator that accepts arguments and returns a normal decorator that accepts a function.**
  631. ```python
  632. from functools import wraps
  633. def debug(print_result=False):
  634. def decorator(func):
  635. @wraps(func)
  636. def out(*args, **kwargs):
  637. result = func(*args, **kwargs)
  638. print(func.__name__, result if print_result else '')
  639. return result
  640. return out
  641. return decorator
  642. @debug(print_result=True)
  643. def add(x, y):
  644. return x + y
  645. ```
  646. Class
  647. -----
  648. ```python
  649. class <name>:
  650. def __init__(self, a):
  651. self.a = a
  652. def __repr__(self):
  653. class_name = self.__class__.__name__
  654. return f'{class_name}({self.a!r})'
  655. def __str__(self):
  656. return str(self.a)
  657. @classmethod
  658. def get_class_name(cls):
  659. return cls.__name__
  660. ```
  661. ### Constructor Overloading
  662. ```python
  663. class <name>:
  664. def __init__(self, a=None):
  665. self.a = a
  666. ```
  667. ### Inheritance
  668. ```python
  669. class Person:
  670. def __init__(self, name, age):
  671. self.name = name
  672. self.age = age
  673. class Employee(Person):
  674. def __init__(self, name, age, staff_num):
  675. super().__init__(name, age)
  676. self.staff_num = staff_num
  677. ```
  678. ### Comparable
  679. * **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`.**
  680. * **That means all objects compare not equal by default.**
  681. ```python
  682. class MyComparable:
  683. def __init__(self, a):
  684. self.a = a
  685. def __eq__(self, other):
  686. if isinstance(other, type(self)):
  687. return self.a == other.a
  688. return False
  689. ```
  690. ### Hashable
  691. * **Hashable object needs both hash() and eq() methods and its hash value should never change.**
  692. * **Hashable objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do.**
  693. * **That is why Python automatically makes classes unhashable if you only implement eq().**
  694. ```python
  695. class MyHashable:
  696. def __init__(self, a):
  697. self.__a = copy.deepcopy(a)
  698. @property
  699. def a(self):
  700. return self.__a
  701. def __eq__(self, other):
  702. if isinstance(other, type(self)):
  703. return self.a == other.a
  704. return False
  705. def __hash__(self):
  706. return hash(self.a)
  707. ```
  708. ### Sequence
  709. * **Methods do not depend on each other, so they can be skipped if not needed.**
  710. * **Any object with defined getitem() is considered iterable, even if it lacks iter().**
  711. ```python
  712. class MySequence:
  713. def __init__(self, a):
  714. self.a = a
  715. def __len__(self):
  716. return len(self.a)
  717. def __getitem__(self, i):
  718. return self.a[i]
  719. def __iter__(self):
  720. for el in self.a:
  721. yield el
  722. ```
  723. ### Callable
  724. ```python
  725. class Counter:
  726. def __init__(self):
  727. self.i = 0
  728. def __call__(self):
  729. self.i += 1
  730. return self.i
  731. ```
  732. ```python
  733. >>> counter = Counter()
  734. >>> counter(), counter(), counter()
  735. (1, 2, 3)
  736. ```
  737. ### Withable
  738. ```python
  739. class MyOpen():
  740. def __init__(self, filename):
  741. self.filename = filename
  742. def __enter__(self):
  743. self.file = open(self.filename)
  744. return self.file
  745. def __exit__(self, *args):
  746. self.file.close()
  747. ```
  748. ```python
  749. >>> with open('test.txt', 'w') as file:
  750. ... file.write('Hello World!')
  751. >>> with MyOpen('test.txt') as file:
  752. ... print(file.read())
  753. Hello World!
  754. ```
  755. ### Copy
  756. ```python
  757. from copy import copy, deepcopy
  758. <object> = copy(<object>)
  759. <object> = deepcopy(<object>)
  760. ```
  761. Enum
  762. ----
  763. ```python
  764. from enum import Enum, auto
  765. class <enum_name>(Enum):
  766. <member_name_1> = <value_1>
  767. <member_name_2> = <value_2_a>, <value_2_b>
  768. <member_name_3> = auto()
  769. @classmethod
  770. def get_member_names(cls):
  771. return [a.name for a in cls.__members__.values()]
  772. ```
  773. ```python
  774. <member> = <enum>.<member_name>
  775. <member> = <enum>['<member_name>']
  776. <member> = <enum>(<value>)
  777. name = <member>.name
  778. value = <member>.value
  779. ```
  780. ```python
  781. list_of_members = list(<enum>)
  782. member_names = [a.name for a in <enum>]
  783. member_values = [a.value for a in <enum>]
  784. random_member = random.choice(list(<enum>))
  785. ```
  786. ### Inline
  787. ```python
  788. Cutlery = Enum('Cutlery', ['fork', 'knife', 'spoon'])
  789. Cutlery = Enum('Cutlery', 'fork knife spoon')
  790. Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
  791. ```
  792. #### Functions can not be values, so they must be wrapped:
  793. ```python
  794. from functools import partial
  795. LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
  796. 'OR' : partial(lambda l, r: l or r)})
  797. ```
  798. Exceptions
  799. ----------
  800. ```python
  801. while True:
  802. try:
  803. x = int(input('Please enter a number: '))
  804. except ValueError:
  805. print('Oops! That was no valid number. Try again...')
  806. else:
  807. print('Thank you.')
  808. break
  809. ```
  810. ### Raising Exception
  811. ```python
  812. raise ValueError('A very specific message!')
  813. ```
  814. ### Finally
  815. ```python
  816. >>> try:
  817. ... raise KeyboardInterrupt
  818. ... finally:
  819. ... print('Goodbye, world!')
  820. Goodbye, world!
  821. Traceback (most recent call last):
  822. File "<stdin>", line 2, in <module>
  823. KeyboardInterrupt
  824. ```
  825. Print
  826. -----
  827. ```python
  828. print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  829. ```
  830. * **Use `'file=sys.stderr'` for errors.**
  831. ### Pretty Print
  832. ```python
  833. >>> from pprint import pprint
  834. >>> pprint(dir())
  835. ['__annotations__',
  836. '__builtins__',
  837. '__doc__', ...]
  838. ```
  839. Input
  840. -----
  841. * **Reads a line from user input or pipe if present.**
  842. * **Trailing newline gets stripped.**
  843. * **Prompt string is printed to the standard output before reading input.**
  844. ```python
  845. <str> = input(prompt=None)
  846. ```
  847. #### Prints lines until EOF:
  848. ```python
  849. while True:
  850. try:
  851. print(input())
  852. except EOFError:
  853. break
  854. ```
  855. Command Line Arguments
  856. ----------------------
  857. ```python
  858. import sys
  859. script_name = sys.argv[0]
  860. arguments = sys.argv[1:]
  861. ```
  862. ### Argparse
  863. ```python
  864. from argparse import ArgumentParser, FileType
  865. p = ArgumentParser(description=<str>)
  866. p.add_argument('-<short_name>', '--<name>', action='store_true') # Flag
  867. p.add_argument('-<short_name>', '--<name>', type=<type>) # Option
  868. p.add_argument('<name>', type=<type>, nargs=1) # Argument
  869. p.add_argument('<name>', type=<type>, nargs='+') # Arguments
  870. args = p.parse_args()
  871. value = args.<name>
  872. ```
  873. * **Use `'help=<str>'` for argument description.**
  874. * **Use `'type=FileType(<mode>)'` for files.**
  875. Open
  876. ----
  877. **Opens a file and returns a corresponding file object.**
  878. ```python
  879. <file> = open('<path>', mode='r', encoding=None)
  880. ```
  881. ### Modes
  882. * **`'r'` - Read (default).**
  883. * **`'w'` - Write (truncate).**
  884. * **`'x'` - Write or fail if the file already exists.**
  885. * **`'a'` - Append.**
  886. * **`'w+'` - Read and write (truncate).**
  887. * **`'r+'` - Read and write from the start.**
  888. * **`'a+'` - Read and write from the end.**
  889. * **`'t'` - Text mode (default).**
  890. * **`'b'` - Binary mode.**
  891. ### File
  892. ```python
  893. <file>.seek(0) # Moves to the start of the file.
  894. <file>.seek(offset) # Moves 'offset' chars/bytes from the start.
  895. <file>.seek(offset, <anchor>) # Anchor: 0 start, 1 current pos., 2 end.
  896. ```
  897. ```python
  898. <str/bytes> = <file>.read(size=-1) # Reads 'size' chars/bytes or until EOF.
  899. <str/bytes> = <file>.readline() # Returns a line.
  900. <list> = <file>.readlines() # Returns a list of lines.
  901. <str/bytes> = next(<file>) # Returns a line using buffer. Do not mix.
  902. ```
  903. ```python
  904. <file>.write(<str/bytes>) # Writes a string or bytes object.
  905. <file>.writelines(<list>) # Writes a list of strings or bytes objects.
  906. ```
  907. * **No method adds or strips trailing newlines.**
  908. * **Changes are not written until file gets closed.**
  909. ### Read Text from File
  910. ```python
  911. def read_file(filename):
  912. with open(filename, encoding='utf-8') as file:
  913. return file.readlines()
  914. ```
  915. ### Write Text to File
  916. ```python
  917. def write_to_file(filename, text):
  918. with open(filename, 'w', encoding='utf-8') as file:
  919. file.write(text)
  920. ```
  921. Path
  922. ----
  923. ```python
  924. from os import path, listdir
  925. <bool> = path.exists('<path>')
  926. <bool> = path.isfile('<path>')
  927. <bool> = path.isdir('<path>')
  928. <list> = listdir('<path>')
  929. ```
  930. ```python
  931. >>> from glob import glob
  932. >>> glob('../*.gif')
  933. ['1.gif', 'card.gif']
  934. ```
  935. ### Pathlib
  936. ```python
  937. from pathlib import Path
  938. cwd = Path()
  939. <Path> = Path('<path>' [, '<path>', <Path>, ...])
  940. <Path> = <Path> / '<dir>' / '<file>'
  941. ```
  942. ```python
  943. <bool> = <Path>.exists()
  944. <bool> = <Path>.is_file()
  945. <bool> = <Path>.is_dir()
  946. <iter> = <Path>.iterdir()
  947. ```
  948. ```python
  949. <iter> = <Path>.glob('<pattern>')
  950. ```
  951. ```python
  952. <str> = str(<Path>) # Returns path as a string.
  953. <tup.> = <Path>.parts # Returns all components as strings.
  954. <Path> = <Path>.resolve() # Returns absolute path without symlinks.
  955. ```
  956. ```python
  957. <str> = <Path>.name # Final component.
  958. <str> = <Path>.stem # Final component without extension.
  959. <str> = <Path>.suffix # Final component's extension.
  960. <Path> = <Path>.parent # Path without final component.
  961. ```
  962. Command Execution
  963. -----------------
  964. ```python
  965. import os
  966. <str> = os.popen(<command>).read()
  967. ```
  968. ### Subprocess
  969. ```python
  970. >>> import subprocess
  971. >>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE)
  972. >>> a.stdout
  973. b'.\n..\nfile1.txt\nfile2.txt\n'
  974. >>> a.returncode
  975. 0
  976. ```
  977. CSV
  978. ---
  979. ```python
  980. import csv
  981. ```
  982. ### Read Rows from CSV File
  983. ```python
  984. def read_csv_file(filename):
  985. with open(filename, encoding='utf-8') as file:
  986. return csv.reader(file, delimiter=';')
  987. ```
  988. ### Write Rows to CSV File
  989. ```python
  990. def write_to_csv_file(filename, rows):
  991. with open(filename, 'w', encoding='utf-8') as file:
  992. writer = csv.writer(file, delimiter=';')
  993. writer.writerows(rows)
  994. ```
  995. JSON
  996. ----
  997. ```python
  998. import json
  999. <str> = json.dumps(<object>, ensure_ascii=True, indent=None)
  1000. <object> = json.loads(<str>)
  1001. ```
  1002. ### Read Object from JSON File
  1003. ```python
  1004. def read_json_file(filename):
  1005. with open(filename, encoding='utf-8') as file:
  1006. return json.load(file)
  1007. ```
  1008. ### Write Object to JSON File
  1009. ```python
  1010. def write_to_json_file(filename, an_object):
  1011. with open(filename, 'w', encoding='utf-8') as file:
  1012. json.dump(an_object, file, ensure_ascii=False, indent=2)
  1013. ```
  1014. Pickle
  1015. ------
  1016. ```python
  1017. import pickle
  1018. <bytes> = pickle.dumps(<object>)
  1019. <object> = pickle.loads(<bytes>)
  1020. ```
  1021. ### Read Object from File
  1022. ```python
  1023. def read_pickle_file(filename):
  1024. with open(filename, 'rb') as file:
  1025. return pickle.load(file)
  1026. ```
  1027. ### Write Object to File
  1028. ```python
  1029. def write_to_pickle_file(filename, an_object):
  1030. with open(filename, 'wb') as file:
  1031. pickle.dump(an_object, file)
  1032. ```
  1033. SQLite
  1034. ------
  1035. ```python
  1036. import sqlite3
  1037. db = sqlite3.connect('<path>')
  1038. ...
  1039. db.close()
  1040. ```
  1041. ### Read
  1042. ```python
  1043. cursor = db.execute('<query>')
  1044. if cursor:
  1045. <tuple> = cursor.fetchone() # First row.
  1046. <list> = cursor.fetchall() # Remaining rows.
  1047. ```
  1048. ### Write
  1049. ```python
  1050. db.execute('<query>')
  1051. db.commit()
  1052. ```
  1053. Bytes
  1054. -----
  1055. **Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.**
  1056. ```python
  1057. <bytes> = b'<str>'
  1058. <int> = <bytes>[<index>]
  1059. <bytes> = <bytes>[<slice>]
  1060. <ints> = list(<bytes>)
  1061. <bytes> = b''.join(<coll_of_bytes>)
  1062. ```
  1063. ### Encode
  1064. ```python
  1065. <bytes> = <str>.encode(encoding='utf-8')
  1066. <bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
  1067. <bytes> = bytes.fromhex('<hex>')
  1068. ```
  1069. ### Decode
  1070. ```python
  1071. <str> = <bytes>.decode(encoding='utf-8')
  1072. <int> = int.from_bytes(<bytes>, byteorder='big|little', signed=False)
  1073. <hex> = <bytes>.hex()
  1074. ```
  1075. ### Read Bytes from File
  1076. ```python
  1077. def read_bytes(filename):
  1078. with open(filename, 'rb') as file:
  1079. return file.read()
  1080. ```
  1081. ### Write Bytes to File
  1082. ```python
  1083. def write_bytes(filename, bytes_obj):
  1084. with open(filename, 'wb') as file:
  1085. file.write(bytes_obj)
  1086. ```
  1087. Struct
  1088. ------
  1089. * **Module that performs conversions between Python values and a C struct, represented as a Python bytes object.**
  1090. * **Machine’s native type sizes and byte order are used by default.**
  1091. ```python
  1092. from struct import pack, unpack, iter_unpack, calcsize
  1093. <bytes> = pack('<format>', <value_1> [, <value_2>, ...])
  1094. <tuple> = unpack('<format>', <bytes>)
  1095. <tuples> = iter_unpack('<format>', <bytes>)
  1096. ```
  1097. ### Example
  1098. ```python
  1099. >>> pack('>hhl', 1, 2, 3)
  1100. b'\x00\x01\x00\x02\x00\x00\x00\x03'
  1101. >>> unpack('>hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
  1102. (1, 2, 3)
  1103. >>> calcsize('>hhl')
  1104. 8
  1105. ```
  1106. ### Format
  1107. #### For standard sizes start format string with:
  1108. * **`'='` - native byte order**
  1109. * **`'<'` - little-endian**
  1110. * **`'>'` - big-endian**
  1111. #### Use capital letter for unsigned type. Standard sizes are in brackets:
  1112. * **`'x'` - pad byte**
  1113. * **`'c'` - char (1)**
  1114. * **`'h'` - short (2)**
  1115. * **`'i'` - int (4)**
  1116. * **`'l'` - long (4)**
  1117. * **`'q'` - long long (8)**
  1118. * **`'f'` - float (4)**
  1119. * **`'d'` - double (8)**
  1120. Array
  1121. -----
  1122. **List that can hold only elements of predefined type. Available types are listed above.**
  1123. ```python
  1124. from array import array
  1125. <array> = array('<typecode>' [, <collection>])
  1126. ```
  1127. Memory View
  1128. -----------
  1129. **Used for accessing the internal data of an object that supports the buffer protocol.**
  1130. ```python
  1131. <memoryview> = memoryview(<bytes> / <bytearray> / <array>)
  1132. <memoryview>.release()
  1133. ```
  1134. Deque
  1135. -----
  1136. **Thread-safe list with efficient appends and pops from either side. Pronounced "deck".**
  1137. ```python
  1138. from collections import deque
  1139. <deque> = deque(<collection>, maxlen=None)
  1140. ```
  1141. ```python
  1142. <deque>.appendleft(<el>)
  1143. <el> = <deque>.popleft()
  1144. ```
  1145. ```python
  1146. <deque>.extendleft(<collection>) # Collection gets reversed.
  1147. <deque>.rotate(n=1) # Rotates elements to the right.
  1148. ```
  1149. Threading
  1150. ---------
  1151. ```python
  1152. from threading import Thread, RLock
  1153. ```
  1154. ### Thread
  1155. ```python
  1156. thread = Thread(target=<function>, args=(<first_arg>, ))
  1157. thread.start()
  1158. ...
  1159. thread.join()
  1160. ```
  1161. ### Lock
  1162. ```python
  1163. lock = RLock()
  1164. lock.acquire()
  1165. ...
  1166. lock.release()
  1167. ```
  1168. Introspection
  1169. -------------
  1170. **Inspecting code at runtime.**
  1171. ### Variables
  1172. ```python
  1173. <list> = dir() # Names of in-scope variables.
  1174. <dict> = locals() # Dict of local variables. Also vars().
  1175. <dict> = globals() # Dict of global variables.
  1176. ```
  1177. ### Attributes
  1178. ```python
  1179. <dict> = vars(<object>)
  1180. <bool> = hasattr(<object>, '<attr_name>')
  1181. value = getattr(<object>, '<attr_name>')
  1182. setattr(<object>, '<attr_name>', value)
  1183. ```
  1184. ### Parameters
  1185. ```python
  1186. from inspect import signature
  1187. sig = signature(<function>)
  1188. no_of_params = len(sig.parameters)
  1189. param_names = list(sig.parameters.keys())
  1190. ```
  1191. Metaprograming
  1192. --------------
  1193. **Code that generates code.**
  1194. ### Type
  1195. **Type is the root class. If only passed the object it returns its type (class). Otherwise it creates a new class.**
  1196. ```python
  1197. <class> = type(<class_name>, <parents_tuple>, <attributes_dict>)
  1198. ```
  1199. ```python
  1200. >>> Z = type('Z', (), {'a': 'abcde', 'b': 12345})
  1201. >>> z = Z()
  1202. ```
  1203. ### Meta Class
  1204. **Class that creates class.**
  1205. ```python
  1206. def my_meta_class(name, parents, attrs):
  1207. attrs['a'] = 'abcde'
  1208. return type(name, parents, attrs)
  1209. ```
  1210. #### Or:
  1211. ```python
  1212. class MyMetaClass(type):
  1213. def __new__(cls, name, parents, attrs):
  1214. attrs['a'] = 'abcde'
  1215. return type.__new__(cls, name, parents, attrs)
  1216. ```
  1217. * **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.**
  1218. * **It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance ('MyMetaClass' in our case).**
  1219. * **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.**
  1220. ### Metaclass Attribute
  1221. **When 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.**
  1222. ```python
  1223. class MyClass(metaclass=MyMetaClass):
  1224. b = 12345
  1225. ```
  1226. ```python
  1227. >>> MyClass.a, MyClass.b
  1228. ('abcde', 12345)
  1229. ```
  1230. Operator
  1231. --------
  1232. ```python
  1233. from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
  1234. from operator import eq, ne, lt, le, gt, ge
  1235. from operator import not_, and_, or_
  1236. from operator import itemgetter, attrgetter, methodcaller
  1237. ```
  1238. ```python
  1239. import operator as op
  1240. product_of_elems = functools.reduce(op.mul, <collection>)
  1241. sorted_by_second = sorted(<collection>, key=op.itemgetter(1))
  1242. sorted_by_both = sorted(<collection>, key=op.itemgetter(1, 0))
  1243. LogicOp = enum.Enum('LogicOp', {'AND': op.and_, 'OR' : op.or_})
  1244. last_el = op.methodcaller('pop')(<list>)
  1245. ```
  1246. Eval
  1247. ----
  1248. ### Basic
  1249. ```python
  1250. >>> from ast import literal_eval
  1251. >>> literal_eval('1 + 2')
  1252. 3
  1253. >>> literal_eval('[1, 2, 3]')
  1254. [1, 2, 3]
  1255. >>> literal_eval('abs(1)')
  1256. ValueError: malformed node or string
  1257. ```
  1258. ### Using Abstract Syntax Trees
  1259. ```python
  1260. import ast
  1261. from ast import Num, BinOp, UnaryOp
  1262. import operator as op
  1263. LEGAL_OPERATORS = {ast.Add: op.add, # <el> + <el>
  1264. ast.Sub: op.sub, # <el> - <el>
  1265. ast.Mult: op.mul, # <el> * <el>
  1266. ast.Div: op.truediv, # <el> / <el>
  1267. ast.Pow: op.pow, # <el> ** <el>
  1268. ast.BitXor: op.xor, # <el> ^ <el>
  1269. ast.USub: op.neg} # - <el>
  1270. def evaluate(expression):
  1271. root = ast.parse(expression, mode='eval')
  1272. return eval_node(root.body)
  1273. def eval_node(node):
  1274. node_type = type(node)
  1275. if node_type == Num:
  1276. return node.n
  1277. if node_type not in [BinOp, UnaryOp]:
  1278. raise TypeError(node)
  1279. operator_type = type(node.op)
  1280. if operator_type not in LEGAL_OPERATORS:
  1281. raise TypeError(f'Illegal operator {node.op}')
  1282. operator = LEGAL_OPERATORS[operator_type]
  1283. if node_type == BinOp:
  1284. left, right = eval_node(node.left), eval_node(node.right)
  1285. return operator(left, right)
  1286. elif node_type == UnaryOp:
  1287. operand = eval_node(node.operand)
  1288. return operator(operand)
  1289. ```
  1290. ```python
  1291. >>> evaluate('2 ^ 6')
  1292. 4
  1293. >>> evaluate('2 ** 6')
  1294. 64
  1295. >>> evaluate('1 + 2 * 3 ** (4 ^ 5) / (6 + -7)')
  1296. -5.0
  1297. ```
  1298. Coroutine
  1299. ---------
  1300. * **Similar to generator, but generator pulls data through the pipe with iteration, while coroutine pushes data into the pipeline with send().**
  1301. * **Coroutines provide more powerful data routing possibilities than iterators.**
  1302. * **If you built a collection of simple data processing components, you can glue them together into complex arrangements of pipes, branches, merging, etc.**
  1303. ### Helper Decorator
  1304. * **All coroutines must be "primed" by first calling next().**
  1305. * **Remembering to call next() is easy to forget.**
  1306. * **Solved by wrapping coroutines with a decorator:**
  1307. ```python
  1308. def coroutine(func):
  1309. def out(*args, **kwargs):
  1310. cr = func(*args, **kwargs)
  1311. next(cr)
  1312. return cr
  1313. return out
  1314. ```
  1315. ### Pipeline Example
  1316. ```python
  1317. def reader(target):
  1318. for i in range(10):
  1319. target.send(i)
  1320. target.close()
  1321. @coroutine
  1322. def adder(target):
  1323. while True:
  1324. item = (yield)
  1325. target.send(item + 100)
  1326. @coroutine
  1327. def printer():
  1328. while True:
  1329. item = (yield)
  1330. print(item)
  1331. reader(adder(printer())) # 100, 101, ..., 109
  1332. ```
  1333. <br><br>
  1334. Libraries
  1335. =========
  1336. Progress Bar
  1337. ------------
  1338. ```python
  1339. # $ pip3 install tqdm
  1340. from tqdm import tqdm
  1341. from time import sleep
  1342. for i in tqdm([1, 2, 3]):
  1343. sleep(0.2)
  1344. for i in tqdm(range(100)):
  1345. sleep(0.02)
  1346. ```
  1347. Plot
  1348. ----
  1349. ```python
  1350. # $ pip3 install matplotlib
  1351. from matplotlib import pyplot
  1352. pyplot.plot(<data_1> [, <data_2>, ...])
  1353. pyplot.savefig(<filename>, transparent=True)
  1354. pyplot.show()
  1355. ```
  1356. Table
  1357. -----
  1358. #### Prints CSV file as ASCII table:
  1359. ```python
  1360. # $ pip3 install tabulate
  1361. from tabulate import tabulate
  1362. import csv
  1363. with open(<filename>, encoding='utf-8') as file:
  1364. lines = csv.reader(file, delimiter=';')
  1365. headers = [header.title() for header in next(lines)]
  1366. table = tabulate(lines, headers)
  1367. print(table)
  1368. ```
  1369. Curses
  1370. ------
  1371. ```python
  1372. from curses import wrapper
  1373. def main():
  1374. wrapper(draw)
  1375. def draw(screen):
  1376. screen.clear()
  1377. screen.addstr(0, 0, 'Press ESC to quit.')
  1378. while screen.getch() != 27:
  1379. pass
  1380. def get_border(screen):
  1381. from collections import namedtuple
  1382. P = namedtuple('P', 'x y')
  1383. height, width = screen.getmaxyx()
  1384. return P(width-1, height-1)
  1385. ```
  1386. Logging
  1387. -------
  1388. ```python
  1389. # $ pip3 install loguru
  1390. from loguru import logger
  1391. ```
  1392. ```python
  1393. logger.add('debug_{time}.log', colorize=True) # Connects a log file.
  1394. logger.add('error_{time}.log', level='ERROR') # Adds another file for errors or higher.
  1395. logger.<level>('A logging message')
  1396. ```
  1397. * **Levels: `'debug'`, `'info'`, `'success'`, `'warning'`, `'error'`, `'critical'`.**
  1398. ### Rotation
  1399. **Parameter that sets a condition when a new log file is created.**
  1400. ```python
  1401. rotation=<int>|<datetime.timedelta>|<datetime.time>|<str>
  1402. ```
  1403. * **`'<int>'` - Max file size in bytes.**
  1404. * **`'<timedelta>'` - Max age of a file.**
  1405. * **`'<time>'` - Time of day.**
  1406. * **`'<str>'` - Any of above as a string: `'100 MB'`, `'1 month 2 weeks'`, `'18:00'`, `'w0'`, `'monday at 12:00'`, ...**
  1407. ### Retention
  1408. **Sets a condition which old log files are deleted.**
  1409. ```python
  1410. retention=<int>|<datetime.timedelta>|<str>
  1411. ```
  1412. * **`'<int>'` - Max number of files.**
  1413. * **`'<timedelta>'` - Max age of a file.**
  1414. * **`'<str>'` - Max age as a string: `'1 week, 3 days'`, `'2 months'`, ...**
  1415. ### Compression
  1416. **Sets how inactive log files are compressed.**
  1417. ```python
  1418. compression='gz'|'bz2'|'tar'|'tar.gz'|'tar.bz2'|'zip'
  1419. ```
  1420. Scraping
  1421. --------
  1422. ```python
  1423. # $ pip3 install requests beautifulsoup4
  1424. >>> import requests
  1425. >>> from bs4 import BeautifulSoup
  1426. >>> url = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
  1427. >>> page = requests.get(url)
  1428. >>> doc = BeautifulSoup(page.text, 'html.parser')
  1429. >>> table = doc.find('table', class_='infobox vevent')
  1430. >>> rows = table.find_all('tr')
  1431. >>> link = rows[11].find('a')['href']
  1432. >>> ver = rows[6].find('div').text.split()[0]
  1433. >>> link, ver
  1434. ('https://www.python.org/', '3.7.2')
  1435. ```
  1436. Web
  1437. ---
  1438. ```python
  1439. # $ pip3 install bottle
  1440. from bottle import run, route, post, template, request, response
  1441. import json
  1442. ```
  1443. ### Run
  1444. ```python
  1445. run(host='localhost', port=8080)
  1446. run(host='0.0.0.0', port=80, server='cherrypy')
  1447. ```
  1448. ### Static Request
  1449. ```python
  1450. @route('/img/<image>')
  1451. def send_image(image):
  1452. return static_file(image, 'images/', mimetype='image/png')
  1453. ```
  1454. ### Dynamic Request
  1455. ```python
  1456. @route('/<sport>')
  1457. def send_page(sport):
  1458. return template('<h1>{{title}}</h1>', title=sport)
  1459. ```
  1460. ### REST Request
  1461. ```python
  1462. @post('/odds/<sport>')
  1463. def odds_handler(sport):
  1464. team = request.forms.get('team')
  1465. home_odds, away_odds = 2.44, 3.29
  1466. response.headers['Content-Type'] = 'application/json'
  1467. response.headers['Cache-Control'] = 'no-cache'
  1468. return json.dumps([team, home_odds, away_odds])
  1469. ```
  1470. #### Test:
  1471. ```python
  1472. # $ pip3 install requests
  1473. >>> import requests
  1474. >>> url = 'http://localhost:8080/odds/football'
  1475. >>> data = {'team': 'arsenal f.c.'}
  1476. >>> response = requests.post(url, data=data)
  1477. >>> response.json()
  1478. ['arsenal f.c.', 2.44, 3.29]
  1479. ```
  1480. Profile
  1481. -------
  1482. ### Basic
  1483. ```python
  1484. from time import time
  1485. start_time = time() # Seconds since Epoch.
  1486. ...
  1487. duration = time() - start_time
  1488. ```
  1489. ### High Performance
  1490. ```python
  1491. from time import perf_counter as pc
  1492. start_time = pc() # Seconds since restart.
  1493. ...
  1494. duration = pc() - start_time
  1495. ```
  1496. ### Timing a Snippet
  1497. ```python
  1498. from timeit import timeit
  1499. timeit('"-".join(str(a) for a in range(100))',
  1500. number=10000, globals=globals(), setup='pass')
  1501. ```
  1502. ### Line Profiler
  1503. ```python
  1504. # $ pip3 install line_profiler
  1505. @profile
  1506. def main():
  1507. a = [*range(10000)]
  1508. b = {*range(10000)}
  1509. main()
  1510. ```
  1511. #### Usage:
  1512. ```text
  1513. $ kernprof -lv test.py
  1514. Line # Hits Time Per Hit % Time Line Contents
  1515. ==============================================================
  1516. 1 @profile
  1517. 2 def main():
  1518. 3 1 1128.0 1128.0 27.4 a = [*range(10000)]
  1519. 4 1 2994.0 2994.0 72.6 b = {*range(10000)}
  1520. ```
  1521. ### Call Graph
  1522. #### Generates a PNG image of a call graph with highlighted bottlenecks:
  1523. ```python
  1524. # $ pip3 install pycallgraph
  1525. from pycallgraph import output, PyCallGraph
  1526. from datetime import datetime
  1527. time_str = datetime.now().strftime('%Y%m%d%H%M%S')
  1528. filename = f'profile-{time_str}.png'
  1529. drawer = output.GraphvizOutput(output_file=filename)
  1530. with PyCallGraph(output=drawer):
  1531. <code_to_be_profiled>
  1532. ```
  1533. NumPy
  1534. -----
  1535. **Array manipulation mini language. Can run up to one hundred times faster than equivalent Python code.**
  1536. ```python
  1537. # $ pip3 install numpy
  1538. import numpy as np
  1539. ```
  1540. ```python
  1541. <array> = np.array(<list>)
  1542. <array> = np.arange(from_inclusive, to_exclusive, ±step_size)
  1543. <array> = np.ones(<shape>)
  1544. <array> = np.random.randint(from_inclusive, to_exclusive, <shape>)
  1545. ```
  1546. ```python
  1547. <array>.shape = <shape>
  1548. <view> = <array>.reshape(<shape>)
  1549. <view> = np.broadcast_to(<array>, <shape>)
  1550. ```
  1551. ```python
  1552. <array> = <array>.sum(axis)
  1553. indexes = <array>.argmin(axis)
  1554. ```
  1555. * **Shape is a tuple of dimension sizes.**
  1556. * **Axis is an index of dimension that gets collapsed. Leftmost dimension has index 0.**
  1557. ### Indexing
  1558. ```bash
  1559. <el> = <2d_array>[0, 0] # First element.
  1560. <1d_view> = <2d_array>[0] # First row.
  1561. <1d_view> = <2d_array>[:, 0] # First column. Also [..., 0].
  1562. <3d_view> = <2d_array>[None, :, :] # Expanded by dimension of size 1.
  1563. ```
  1564. ```bash
  1565. <1d_array> = <2d_array>[<1d_row_indexes>, <1d_column_indexes>]
  1566. <2d_array> = <2d_array>[<2d_row_indexes>, <2d_column_indexes>]
  1567. ```
  1568. ```bash
  1569. <2d_bools> = <2d_array> > 0
  1570. <1d_array> = <2d_array>[<2d_bools>]
  1571. ```
  1572. * **If row and column indexes differ in shape, they are combined with broadcasting.**
  1573. ### Broadcasting
  1574. **Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.**
  1575. ```python
  1576. left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
  1577. right = [ 0.1 , 0.6 , 0.8 ] # Shape: (3)
  1578. ```
  1579. #### 1. If array shapes differ in length, left-pad the smaller shape with ones:
  1580. ```python
  1581. left = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
  1582. right = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- !
  1583. ```
  1584. #### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
  1585. ```python
  1586. left = [[0.1, 0.1, 0.1], [0.6, 0.6, 0.6], [0.8, 0.8, 0.8]] # Shape: (3, 3) <- !
  1587. right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] # Shape: (3, 3) <- !
  1588. ```
  1589. #### 3. If neither non-matching dimension has size 1, rise an error.
  1590. ### Example
  1591. #### For each point returns index of its nearest point (`[0.1, 0.6, 0.8] => [1, 2, 1]`):
  1592. ```python
  1593. >>> points = np.array([0.1, 0.6, 0.8])
  1594. [ 0.1, 0.6, 0.8]
  1595. >>> wrapped_points = points.reshape(3, 1)
  1596. [[ 0.1],
  1597. [ 0.6],
  1598. [ 0.8]]
  1599. >>> distances = wrapped_points - points
  1600. [[ 0. , -0.5, -0.7],
  1601. [ 0.5, 0. , -0.2],
  1602. [ 0.7, 0.2, 0. ]]
  1603. >>> distances = np.abs(distances)
  1604. [[ 0. , 0.5, 0.7],
  1605. [ 0.5, 0. , 0.2],
  1606. [ 0.7, 0.2, 0. ]]
  1607. >>> i = np.arange(3)
  1608. [0, 1, 2]
  1609. >>> distances[i, i] = np.inf
  1610. [[ inf, 0.5, 0.7],
  1611. [ 0.5, inf, 0.2],
  1612. [ 0.7, 0.2, inf]]
  1613. >>> distances.argmin(1)
  1614. [1, 2, 1]
  1615. ```
  1616. Image
  1617. -----
  1618. ```python
  1619. # $ pip3 install pillow
  1620. from PIL import Image
  1621. ```
  1622. #### Creates PNG image of a rainbow gradient:
  1623. ```python
  1624. width = 100
  1625. height = 100
  1626. size = width * height
  1627. pixels = [255 * i/size for i in range(size)]
  1628. img = Image.new('HSV', (width, height))
  1629. img.putdata([(int(a), 255, 255) for a in pixels])
  1630. img.convert(mode='RGB').save('test.png')
  1631. ```
  1632. #### Adds noise to a PNG image:
  1633. ```python
  1634. from random import randint
  1635. add_noise = lambda value: max(0, min(255, value + randint(-20, 20)))
  1636. img = Image.open('test.png').convert(mode='HSV')
  1637. img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
  1638. img.convert(mode='RGB').save('test.png')
  1639. ```
  1640. ### Modes
  1641. * **`'1'` - 1-bit pixels, black and white, stored with one pixel per byte.**
  1642. * **`'L'` - 8-bit pixels, greyscale.**
  1643. * **`'RGB'` - 3x8-bit pixels, true color.**
  1644. * **`'RGBA'` - 4x8-bit pixels, true color with transparency mask.**
  1645. * **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.**
  1646. Audio
  1647. -----
  1648. ```python
  1649. import wave
  1650. from struct import pack, iter_unpack
  1651. ```
  1652. ### Read Frames from WAV File
  1653. ```python
  1654. def read_wav_file(filename):
  1655. with wave.open(filename, 'rb') as wf:
  1656. frames = wf.readframes(wf.getnframes())
  1657. return [a[0] for a in iter_unpack('<h', frames)]
  1658. ```
  1659. ### Write Frames to WAV File
  1660. ```python
  1661. def write_to_wav_file(filename, frames_int, mono=True):
  1662. frames_short = (pack('<h', a) for a in frames_int)
  1663. with wave.open(filename, 'wb') as wf:
  1664. wf.setnchannels(1 if mono else 2)
  1665. wf.setsampwidth(2)
  1666. wf.setframerate(44100)
  1667. wf.writeframes(b''.join(frames_short))
  1668. ```
  1669. ### Examples
  1670. #### Saves a sine wave to a mono WAV file:
  1671. ```python
  1672. from math import pi, sin
  1673. frames_f = (sin(i * 2 * pi * 440 / 44100) for i in range(100000))
  1674. frames_i = (int(a * 30000) for a in frames_f)
  1675. write_to_wav_file('test.wav', frames_i)
  1676. ```
  1677. #### Adds noise to a mono WAV file:
  1678. ```python
  1679. from random import randint
  1680. add_noise = lambda value: max(-32768, min(32767, value + randint(-500, 500)))
  1681. frames_i = (add_noise(a) for a in read_wav_file('test.wav'))
  1682. write_to_wav_file('test.wav', frames_i)
  1683. ```
  1684. #### Plays Popcorn:
  1685. ```python
  1686. # $ pip3 install simpleaudio
  1687. import simpleaudio, math, struct
  1688. from itertools import chain, repeat
  1689. F = 44100
  1690. P1 = '71♪,69,,71♪,66,,62♪,66,,59♪,,,'
  1691. P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
  1692. get_pause = lambda seconds: repeat(0, int(seconds * F))
  1693. sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
  1694. get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
  1695. get_hz = lambda key: 8.176 * 2 ** (int(key) / 12)
  1696. parse_n = lambda note: (get_hz(note[:2]), 0.25 if '♪' in note else 0.125)
  1697. get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
  1698. frames_i = chain.from_iterable(get_note(n) for n in f'{P1}{P1}{P2}'.split(','))
  1699. frames_b = b''.join(struct.pack('<h', int(a * 30000)) for a in frames_i)
  1700. simpleaudio.play_buffer(frames_b, 1, 2, F)
  1701. ```
  1702. Basic Script Template
  1703. ---------------------
  1704. ```python
  1705. #!/usr/bin/env python3
  1706. #
  1707. # Usage: .py
  1708. #
  1709. from collections import namedtuple
  1710. from enum import Enum
  1711. import re
  1712. import sys
  1713. def main():
  1714. pass
  1715. ###
  1716. ## UTIL
  1717. #
  1718. def read_file(filename):
  1719. with open(filename, encoding='utf-8') as file:
  1720. return file.readlines()
  1721. if __name__ == '__main__':
  1722. main()
  1723. ```