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.

749 lines
12 KiB

7 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
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
7 years ago
  1. Python and Libraries Cheatsheet
  2. ===============================
  3. Main
  4. ----
  5. ```python
  6. if __name__ == '__main__':
  7. main()
  8. ```
  9. List
  10. ----
  11. ```python
  12. <list>[<inclusive from>:<exclusive to>:<step size>]
  13. <list>.extend(<list>)
  14. <list>.sort()
  15. <list>.reverse()
  16. sum(<list>)
  17. sorted_by_second = sorted(<list>, key=lambda tup: tup[1])
  18. ```
  19. Dictionary
  20. ----------
  21. ```python
  22. <dict>.items()
  23. <dict>.get(<key>, <default>)
  24. <dict>.setdefault(<key>, <default>)
  25. <dict>.update(<dict>)
  26. collections.defaultdict(<type>) # Creates list
  27. ```
  28. Init from two lists
  29. ```
  30. dict(zip(keys, values))
  31. ```
  32. Counter
  33. ```
  34. >>> from collections import Counter
  35. >>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
  36. >>> Counter(z)
  37. Counter({'blue': 3, 'red': 2, 'yellow': 1})
  38. ```
  39. Set
  40. ---
  41. ```python
  42. <set> = set()
  43. <set>.add(<el>)
  44. <set>.update(<set>)
  45. <set>.union(<set>)
  46. <set>.intersection(<set>)
  47. <set>.difference(<set>)
  48. <frozenset> - is hashable can be used as key in dictionary
  49. ```
  50. Range
  51. -----
  52. ```python
  53. range(<to exclusive>)
  54. range(<from inclusive>, <to exclusive>)
  55. range(<from inclusive>, <to exclusive>, <step size>) # Negative step for backward
  56. ```
  57. Enumerate
  58. ---------
  59. ```python
  60. for i, <el> in enumerate(<list>, [start])
  61. ```
  62. Named Tuples
  63. ------------
  64. ```
  65. >>> TestResults = collections.namedtuple('TestResults', ['filed', 'attempted'])
  66. >>> TestResults(1, 2)
  67. TestResults(filed=1, attempted=2)
  68. ```
  69. Iterator
  70. --------
  71. Reads input until it reaches empty line.
  72. ```
  73. for line in iter(input, ''):
  74. print(line)
  75. ```
  76. Use partial from functools if function needs arguments.
  77. Skips first element.
  78. ```
  79. next(<iter>)
  80. for element in <iter>:
  81. ...
  82. ```
  83. Type
  84. ----
  85. ```python
  86. type(<el>) is int/str/set/list
  87. ```
  88. ```python
  89. import numbers
  90. isinstance(<el>, numbers.Number)
  91. ```
  92. String
  93. ------
  94. ```python
  95. str.replace(<text>, <old>, <new>)
  96. <str>.isnumeric()
  97. <str>.split()
  98. <str>.strip()
  99. '<str>'.join(<list>)
  100. ```
  101. ### Print
  102. ```python
  103. print(<el1>, <el2>, end='', sep='', file=<file>)
  104. ```
  105. ### Regex
  106. ```python
  107. import re
  108. re.sub(<regex>, <new>, <text>)
  109. re.search(<regex>, <text>)
  110. ```
  111. ### Format
  112. ```python
  113. '{}'.format(<el>)
  114. ```
  115. ```python
  116. {:<min width>} -> '<el> '
  117. {:><min width>} -> ' <el>'
  118. {:^<min width>} -> ' <el> '
  119. {:_<min width>} -> '<el>____'
  120. {:.<max width>} -> '<e>'
  121. {:<max widht>.<min width>} -> ' <e>'
  122. {:<max width>.<no of decimals>f} -> ' 3.14'
  123. ```
  124. ```python
  125. >>> person = {'name': 'Jean-Luc', 'height': 187.1}
  126. >>> '{p[height]:.0f}'.format(p=person)
  127. '187'
  128. ```
  129. ### Text Wrap
  130. ```python
  131. import textwrap
  132. textwrap.wrap(<text>, <width>)
  133. ```
  134. Random
  135. ------
  136. ```python
  137. import random
  138. random.random()
  139. random.randint(<from inclusive>, <to inclusive>)
  140. random.shuffle(<list>)
  141. ```
  142. Infinity
  143. --------
  144. ```python
  145. float("inf")
  146. ```
  147. Datetime
  148. --------
  149. ```python
  150. import datetime
  151. now = datetime.datetime.now()
  152. now.strftime('%Y%m%d')
  153. now.strftime('%Y%m%d%H%M%S')
  154. ```
  155. Arguments
  156. ---------
  157. ```
  158. args = (1, 2)
  159. kwargs = {'x': 3, 'y': 4, 'z': 5}
  160. func(*args, **kwargs)
  161. # same as
  162. func(1, 2, x=3, y=4, z=5)
  163. ```
  164. * is the "splat" operator: It takes a list as input, and expands it into actual positional arguments in the function call.
  165. So if uniqueCrossTabs was [ [ 1, 2 ], [ 3, 4 ] ], then itertools.chain(*uniqueCrossTabs) is the same as saying itertools.chain([ 1, 2 ], [ 3, 4 ])
  166. Inline
  167. ------
  168. ### Lambda
  169. ```python
  170. lambda <arg1>, <arg2>: <return value>
  171. lambda: <return value>
  172. ```
  173. ### Comprehension
  174. ```python
  175. [i+1 for i in range(10)]
  176. [i for i in range(10) if i>5]
  177. {i: i*2 for i in range(10)} - dictionary
  178. (x+5 for x in range(0, 10)) - generator
  179. ```
  180. ```
  181. [i+j for i in range(10) for j in range(10)]
  182. # Same as:
  183. for i in range(10):
  184. for j in range(10):
  185. out.append(i+j)
  186. ```
  187. ### Map, Filter, Reduce
  188. ```python
  189. map(lambda x: x+1, range(10))
  190. filter(lambda x: x>5, range(10))
  191. functools.reduce(combining_function, list_of_inputs)
  192. ```
  193. ### Any, All
  194. ```
  195. any(a[1] for a in aaa)
  196. ```
  197. ### If - Else
  198. ```
  199. expression_if_true if condition else expression_if_false
  200. ```
  201. Closure
  202. -------
  203. ```python
  204. def mult_clos(x):
  205. def wrapped(y):
  206. return x * y
  207. return wrapped
  208. mul_by_3 = mult_clos(3)
  209. ```
  210. or
  211. ```
  212. from functools import partial
  213. partial(<function>, <parameter>)
  214. ```
  215. Decorator
  216. ---------
  217. ```python
  218. @closure_name
  219. def function_that_gets_passed_to_closure():
  220. ...
  221. ```
  222. Debugger example
  223. ```
  224. from functools import wraps
  225. def debug(func):
  226. @wraps(func) # Nedded for metadata copying (func name, etc)
  227. def wrapper(*args, **kwargs):
  228. print(func.__name__)
  229. return func(*args, **kwargs)
  230. return wrapper
  231. @debug
  232. def add(x, y):
  233. return x + y
  234. ```
  235. Generator
  236. ---------
  237. ```python
  238. def step(start, step):
  239. while True:
  240. yield start
  241. start += step
  242. stepper = step(10, 2)
  243. next(stepper)
  244. ```
  245. Class
  246. -----
  247. ### Class
  248. ```python
  249. class <name>:
  250. def __init__(self, <arg>):
  251. self.a = <arg>
  252. def __repr__(self):
  253. return str({'a': self.a})
  254. def __str__(self):
  255. return str(self.a)
  256. ```
  257. ### Enum
  258. ```python
  259. import enum
  260. class <enum-name>(enum.Enum):
  261. <name> = <value> # or enum.auto()
  262. <enum-name>.<name> == <enum>
  263. <enum-name>(value) == <enum>
  264. <enum>.name == <name>
  265. <enum>.value == <value>
  266. Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon'])
  267. list(<enum-name>) == [<enum1>, <enum2>, ...]
  268. random.choice(list(<enum-name>)) == random <enum>
  269. ```
  270. ### Copy
  271. ```python
  272. import copy
  273. copy.copy(<object>)
  274. copy.deepcopy(<object>)
  275. ```
  276. System
  277. ------
  278. ### Arguments
  279. ```python
  280. import sys
  281. sys.argv
  282. ```
  283. ### Read File
  284. ```python
  285. with open(<filename>, encoding='utf-8') as file:
  286. return file.readlines()
  287. ```
  288. ```
  289. def get_file_contents(file_name):
  290. with open(file_name, encoding='utf-8') as f:
  291. return f.readlines()
  292. ```
  293. ### Write to File
  294. ```python
  295. with open(<filename>, 'w', enconding='utf-8') as file:
  296. file.write(<text>)
  297. ```
  298. ### Execute Command
  299. ```python
  300. import os
  301. os.popen(<command>).read()
  302. ```
  303. ### Input
  304. ```python
  305. filename = input('Enter a file name: ')
  306. ```
  307. Print lines until EOF
  308. ```
  309. while True:
  310. try:
  311. print(input())
  312. except EOFError:
  313. break
  314. ```
  315. JSON
  316. ----
  317. ```python
  318. import json
  319. ```
  320. ### Read File
  321. ```python
  322. with open(<filename>, encoding='utf-8') as file:
  323. return json.load(file)
  324. ```
  325. ### Write to File
  326. ```python
  327. with open(<filename>, 'w', enconding='utf-8') as file:
  328. file.write(json.dumps(<object>))
  329. ```
  330. SQLite
  331. ------
  332. ```python
  333. import sqlite3
  334. db = sqlite3.connect(<filename>)
  335. ```
  336. ### Read
  337. ```python
  338. cursor = db.execute(<query>)
  339. if cursor:
  340. cursor.<fetchone/fetchall>()
  341. db.close()
  342. ```
  343. ### Write
  344. ```python
  345. db.execute(<query>)
  346. db.commit()
  347. ```
  348. Exceptions
  349. ----------
  350. ```
  351. while True:
  352. try:
  353. x = int(input("Please enter a number: "))
  354. break
  355. except ValueError:
  356. print("Oops! That was no valid number. Try again...")
  357. ```
  358. Threading
  359. ---------
  360. ```python
  361. import threading
  362. ```
  363. ### Thread
  364. ```python
  365. thread = threading.Thread(target=<function>, args=(<first arg>, ))
  366. thread.start()
  367. thread.join()
  368. ```
  369. ### Lock
  370. ```python
  371. lock = threading.Rlock()
  372. lock.acquire()
  373. lock.release()
  374. ```
  375. Itertools
  376. ---------
  377. Every function returns an generator and can accept any collection.
  378. All examples should be passed to list() to get the output.
  379. ```python
  380. from itertools import *
  381. ```
  382. ### Chain
  383. ```python
  384. >>> chain([1,2], range(3,5))
  385. [1, 2, 3, 4]
  386. ```
  387. ### Combinations
  388. ```python
  389. >>> combinations("abc", 2)
  390. [('a', 'b'), ('a', 'c'), ('b', 'c')]
  391. ```
  392. ### Permutations
  393. ```python
  394. >>> permutations("abc", 2)
  395. [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
  396. ```
  397. ### Product
  398. ```python
  399. >>> list(product('ab', [1,2]))
  400. [('a', 1), ('a', 2), ('b', 1), ('b', 2)]
  401. ```
  402. ### Compress
  403. ```python
  404. >>> compress("abc", [True, 0, 23])
  405. ['a', 'c']
  406. ```
  407. ### Count
  408. ```python
  409. >>> a = count(5, 2)
  410. >>> next(a), next(a)
  411. (5, 7)
  412. ```
  413. ### Cycle
  414. ```python
  415. >>> a = cycle("abc")
  416. >>> [next(a) for _ in range(10)]
  417. ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a']
  418. ```
  419. ### Groupby
  420. ```python
  421. >>> {k: list(v) for k, v in groupby("aabbbc")}
  422. {'a': ['a', 'a'], 'b': ['b', 'b', 'b'], 'c': ['c']}
  423. ```
  424. ```python
  425. >>> a = [{"id": 1, "name": "bob"}, {"id": 2, "name": "bob"}, {"id": 3, "name": "peter"}]
  426. >>> {k: list(v) for k, v in groupby(a, key=lambda x: x["name"])}
  427. {'bob': [{'id': 1, 'name': 'bob'}, {'id': 2, 'name': 'bob'}], 'peter': [{'id': 3, 'name': 'peter'}]}
  428. ```
  429. ### Islice
  430. ```
  431. islice([1, 2, 3], 1, None)
  432. [2, 3]
  433. ```
  434. ### ifilter/imap/izip
  435. Filter, map and zip functions that return generators instead of iterators
  436. Introspection and Metaprograming
  437. --------------------------------
  438. Inspecting code at runetime and code that generates code.
  439. ```python
  440. >>> class B:
  441. ... def __init__(self):
  442. ... self.a= 'sdfsd'
  443. ... self.b = 123324
  444. >>> b = B()
  445. ```
  446. ### Getattr
  447. ```python
  448. >>> getattr(b, 'a')
  449. 'sdfsd'
  450. ```
  451. same as
  452. ```
  453. B.__getattribute__(b, 'a')
  454. ```
  455. ### Hasattr
  456. ```python
  457. >>> hasattr(b, 'c')
  458. False
  459. ```
  460. ### Setattr
  461. ```python
  462. >>> setattr(b, 'c', 10)
  463. ```
  464. Type
  465. ----
  466. Type is the root class. If only passed the object it returns it's type.
  467. Otherwise it creates new class (and not the instance!).
  468. ```python
  469. type(class_name, parents[tuple], attributes[dict])
  470. ```
  471. ```python
  472. BB = type('B', (), {'a': 'sdfsd', 'b': 123324}
  473. b = BB()
  474. ```
  475. MetaClass
  476. ---------
  477. Classes that creates classes.
  478. ```python
  479. def my_meta_class(name, parents, attrs):
  480. ... do stuff
  481. return type(name, parents, attrs)
  482. ```
  483. or
  484. ```python
  485. class MyMetaClass(type):
  486. def __new__(klass, name, parents, attrs):
  487. ... do stuff
  488. return type.__new__(klass, name, parents, attrs)
  489. ```
  490. Do Stuff
  491. --------
  492. * Look at the attributes
  493. * Set new attributes
  494. * Create functions dynamically
  495. * Traverse the parent classes
  496. * Change values in the class
  497. Metaclass Attr
  498. --------------
  499. 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.
  500. ```python
  501. class BlaBla:
  502. __metaclass__ = Bla
  503. ```
  504. Eval
  505. ----
  506. ```
  507. import ast
  508. import operator as op
  509. # supported operators
  510. operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
  511. ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor,
  512. ast.USub: op.neg}
  513. def eval_expr(expr):
  514. """
  515. >>> eval_expr('2^6')
  516. 4
  517. >>> eval_expr('2**6')
  518. 64
  519. >>> eval_expr('1 + 2*3**(4^5) / (6 + -7)')
  520. -5.0
  521. """
  522. print(expr)
  523. return eval_(ast.parse(expr, mode='eval').body)
  524. def eval_(node):
  525. if isinstance(node, ast.Num): # <number>
  526. return node.n
  527. elif isinstance(node, ast.BinOp): # <left> <operator> <right>
  528. return operators[type(node.op)](eval_(node.left), eval_(node.right))
  529. elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
  530. return operators[type(node.op)](eval_(node.operand))
  531. else:
  532. raise TypeError(node)
  533. ```
  534. Ascii
  535. -----
  536. Get char from int.
  537. ```
  538. chr(<int>)
  539. ```
  540. Flatten List
  541. ------------
  542. ```
  543. [item for sublist in a_list for item in sublist]
  544. ```
  545. Libraries
  546. =========
  547. Plot
  548. ----
  549. ```
  550. import matplotlib
  551. matplotlib.pyplot.plot(<data>, ...)
  552. matplotlib.pyplot.show()
  553. matplotlib.pyplot.savefig(<filename>)
  554. ```
  555. Web
  556. ---
  557. ```
  558. import bottle
  559. ```
  560. ### Run
  561. ```
  562. bottle.run(host='localhost', port=8080)
  563. bottle.run(host='0.0.0.0', port=80, server='cherypy')
  564. ```
  565. ### Static request
  566. ### Dynamic request
  567. ### REST request
  568. Curses
  569. ------
  570. ```
  571. import curses
  572. def main():
  573. curses.wrapper(draw)
  574. def draw(screen):
  575. screen.clear()
  576. screen.addstr(0, 0, "Press ESC to quit.")
  577. while screen.getch() != 27:
  578. pass
  579. ```
  580. Profile
  581. -------
  582. ```
  583. import timeit
  584. timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
  585. ```
  586. ```
  587. import pycallgraph
  588. graph = pycallgraph.output.GraphvizOutput()
  589. graph.output_file = "{}-{}.png".format("profile",
  590. get_current_datetime_string())
  591. with pycallgraph.PyCallGraph(output=graph):
  592. <code>
  593. def get_current_datetime_string():
  594. now = datetime.datetime.now()
  595. return get_datetime_string(now)
  596. def get_datetime_string(a_datetime):
  597. return a_datetime.strftime('%Y%m%d%H%M%S')
  598. ```
  599. Audio
  600. -----
  601. Saves list of floats of size 0 to 1 to a wav file.
  602. ```
  603. import wave
  604. import struct
  605. frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>]
  606. wf = wave.open(<filename>, 'wb')
  607. wf.setnchannels(1)
  608. wf.setsampwidth(4)
  609. wf.setframerate(44100)
  610. wf.writeframes(b''.join(frames))
  611. wf.close()
  612. ```
  613. Coockboock
  614. ==========
  615. Dictionary
  616. ----------
  617. Filter by keys:
  618. ```
  619. {k: v for k, v in d.iteritems() if k in [2,3]}
  620. ```