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.

104 lines
1.2 KiB

7 years ago
  1. Python and Libraries Cheatsheet
  2. ===============================
  3. Sum
  4. ---
  5. ```
  6. sum(<list>)
  7. ```
  8. Profile
  9. -------
  10. ```
  11. import pycallgraph
  12. graph = pycallgraph.output.GraphvizOutput()
  13. graph.output_file = <filename>
  14. whith pycallgraph.PyCallGraph(output=graph):
  15. <code>
  16. ```
  17. SQLite
  18. ------
  19. ```
  20. import sqlite3
  21. db = sqlite3.connect(<filename>)
  22. ```
  23. ### Read
  24. ```
  25. cursor = db.execute(<query>)
  26. if cursor:
  27. cursor.<fetchone/fetchall>()
  28. db.close()
  29. ```
  30. ### Write
  31. ```
  32. db.execute(<query>)
  33. db.commit()
  34. ```
  35. Curses
  36. ------
  37. ```
  38. import curses
  39. def main():
  40. curses.wrapper(draw)
  41. def draw(screen):
  42. screen.clear()
  43. screen.addstr(0, 0, "Press ESC to quit.")
  44. while screen.getch() != 27:
  45. pass
  46. ```
  47. Regex
  48. -----
  49. ```
  50. import re
  51. re.sub(<regex>, <new>, <text>)
  52. re.search(<regex>, <text>)
  53. ```
  54. Threading
  55. ---------
  56. ```
  57. import threading
  58. ```
  59. ### Thread
  60. ```
  61. thread = threading.Thread(target=<function>, args=(<first arg>, ))
  62. thread.start()
  63. thread.join()
  64. ```
  65. ### Lock
  66. ```
  67. lock = threading.Rlock()
  68. lock.acquire()
  69. lock.release()
  70. ```
  71. Bottle
  72. ------
  73. ```
  74. import bottle
  75. ```
  76. ### Run
  77. ```
  78. bottle.run(host='localhost', port=8080)
  79. bottle.run(host='0.0.0.0', port=80, server='cherypy')
  80. ```
  81. ### Static request
  82. ### Dynamic request
  83. ### REST request
  84. Type
  85. ----