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.

1298 lines
26 KiB

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