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
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
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
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 = 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. <dict>.keys()
  41. <dict>.values()
  42. <dict>.items()
  43. <dict>.get(key, default)
  44. <dict>.setdefault(key, default)
  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>.union(<set>)
  68. <set>.intersection(<set>)
  69. <set>.difference(<set>)
  70. <set>.issubset(<set>)
  71. <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 __repr__(self):
  395. return str({'a': self.a})
  396. # Use f'{s.__dict__}' for all members.
  397. def __str__(self):
  398. return str(self.a)
  399. @classmethod
  400. def get_class_name(cls):
  401. return cls.__name__
  402. ```
  403. ### Enum
  404. ```python
  405. from enum import Enum, auto
  406. class <enum_name>(Enum):
  407. <member_name_1> = <value_1>
  408. <member_name_2> = <value_2>, <value_2b>
  409. <member_name_3> = auto() # Can be used for automatic indexing.
  410. ...
  411. @classmethod
  412. def get_names(cls):
  413. return [a.name for a in cls.__members__.values()]
  414. @classmethod
  415. def get_values(cls):
  416. return [a.value for a in cls.__members__.values()]
  417. ```
  418. ```python
  419. <member> = <enum>.<member_name>
  420. <member> = <enum>['<member_name>']
  421. <member> = <enum>(<value>)
  422. <name> = <member>.name
  423. <value> = <member>.value
  424. ```
  425. ```python
  426. list(<enum>) # == [<member_1>, <member_2>, ...]
  427. list(a.name for a in <enum>) # == ['<member_name_1>', '<member_name_2>', ...]
  428. random.choice(list(<enum>)) # == random <member>
  429. ```
  430. #### Inline:
  431. ```python
  432. Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon'])
  433. Cutlery = Enum('Cutlery', 'knife fork spoon')
  434. Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3})
  435. # Functions can not be values, so they must be enclosed in tuple:
  436. LogicOp = Enum('LogicOp', {'AND': (lambda l, r: l and r, ),
  437. 'OR' : (lambda l, r: l or r, )}
  438. ```
  439. ### Copy
  440. ```python
  441. import copy
  442. <object> = copy.copy(<object>)
  443. <object> = copy.deepcopy(<object>)
  444. ```
  445. System
  446. ------
  447. ### Arguments
  448. ```python
  449. import sys
  450. script_name = sys.argv[0]
  451. arguments = sys.argv[1:]
  452. ```
  453. ### Read File
  454. ```python
  455. def read_file(filename):
  456. with open(filename, encoding='utf-8') as file:
  457. return file.readlines()
  458. ```
  459. ### Write to File
  460. ```python
  461. def write_to_file(filename, text):
  462. with open(filename, 'w', encoding='utf-8') as file:
  463. file.write(text)
  464. ```
  465. ### Path
  466. ```python
  467. import os
  468. os.path.exists(<path>)
  469. os.path.isfile(<path>)
  470. os.path.isdir(<path>)
  471. os.listdir(<path>)
  472. ```
  473. ### Execute Command
  474. ```python
  475. import os
  476. os.popen(<command>).read()
  477. ```
  478. #### Or:
  479. ```python
  480. >>> import subprocess
  481. >>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE)
  482. >>> a.stdout
  483. b'.\n..\nfile1.txt\nfile2.txt\n'
  484. >>> a.returncode
  485. 0
  486. ```
  487. ### Input
  488. ```python
  489. filename = input('Enter a file name: ')
  490. ```
  491. #### Prints lines until EOF:
  492. ```python
  493. while True:
  494. try:
  495. print(input())
  496. except EOFError:
  497. break
  498. ```
  499. JSON
  500. ----
  501. ```python
  502. import json
  503. ```
  504. ### Serialization
  505. ```python
  506. <str> = json.dumps(<object>, ensure_ascii=True, indent=None)
  507. <dict> = json.loads(<str>)
  508. ```
  509. #### To preserve order:
  510. ```python
  511. from collections import OrderedDict
  512. <dict> = json.loads(<str>, object_pairs_hook=OrderedDict)
  513. ```
  514. ### Read File
  515. ```python
  516. def read_json_file(filename):
  517. with open(filename, encoding='utf-8') as file:
  518. return json.load(file)
  519. ```
  520. ### Write to File
  521. ```python
  522. def write_to_json_file(filename, an_object):
  523. with open(filename, 'w', encoding='utf-8') as file:
  524. json.dump(an_object, file, ensure_ascii=False, indent=2)
  525. ```
  526. SQLite
  527. ------
  528. ```python
  529. import sqlite3
  530. db = sqlite3.connect(<filename>)
  531. ```
  532. ### Read
  533. ```python
  534. cursor = db.execute(<query>)
  535. if cursor:
  536. cursor.fetchall() # Or cursor.fetchone()
  537. db.close()
  538. ```
  539. ### Write
  540. ```python
  541. db.execute(<query>)
  542. db.commit()
  543. ```
  544. Exceptions
  545. ----------
  546. ```python
  547. while True:
  548. try:
  549. x = int(input('Please enter a number: '))
  550. except ValueError:
  551. print('Oops! That was no valid number. Try again...')
  552. else:
  553. print('Thank you.')
  554. break
  555. ```
  556. #### Raise exception
  557. ```python
  558. raise IOError("input/output error")
  559. ```
  560. Bytes
  561. -----
  562. **Bytes objects are immutable sequences of single bytes.**
  563. ### Encode
  564. ```python
  565. <Bytes> = b'<str>'
  566. <Bytes> = <str>.encode(encoding='utf-8')
  567. <Bytes> = <int>.to_bytes(<length>, byteorder='big|little', signed=False)
  568. <Bytes> = bytes.fromhex(<hex>)
  569. ```
  570. ### Decode
  571. ```python
  572. <str> = <Bytes>.decode('utf-8')
  573. <int> = int.from_bytes(<Bytes>, byteorder='big|little', signed=False)
  574. <hex> = <Bytes>.hex()
  575. ```
  576. ### Read Bytes from File
  577. ```python
  578. def read_bytes(filename):
  579. with open(filename, 'rb') as file:
  580. return file.read()
  581. ```
  582. ### Write Bytes to File
  583. ```python
  584. def write_bytes(filename, bytes):
  585. with open(filename, 'wb') as file:
  586. file.write(bytes)
  587. ```
  588. ```python
  589. <Bytes> = b''.join(<list_of_Bytes>)
  590. ```
  591. Struct
  592. ------
  593. **This module performs conversions between Python values and C structs represented as Python bytes objects:**
  594. ```python
  595. <Bytes> = struct.pack('<format>', <value_1> [, <value_2>, ...])
  596. <tuple> = struct.unpack('<format>', <Bytes>)
  597. ```
  598. ### Example
  599. ```python
  600. >>> from struct import pack, unpack, calcsize
  601. >>> pack('hhl', 1, 2, 3)
  602. b'\x00\x01\x00\x02\x00\x00\x00\x03'
  603. >>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
  604. (1, 2, 3)
  605. >>> calcsize('hhl')
  606. 8
  607. ```
  608. ### Format
  609. **Use capital leters for unsigned type.**
  610. * `x` - pad byte
  611. * `c` - char
  612. * `h` - short
  613. * `i` - int
  614. * `l` - long
  615. * `q` - long long
  616. * `f` - float
  617. * `d` - double
  618. Hashlib
  619. -------
  620. ```python
  621. >>> hashlib.md5(b'<str>').hexdigest()
  622. '33d0eba106da4d3ebca17fcd3f4c3d77'
  623. ```
  624. Threading
  625. ---------
  626. ```python
  627. import threading
  628. ```
  629. ### Thread
  630. ```python
  631. thread = threading.Thread(target=<function>, args=(<first_arg>, ))
  632. thread.start()
  633. thread.join()
  634. ```
  635. ### Lock
  636. ```python
  637. lock = threading.Rlock()
  638. lock.acquire()
  639. lock.release()
  640. ```
  641. Itertools
  642. ---------
  643. **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.**
  644. ```python
  645. from itertools import *
  646. ```
  647. ### Combinatoric iterators
  648. ```python
  649. >>> combinations('abc', 2)
  650. [('a', 'b'), ('a', 'c'), ('b', 'c')]
  651. >>> ombinations_with_replacement('abc', 2)
  652. [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
  653. >>> permutations('abc', 2)
  654. [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
  655. >>> product('ab', [1, 2])
  656. [('a', 1), ('a', 2), ('b', 1), ('b', 2)]
  657. ```
  658. ### Infinite iterators
  659. ```python
  660. >>> i = count(5, 2)
  661. >>> next(i), next(i), next(i)
  662. (5, 7, 9)
  663. >>> a = cycle('abc')
  664. >>> [next(a) for _ in range(10)]
  665. ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a']
  666. >>> repeat(10, 3)
  667. [10, 10, 10]
  668. ```
  669. ### Iterators
  670. ```python
  671. >>> chain([1, 2], range(3, 5))
  672. [1, 2, 3, 4]
  673. >>> compress('abc', [True, 0, 1])
  674. ['a', 'c']
  675. >>> islice([1, 2, 3], 1, None) # islice(<seq>, from_inclusive, to_exclusive)
  676. [2, 3]
  677. >>> a = [{'id': 1, 'name': 'bob'},
  678. {'id': 2, 'name': 'bob'},
  679. {'id': 3, 'name': 'peter'}]
  680. >>> {k: list(v) for k, v in groupby(a, key=lambda x: x['name'])}
  681. {'bob': [{'id': 1, 'name': 'bob'},
  682. {'id': 2, 'name': 'bob'}],
  683. 'peter': [{'id': 3, 'name': 'peter'}]}
  684. ```
  685. ### Ifilter, imap and izip
  686. #### Filter, map and zip functions that return generators instead of iterators. Dropped in Pyhon 3, because filter, map and zip functions started returning generators.
  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. ```