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.

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