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.

1342 lines
27 KiB

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