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.

1351 lines
27 KiB

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
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
5 years ago
5 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
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
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
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
7 years ago
6 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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
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
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
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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
  1. Comprehensive Python Cheatsheet
  2. ===============================
  3. ![Monty Python](web/image_888.jpeg)
  4. Main
  5. ----
  6. ```python
  7. if __name__ == '__main__':
  8. main()
  9. ```
  10. List
  11. ----
  12. ```python
  13. <list>[from_inclusive : to_exclusive : step_size]
  14. <list>.append(<el>) # Or: <list> += [<el>]
  15. <list>.extend(<list>) # Or: <list> += <list>
  16. ```
  17. ```python
  18. <list>.sort()
  19. <list>.reverse()
  20. <list> = sorted(<list>)
  21. <iter> = reversed(<list>)
  22. ```
  23. ```python
  24. sum_of_elements = sum(<list>)
  25. elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)]
  26. sorted_by_second = sorted(<list>, key=lambda el: el[1])
  27. sorted_by_both = sorted(<list>, key=lambda el: (el[1], el[0]))
  28. flattened_list = [item for sublist in <list> for item in sublist]
  29. list_of_chars = list(<str>)
  30. product_of_elems = functools.reduce(lambda out, x: out * x, <list>)
  31. ```
  32. ```python
  33. index = <list>.index(<el>) # Returns first index of item.
  34. <list>.insert(index, <el>) # Inserts item at index and moves the rest to the right.
  35. <el> = <list>.pop([index]) # Removes and returns item at index or from the end.
  36. <list>.remove(<el>) # Removes first occurrence of item.
  37. <list>.clear() # Removes all items.
  38. ```
  39. Dictionary
  40. ----------
  41. ```python
  42. <view> = <dict>.keys()
  43. <view> = <dict>.values()
  44. <view> = <dict>.items()
  45. <value> = <dict>.get(key, default) # Returns default if key does not exist.
  46. <value> = <dict>.setdefault(key, default) # Same, but also adds default to dict.
  47. <dict>.update(<dict>)
  48. ```
  49. ```python
  50. collections.defaultdict(<type>) # Creates a dictionary with default value of type.
  51. collections.defaultdict(lambda: 1) # Creates a dictionary with default value 1.
  52. collections.OrderedDict() # Creates ordered dictionary.
  53. ```
  54. ```python
  55. dict(<list>) # Initiates a dict from list of key-value pairs.
  56. dict(zip(keys, values)) # Initiates a dict from two lists.
  57. {k: v for k, v in <dict>.items() if k in <list>} # Filters a dict by keys.
  58. ```
  59. ### Counter
  60. ```python
  61. >>> from collections import Counter
  62. >>> colors = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
  63. >>> Counter(colors)
  64. Counter({'blue': 3, 'red': 2, 'yellow': 1})
  65. >>> <counter>.most_common()[0][0]
  66. 'blue'
  67. ```
  68. Set
  69. ---
  70. ```python
  71. <set> = set()
  72. <set>.add(<el>)
  73. <set>.update(<set>)
  74. <set>.clear()
  75. ```
  76. ```python
  77. <set> = <set>.union(<set>) # Or: <set> | <set>
  78. <set> = <set>.intersection(<set>) # Or: <set> & <set>
  79. <set> = <set>.difference(<set>) # Or: <set> - <set>
  80. <set> = <set>.symmetric_difference(<set>) # Or: <set> ^ <set>
  81. <bool> = <set>.issubset(<set>)
  82. <bool> = <set>.issuperset(<set>)
  83. ```
  84. ### Frozenset
  85. #### Is hashable and can be used as a key in dictionary:
  86. ```python
  87. <frozenset> = frozenset(<collection>)
  88. ```
  89. Range
  90. -----
  91. ```python
  92. range(to_exclusive)
  93. range(from_inclusive, to_exclusive)
  94. range(from_inclusive, to_exclusive, step_size)
  95. range(from_inclusive, to_exclusive, -step_size)
  96. ```
  97. ```python
  98. from_inclusive = <range>.start
  99. to_exclusive = <range>.stop
  100. ```
  101. Enumerate
  102. ---------
  103. ```python
  104. for i, <el> in enumerate(<collection> [, i_start]):
  105. ...
  106. ```
  107. Named Tuple
  108. -----------
  109. ```python
  110. >>> Point = collections.namedtuple('Point', ['x', 'y'])
  111. >>> a = Point(1, y=2)
  112. Point(x=1, y=2)
  113. >>> a.x
  114. 1
  115. >>> getattr(a, 'y')
  116. 2
  117. >>> Point._fields
  118. ('x', 'y')
  119. ```
  120. Iterator
  121. --------
  122. #### Skips first element:
  123. ```python
  124. next(<iter>)
  125. for element in <iter>:
  126. ...
  127. ```
  128. #### Reads input until it reaches an empty line:
  129. ```python
  130. for line in iter(input, ''):
  131. ...
  132. ```
  133. #### Same, but prints a message every time:
  134. ```python
  135. from functools import partial
  136. for line in iter(partial(input, 'Please enter value'), ''):
  137. ...
  138. ```
  139. Generator
  140. ---------
  141. **Convenient way to implement the iterator protocol.**
  142. ```python
  143. def step(start, step):
  144. while True:
  145. yield start
  146. start += step
  147. ```
  148. ```python
  149. >>> stepper = step(10, 2)
  150. >>> next(stepper), next(stepper), next(stepper)
  151. (10, 12, 14)
  152. ```
  153. Type
  154. ----
  155. ```python
  156. <type> = type(<el>) # <class 'int'> / <class 'str'> / ...
  157. ```
  158. ```python
  159. from numbers import Number, Integral, Real, Rational, Complex
  160. is_number = isinstance(<el>, Number)
  161. is_function = callable(<el>)
  162. ```
  163. String
  164. ------
  165. ```python
  166. <str> = <str>.strip() # Strips all whitespace characters.
  167. <str> = <str>.strip('<chars>') # Strips all passed characters.
  168. <list> = <str>.split() # Splits on any whitespace character.
  169. <list> = <str>.split(sep=None, maxsplit=-1) # Splits on 'sep' at most 'maxsplit' times.
  170. ```
  171. ```python
  172. <str> = <str>.join(<list>)
  173. <str> = <str>.replace(old_str, new_str)
  174. <bool> = <str>.startswith(<sub_str>) # Pass tuple of strings for multiple options.
  175. <bool> = <str>.endswith(<sub_str>) # Pass tuple of strings for multiple options.
  176. <int> = <str>.index(<sub_str>) # Returns first index of a substring.
  177. <bool> = <str>.isnumeric() # True if str contains only numeric characters.
  178. <list> = textwrap.wrap(<str>, width) # Nicely breakes string into lines.
  179. ```
  180. ### Char
  181. ```python
  182. <str> = chr(<int>) # Converts int to unicode char.
  183. <int> = ord(<str>) # Converts unicode char to int.
  184. ```
  185. ```python
  186. >>> ord('0'), ord('9')
  187. (48, 57)
  188. >>> ord('A'), ord('Z')
  189. (65, 90)
  190. >>> ord('a'), ord('z')
  191. (97, 122)
  192. ```
  193. ### Print
  194. ```python
  195. print(<el_1> [, <el_2>, end='', sep='', file=<file>]) # Use 'file=sys.stderr' for errors.
  196. ```
  197. ### Regex
  198. ```python
  199. import re
  200. <str> = re.sub(<regex>, new, text, count=0) # Substitutes all occurrences.
  201. <list> = re.findall(<regex>, text)
  202. <list> = re.split(<regex>, text, maxsplit=0) # Use brackets in regex to keep the matches.
  203. <Match> = re.search(<regex>, text) # Searches for first occurrence of pattern.
  204. <Match> = re.match(<regex>, text) # Searches only at the beginning of the string.
  205. <Match_iter> = re.finditer(<regex>, text) # Searches for all occurences of pattern.
  206. ```
  207. * **Parameter 'flags=re.IGNORECASE' can be used with all functions. Parameter 'flags=re.DOTALL' makes dot also accept newline.**
  208. * **Use '\\\\1' or r'\1' for backreference.**
  209. * **Use ? to make operators non-greedy.**
  210. #### Match object:
  211. ```python
  212. <str> = <Match>.group() # Whole match.
  213. <str> = <Match>.group(1) # Part in first bracket.
  214. <int> = <Match>.start() # Start index of a match.
  215. <int> = <Match>.end() # Exclusive end index of a match.
  216. ```
  217. #### Special sequences:
  218. ```python
  219. # Use capital letter for negation.
  220. '\d' == '[0-9]' # Digit
  221. '\s' == '[ \t\n\r\f\v]' # Whitespace
  222. '\w' == '[a-zA-Z0-9_]' # Alphanumeric
  223. ```
  224. ### Format
  225. ```python
  226. <str> = '{}, {}'.format(<el_1>, <el_2>)
  227. <str> = f'{<el_1>}, {<el_2}'
  228. ```
  229. #### General options:
  230. ```python
  231. {<el>:<10} # '<el> '
  232. {<el>:>10} # ' <el>'
  233. {<el>:^10} # ' <el> '
  234. {<el>:-^10} # '---<el>---'
  235. {<el>:^0} # '<el>'
  236. ```
  237. #### Options specific to strings:
  238. ```python
  239. {'abcde':.3} # 'abc'
  240. {'abcde':>10.3}' # ' abc'
  241. ```
  242. #### Options specific to numbers:
  243. ```python
  244. {1.23456:.3f} # '1.235'
  245. {1.23456:>10.3f} # ' 1.235'
  246. {123456:>10,} # ' 123,456'
  247. {123456:>10_} # ' 123_456'
  248. {3:08b} # '00000011' -> Binary with leading zeros.
  249. ````
  250. #### Example:
  251. ```python
  252. >>> person = {'name': 'Jean-Luc', 'height': 187.1}
  253. >>> '{p[height]:.0f}'.format(p=person)
  254. '187'
  255. >>> f"{person['height']:.0f}"
  256. '187'
  257. ```
  258. #### Float presentation types:
  259. * `'f'` - Fixed point: `.<precision>f`
  260. * `'e'` - Exponent
  261. #### Integer presentation types:
  262. * `'c'` - Character
  263. * `'b'` - Binary
  264. * `'x'` - Hex
  265. * `'X'` - HEX
  266. Numbers
  267. -------
  268. ### Basic Functions
  269. ```python
  270. round(<num> [, ndigits])
  271. abs(<num>)
  272. math.pow(x, y) # Or: x ** y
  273. ```
  274. ### Constants
  275. ```python
  276. from math import e, pi
  277. ```
  278. ### Trigonometry
  279. ```python
  280. from math import cos, acos, sin, asin, tan, atan, degrees, radians
  281. ```
  282. ### Logarithm
  283. ```python
  284. from math import log, log10, log2
  285. log(x [, base]) # Base e, if not specified.
  286. log10(x) # Base 10.
  287. log2(x) # Base 2.
  288. ```
  289. ### Infinity, nan
  290. ```python
  291. from math import inf, nan, isfinite, isinf, isnan
  292. ```
  293. #### Or:
  294. ```python
  295. float('inf'), float('nan')
  296. ```
  297. ### Random
  298. ```python
  299. import random
  300. <float> = random.random()
  301. <int> = random.randint(from_inclusive, to_inclusive)
  302. <el> = random.choice(<list>)
  303. random.shuffle(<list>)
  304. ```
  305. Datetime
  306. --------
  307. ```python
  308. import datetime
  309. now = datetime.datetime.now()
  310. now.month # 3
  311. now.strftime('%Y%m%d') # '20180315'
  312. now.strftime('%Y%m%d%H%M%S') # '20180315002834'
  313. ```
  314. ```python
  315. <datetime> = datetime.strptime('2015-05-12 00:39', '%Y-%m-%d %H:%M')
  316. ```
  317. Arguments
  318. ---------
  319. **"*" is the splat operator, that takes a list as input, and expands it into actual positional arguments in the function call:**
  320. ```python
  321. args = (1, 2)
  322. kwargs = {'x': 3, 'y': 4, 'z': 5}
  323. func(*args, **kwargs)
  324. ```
  325. #### Is the same as:
  326. ```python
  327. func(1, 2, x=3, y=4, z=5)
  328. ```
  329. #### Splat operator can also be used in function declarations:
  330. ```python
  331. def add(*a):
  332. return sum(a)
  333. ```
  334. ```python
  335. >>> add(1, 2, 3)
  336. 6
  337. ```
  338. #### And in some other places:
  339. ```python
  340. >>> a = (1, 2, 3)
  341. >>> [*a]
  342. [1, 2, 3]
  343. ```
  344. ```python
  345. >>> head, *body, tail = [1, 2, 3, 4]
  346. >>> body
  347. [2, 3]
  348. ```
  349. Inline
  350. ------
  351. ### Lambda
  352. ```python
  353. lambda: <return_value>
  354. lambda <argument_1>, <argument_2>: <return_value>
  355. ```
  356. ### Comprehension
  357. ```python
  358. [i+1 for i in range(10)] # [1, 2, ..., 10]
  359. [i for i in range(10) if i>5] # [6, 7, ..., 9]
  360. {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18}
  361. (x+5 for x in range(0, 10)) # (5, 6, ..., 14) -> Iterator
  362. ```
  363. ```python
  364. [i+j for i in range(10) for j in range(10)]
  365. ```
  366. #### Is the same as:
  367. ```python
  368. out = []
  369. for i in range(10):
  370. for j in range(10):
  371. out.append(i+j)
  372. ```
  373. ### Map, Filter, Reduce
  374. ```python
  375. map(lambda x: x+1, range(10)) # [1, 2, ..., 10]
  376. filter(lambda x: x>5, range(10)) # [6, 7, ..., 9]
  377. functools.reduce(lambda sum, x: sum+x, range(10)) # 45
  378. ```
  379. ### Any, All
  380. ```python
  381. <bool> = any(el[1] for el in <collection>)
  382. ```
  383. ### If - Else
  384. ```python
  385. <expression_if_true> if <condition> else <expression_if_false>
  386. ```
  387. ```python
  388. >>> [a if a else 2 for a in [0, 1, 0, 3]]
  389. [2, 1, 2, 3]
  390. ```
  391. ### Namedtuple, Enum, Class
  392. ```python
  393. from collections import namedtuple
  394. Point = namedtuple('Point', 'x y')
  395. from enum import Enum
  396. Direction = Enum('Direction', 'n e s w')
  397. Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3})
  398. # Warrning: Objects will share the objects that are initialized in the dict!
  399. Creature = type('Creature', (), {'position': Point(0, 0), 'direction': Direction.n})
  400. ```
  401. Closure
  402. -------
  403. ```python
  404. def multiply_closure(x):
  405. def wrapped(y):
  406. return x * y
  407. return wrapped
  408. multiply_by_3 = multiply_closure(3)
  409. ```
  410. #### Or:
  411. ```python
  412. from functools import partial
  413. partial(<function>, <arg_1> [, <arg_2>, ...])
  414. ```
  415. Decorator
  416. ---------
  417. ```python
  418. @closure_name
  419. def function_that_gets_passed_to_closure():
  420. pass
  421. ```
  422. #### Debugger example:
  423. ```python
  424. from functools import wraps
  425. def debug(func):
  426. @wraps(func) # Needed for metadata copying (func name, ...).
  427. def wrapper(*args, **kwargs):
  428. print(func.__name__)
  429. return func(*args, **kwargs)
  430. return wrapper
  431. @debug
  432. def add(x, y):
  433. return x + y
  434. ```
  435. Class
  436. -----
  437. ```python
  438. class <name>:
  439. def __init__(self, a):
  440. self.a = a
  441. def __str__(self):
  442. return str(self.a)
  443. def __repr__(self):
  444. return str({'a': self.a}) # Or: return f'{self.__dict__}'
  445. @classmethod
  446. def get_class_name(cls):
  447. return cls.__name__
  448. ```
  449. ### Constructor Overloading
  450. ```python
  451. class <name>:
  452. def __init__(self, a=None):
  453. self.a = a
  454. ```
  455. ### Copy
  456. ```python
  457. import copy
  458. <object> = copy.copy(<object>)
  459. <object> = copy.deepcopy(<object>)
  460. ```
  461. Enum
  462. ----
  463. ```python
  464. from enum import Enum, auto
  465. class <enum_name>(Enum):
  466. <member_name_1> = <value_1>
  467. <member_name_2> = <value_2>, <value_2b>
  468. <member_name_3> = auto() # Can be used for automatic indexing.
  469. ...
  470. @classmethod
  471. def get_names(cls):
  472. return [a.name for a in cls.__members__.values()]
  473. @classmethod
  474. def get_values(cls):
  475. return [a.value for a in cls.__members__.values()]
  476. ```
  477. ```python
  478. <member> = <enum>.<member_name>
  479. <member> = <enum>['<member_name>']
  480. <member> = <enum>(<value>)
  481. <name> = <member>.name
  482. <value> = <member>.value
  483. ```
  484. ```python
  485. list(<enum>) # == [<member_1>, <member_2>, ...]
  486. [a.name for a in <enum>] # == ['<member_name_1>', '<member_name_2>', ...]
  487. random.choice(list(<enum>)) # == random <member>
  488. ```
  489. ### Inline
  490. ```python
  491. Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon'])
  492. Cutlery = Enum('Cutlery', 'knife fork spoon')
  493. Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3})
  494. # Functions can not be values, so they must be enclosed in tuple:
  495. LogicOp = Enum('LogicOp', {'AND': (lambda l, r: l and r, ),
  496. 'OR' : (lambda l, r: l or r, )}
  497. # But 'list(<enum>)' will only work if there is another value in the tuple:
  498. LogicOp = Enum('LogicOp', {'AND': (auto(), lambda l, r: l and r),
  499. 'OR' : (auto(), lambda l, r: l or r)}
  500. ```
  501. System
  502. ------
  503. ### Arguments
  504. ```python
  505. import sys
  506. script_name = sys.argv[0]
  507. arguments = sys.argv[1:]
  508. ```
  509. ### Read File
  510. ```python
  511. def read_file(filename):
  512. with open(filename, encoding='utf-8') as file:
  513. return file.readlines()
  514. ```
  515. ### Write to File
  516. ```python
  517. def write_to_file(filename, text):
  518. with open(filename, 'w', encoding='utf-8') as file:
  519. file.write(text)
  520. ```
  521. ### Path
  522. ```python
  523. import os
  524. <bool> = os.path.exists(<path>)
  525. <bool> = os.path.isfile(<path>)
  526. <bool> = os.path.isdir(<path>)
  527. <list> = os.listdir(<path>)
  528. ```
  529. ### Execute Command
  530. ```python
  531. import os
  532. <str> = os.popen(<command>).read()
  533. ```
  534. #### Or:
  535. ```python
  536. >>> import subprocess
  537. >>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE)
  538. >>> a.stdout
  539. b'.\n..\nfile1.txt\nfile2.txt\n'
  540. >>> a.returncode
  541. 0
  542. ```
  543. ### Input
  544. ```python
  545. filename = input('Enter a file name: ')
  546. ```
  547. #### Prints lines until EOF:
  548. ```python
  549. while True:
  550. try:
  551. print(input())
  552. except EOFError:
  553. break
  554. ```
  555. ### Recursion Limit
  556. ```python
  557. >>> sys.getrecursionlimit()
  558. 1000
  559. >>> sys.setrecursionlimit(10000)
  560. ```
  561. JSON
  562. ----
  563. ```python
  564. import json
  565. ```
  566. ### Serialization
  567. ```python
  568. <str> = json.dumps(<object>, ensure_ascii=True, indent=None)
  569. <dict> = json.loads(<str>)
  570. ```
  571. #### To preserve order:
  572. ```python
  573. from collections import OrderedDict
  574. <dict> = json.loads(<str>, object_pairs_hook=OrderedDict)
  575. ```
  576. ### Read File
  577. ```python
  578. def read_json_file(filename):
  579. with open(filename, encoding='utf-8') as file:
  580. return json.load(file)
  581. ```
  582. ### Write to File
  583. ```python
  584. def write_to_json_file(filename, an_object):
  585. with open(filename, 'w', encoding='utf-8') as file:
  586. json.dump(an_object, file, ensure_ascii=False, indent=2)
  587. ```
  588. SQLite
  589. ------
  590. ```python
  591. import sqlite3
  592. db = sqlite3.connect(<filename>)
  593. ```
  594. ### Read
  595. ```python
  596. cursor = db.execute(<query>)
  597. if cursor:
  598. cursor.fetchall() # Or cursor.fetchone()
  599. db.close()
  600. ```
  601. ### Write
  602. ```python
  603. db.execute(<query>)
  604. db.commit()
  605. ```
  606. Exceptions
  607. ----------
  608. ```python
  609. while True:
  610. try:
  611. x = int(input('Please enter a number: '))
  612. except ValueError:
  613. print('Oops! That was no valid number. Try again...')
  614. else:
  615. print('Thank you.')
  616. break
  617. ```
  618. #### Raise exception
  619. ```python
  620. raise IOError("input/output error")
  621. ```
  622. Bytes
  623. -----
  624. **Bytes objects are immutable sequences of single bytes.**
  625. ### Encode
  626. ```python
  627. <Bytes> = b'<str>'
  628. <Bytes> = <str>.encode(encoding='utf-8')
  629. <Bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
  630. <Bytes> = bytes.fromhex(<hex>)
  631. ```
  632. ### Decode
  633. ```python
  634. <str> = <Bytes>.decode('utf-8')
  635. <int> = int.from_bytes(<Bytes>, byteorder='big|little', signed=False)
  636. <hex> = <Bytes>.hex()
  637. ```
  638. ### Read Bytes from File
  639. ```python
  640. def read_bytes(filename):
  641. with open(filename, 'rb') as file:
  642. return file.read()
  643. ```
  644. ### Write Bytes to File
  645. ```python
  646. def write_bytes(filename, bytes):
  647. with open(filename, 'wb') as file:
  648. file.write(bytes)
  649. ```
  650. ```python
  651. <Bytes> = b''.join(<list_of_Bytes>)
  652. ```
  653. Struct
  654. ------
  655. **This module performs conversions between Python values and C structs represented as Python Bytes objects:**
  656. ```python
  657. <Bytes> = struct.pack('<format>', <value_1> [, <value_2>, ...])
  658. <tuple> = struct.unpack('<format>', <Bytes>)
  659. ```
  660. ### Example
  661. ```python
  662. >>> from struct import pack, unpack, calcsize
  663. >>> pack('hhl', 1, 2, 3)
  664. b'\x00\x01\x00\x02\x00\x00\x00\x03'
  665. >>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
  666. (1, 2, 3)
  667. >>> calcsize('hhl')
  668. 8
  669. ```
  670. ### Format
  671. **Use capital leters for unsigned type.**
  672. * `'x'` - pad byte
  673. * `'c'` - char
  674. * `'h'` - short
  675. * `'i'` - int
  676. * `'l'` - long
  677. * `'q'` - long long
  678. * `'f'` - float
  679. * `'d'` - double
  680. Hashlib
  681. -------
  682. ```python
  683. >>> hashlib.md5(<str>.encode()).hexdigest()
  684. '33d0eba106da4d3ebca17fcd3f4c3d77'
  685. ```
  686. Threading
  687. ---------
  688. ```python
  689. import threading
  690. ```
  691. ### Thread
  692. ```python
  693. thread = threading.Thread(target=<function>, args=(<first_arg>, ))
  694. thread.start()
  695. ...
  696. thread.join()
  697. ```
  698. ### Lock
  699. ```python
  700. lock = threading.Rlock()
  701. lock.acquire()
  702. ...
  703. lock.release()
  704. ```
  705. Itertools
  706. ---------
  707. **Every function returns an iterator and can accept any collection and/or iterator. If you want to print the iterator, you need to pass it to the list() function.**
  708. ```python
  709. from itertools import *
  710. ```
  711. ### Combinatoric iterators
  712. ```python
  713. >>> combinations('abc', 2)
  714. [('a', 'b'), ('a', 'c'), ('b', 'c')]
  715. >>> combinations_with_replacement('abc', 2)
  716. [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
  717. >>> permutations('abc', 2)
  718. [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
  719. >>> product('ab', [1, 2])
  720. [('a', 1), ('a', 2), ('b', 1), ('b', 2)]
  721. >>> product([0, 1], repeat=3)
  722. [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
  723. ```
  724. ### Infinite iterators
  725. ```python
  726. >>> i = count(5, 2)
  727. >>> next(i), next(i), next(i)
  728. (5, 7, 9)
  729. >>> a = cycle('abc')
  730. >>> [next(a) for _ in range(10)]
  731. ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a']
  732. >>> repeat(10, 3)
  733. [10, 10, 10]
  734. ```
  735. ### Iterators
  736. ```python
  737. >>> chain([1, 2], range(3, 5))
  738. [1, 2, 3, 4]
  739. >>> compress('abc', [True, 0, 1])
  740. ['a', 'c']
  741. >>> islice([1, 2, 3], 1, None) # islice(<seq>, from_inclusive, to_exclusive)
  742. [2, 3]
  743. >>> people = [{'id': 1, 'name': 'bob'},
  744. {'id': 2, 'name': 'bob'},
  745. {'id': 3, 'name': 'peter'}]
  746. >>> {k: list(v) for k, v in groupby(people, key=lambda x: x['name'])}
  747. {'bob': [{'id': 1, 'name': 'bob'},
  748. {'id': 2, 'name': 'bob'}],
  749. 'peter': [{'id': 3, 'name': 'peter'}]}
  750. ```
  751. Introspection and Metaprograming
  752. --------------------------------
  753. **Inspecting code at runtime and code that generates code. You can:**
  754. * **Look at the attributes**
  755. * **Set new attributes**
  756. * **Create functions dynamically**
  757. * **Traverse the parent classes**
  758. * **Change values in the class**
  759. ### Variables
  760. ```python
  761. <list> = dir() # In-scope variables.
  762. <dict> = locals() # Local variables.
  763. <dict> = globals() # Global variables.
  764. ```
  765. ### Attributes
  766. ```python
  767. >>> class Z:
  768. ... def __init__(self):
  769. ... self.a = 'abcde'
  770. ... self.b = 12345
  771. >>> z = Z()
  772. ```
  773. ```python
  774. >>> getattr(z, 'a') # Same as Z.__getattribute__(z, 'a')
  775. 'abcde'
  776. >>> hasattr(z, 'c')
  777. False
  778. >>> setattr(z, 'c', 10)
  779. ```
  780. ### Parameters
  781. #### Getting the number of parameters of a function:
  782. ```python
  783. from inspect import signature
  784. sig = signature(<function>)
  785. no_of_params = len(sig.parameters)
  786. ```
  787. ### Type
  788. **Type is the root class. If only passed the object it returns it's type. Otherwise it creates a new class (and not the instance!):**
  789. ```python
  790. type(<class_name>, <parents_tuple>, <attributes_dict>)
  791. ```
  792. ```python
  793. >>> Z = type('Z', (), {'a': 'abcde', 'b': 12345})
  794. >>> z = Z()
  795. ```
  796. ### MetaClass
  797. #### Class that creates class:
  798. ```python
  799. def my_meta_class(name, parents, attrs):
  800. ...
  801. return type(name, parents, attrs)
  802. ```
  803. #### Or:
  804. ```python
  805. class MyMetaClass(type):
  806. def __new__(klass, name, parents, attrs):
  807. ...
  808. return type.__new__(klass, name, parents, attrs)
  809. ```
  810. ### Metaclass Attribute
  811. **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:**
  812. ```python
  813. class BlaBla:
  814. __metaclass__ = Bla
  815. ```
  816. Eval
  817. ----
  818. ### Basic
  819. ```python
  820. >>> import ast
  821. >>> ast.literal_eval('1 + 1')
  822. 2
  823. >>> ast.literal_eval('[1, 2, 3]')
  824. [1, 2, 3]
  825. ```
  826. ### Detailed
  827. ```python
  828. import ast
  829. import operator as op
  830. # Supported operators
  831. operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
  832. ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor,
  833. ast.USub: op.neg}
  834. def eval_expr(expr):
  835. return eval_(ast.parse(expr, mode='eval').body)
  836. def eval_(node):
  837. if isinstance(node, ast.Num): # <number>
  838. return node.n
  839. elif isinstance(node, ast.BinOp): # <left> <operator> <right>
  840. return operators[type(node.op)](eval_(node.left), eval_(node.right))
  841. elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
  842. return operators[type(node.op)](eval_(node.operand))
  843. else:
  844. raise TypeError(node)
  845. ```
  846. ```python
  847. >>> eval_expr('2^6')
  848. 4
  849. >>> eval_expr('2**6')
  850. 64
  851. >>> eval_expr('1 + 2*3**(4^5) / (6 + -7)')
  852. -5.0
  853. ```
  854. Coroutine
  855. ---------
  856. * **Similar to Generator, but Generator pulls data through the pipe with iteration, while Coroutine pushes data into the pipeline with send().**
  857. * **Coroutines provide more powerful data routing possibilities than iterators.**
  858. * **If you built a collection of simple data processing components, you can glue them together into complex arrangements of pipes, branches, merging, etc.**
  859. ### Helper Decorator
  860. * **All coroutines must be "primed" by first calling .next()**
  861. * **Remembering to call .next() is easy to forget.**
  862. * **Solved by wrapping coroutines with a decorator:**
  863. ```python
  864. def coroutine(func):
  865. def start(*args, **kwargs):
  866. cr = func(*args, **kwargs)
  867. next(cr)
  868. return cr
  869. return start
  870. ```
  871. ### Pipeline Example
  872. ```python
  873. def reader(target):
  874. for i in range(10):
  875. target.send(i)
  876. target.close()
  877. @coroutine
  878. def adder(target):
  879. while True:
  880. item = (yield)
  881. target.send(item + 100)
  882. @coroutine
  883. def printer():
  884. while True:
  885. item = (yield)
  886. print(item)
  887. reader(adder(printer()))
  888. ```
  889. <br><br>
  890. Libraries
  891. =========
  892. Plot
  893. ----
  894. ```python
  895. # $ pip3 install matplotlib
  896. from matplotlib import pyplot
  897. pyplot.plot(<data_1> [, <data_2>, ...])
  898. pyplot.show()
  899. pyplot.savefig(<filename>, transparent=True)
  900. ```
  901. Table
  902. -----
  903. #### Prints CSV file as ASCII table:
  904. ```python
  905. # $ pip3 install tabulate
  906. import csv
  907. from tabulate import tabulate
  908. with open(<filename>, newline='') as csv_file:
  909. reader = csv.reader(csv_file, delimiter=';')
  910. headers = [a.title() for a in next(reader)]
  911. print(tabulate(reader, headers))
  912. ```
  913. Curses
  914. ------
  915. ```python
  916. # $ pip3 install curses
  917. import curses
  918. def main():
  919. curses.wrapper(draw)
  920. def draw(screen):
  921. screen.clear()
  922. screen.addstr(0, 0, 'Press ESC to quit.')
  923. while screen.getch() != 27:
  924. pass
  925. def get_border(screen):
  926. from collections import namedtuple
  927. P = namedtuple('P', 'x y')
  928. height, width = screen.getmaxyx()
  929. return P(width - 1, height - 1)
  930. ```
  931. Image
  932. -----
  933. #### Creates PNG image of greyscale gradient:
  934. ```python
  935. # $ pip3 install pillow
  936. from PIL import Image
  937. width, height = 100, 100
  938. img = Image.new('L', (width, height), 'white')
  939. img.putdata([255*a/(width*height) for a in range(width*height)])
  940. img.save('out.png')
  941. ```
  942. ### Modes
  943. * `'1'` - 1-bit pixels, black and white, stored with one pixel per byte.
  944. * `'L'` - 8-bit pixels, greyscale.
  945. * `'RGB'` - 3x8-bit pixels, true color.
  946. * `'RGBA'` - 4x8-bit pixels, true color with transparency mask.
  947. * `'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.
  948. Audio
  949. -----
  950. #### Saves list of floats with values between 0 and 1 to a WAV file:
  951. ```python
  952. import wave, struct
  953. frames = [struct.pack('h', int((a-0.5)*60000)) for a in <list>]
  954. wf = wave.open(<filename>, 'wb')
  955. wf.setnchannels(1)
  956. wf.setsampwidth(4)
  957. wf.setframerate(44100)
  958. wf.writeframes(b''.join(frames))
  959. wf.close()
  960. ```
  961. UrlLib
  962. ------
  963. ### Translate special characters
  964. ```python
  965. import urllib.parse
  966. <str> = urllib.parse.quote_plus(<str>)
  967. ```
  968. Web
  969. ---
  970. ```python
  971. # $ pip3 install bottle
  972. import bottle
  973. import urllib
  974. ```
  975. ### Run
  976. ```python
  977. bottle.run(host='localhost', port=8080)
  978. bottle.run(host='0.0.0.0', port=80, server='cherrypy')
  979. ```
  980. ### Static request
  981. ```python
  982. @route('/img/<image>')
  983. def send_image(image):
  984. return static_file(image, 'images/', mimetype='image/png')
  985. ```
  986. ### Dynamic request
  987. ```python
  988. @route('/<sport>')
  989. def send_page(sport):
  990. sport = urllib.parse.unquote(sport).lower()
  991. page = read_file(sport)
  992. return template(page)
  993. ```
  994. ### REST request
  995. ```python
  996. @post('/p/<sport>')
  997. def p_handler(sport):
  998. team = bottle.request.forms.get('team')
  999. team = urllib.parse.unquote(team).lower()
  1000. db = sqlite3.connect(<db_path>)
  1001. home_odds, away_odds = get_p(db, sport, team)
  1002. db.close()
  1003. response.headers['Content-Type'] = 'application/json'
  1004. response.headers['Cache-Control'] = 'no-cache'
  1005. return json.dumps([home_odds, away_odds])
  1006. ```
  1007. Profile
  1008. -------
  1009. #### Basic:
  1010. ```python
  1011. from time import time
  1012. start_time = time()
  1013. ...
  1014. duration = time() - start_time
  1015. ```
  1016. #### Times execution of the passed code:
  1017. ```python
  1018. from timeit import timeit
  1019. timeit('"-".join(str(n) for n in range(100))', number=10000, globals=globals())
  1020. ```
  1021. #### Generates a PNG image of call graph and highlights the bottlenecks:
  1022. ```python
  1023. # $ pip3 install pycallgraph
  1024. import pycallgraph
  1025. graph = pycallgraph.output.GraphvizOutput()
  1026. graph.output_file = get_filename()
  1027. with pycallgraph.PyCallGraph(output=graph):
  1028. <code_to_be_profiled>
  1029. ```
  1030. #### Utility code for unique PNG filenames:
  1031. ```python
  1032. def get_filename():
  1033. time_str = get_current_datetime_string()
  1034. return f'profile-{time_str}.png'
  1035. def get_current_datetime_string():
  1036. now = datetime.datetime.now()
  1037. return get_datetime_string(now)
  1038. def get_datetime_string(a_datetime):
  1039. return a_datetime.strftime('%Y%m%d%H%M%S')
  1040. ```
  1041. Progress Bar
  1042. ------------
  1043. ### Basic:
  1044. ```python
  1045. import sys
  1046. class Bar():
  1047. @staticmethod
  1048. def range(*args):
  1049. bar = Bar(len(list(range(*args))))
  1050. for i in range(*args):
  1051. yield i
  1052. bar.tick()
  1053. @staticmethod
  1054. def foreach(elements):
  1055. bar = Bar(len(elements))
  1056. for el in elements:
  1057. yield el
  1058. bar.tick()
  1059. def __init__(s, steps, width=40):
  1060. s.st, s.wi, s.fl, s.i = steps, width, 0, 0
  1061. s.th = s.fl * s.st / s.wi
  1062. s.p(f"[{' ' * s.wi}]")
  1063. s.p('\b' * (s.wi + 1))
  1064. def tick(s):
  1065. s.i += 1
  1066. while s.i > s.th:
  1067. s.fl += 1
  1068. s.th = s.fl * s.st / s.wi
  1069. s.p('-')
  1070. if s.i == s.st:
  1071. s.p('\n')
  1072. def p(s, t):
  1073. sys.stdout.write(t)
  1074. sys.stdout.flush()
  1075. ```
  1076. #### Usage:
  1077. ```python
  1078. from time import sleep
  1079. # Range:
  1080. for i in Bar.range(100):
  1081. sleep(0.02)
  1082. # Foreach:
  1083. for el in Bar.foreach(['a', 'b', 'c']):
  1084. sleep(0.02)
  1085. ```
  1086. ### Progress:
  1087. ```python
  1088. # $ pip3 install progress
  1089. from progress.bar import Bar
  1090. from time import sleep
  1091. STEPS = 100
  1092. bar = Bar('Processing', max=STEPS)
  1093. for i in range(STEPS):
  1094. sleep(0.02)
  1095. bar.next()
  1096. bar.finish()
  1097. ```
  1098. Basic Script Template
  1099. ---------------------
  1100. ```python
  1101. # Linux:
  1102. #!/usr/bin/env python3
  1103. # Mac:
  1104. #!/usr/local/bin/python3
  1105. #
  1106. # Usage: .py
  1107. #
  1108. from collections import namedtuple
  1109. from enum import Enum
  1110. import re
  1111. import sys
  1112. def main():
  1113. pass
  1114. ###
  1115. ## UTIL
  1116. #
  1117. def read_file(filename):
  1118. with open(filename, encoding='utf-8') as file:
  1119. return file.readlines()
  1120. if __name__ == '__main__':
  1121. main()
  1122. ```