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.

1244 lines
25 KiB

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