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.

1242 lines
25 KiB

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