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.

1320 lines
26 KiB

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