From 8be850c5563d98c81daf873f2376cec2a733c2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 9 Nov 2017 20:16:27 +0100 Subject: [PATCH] Added condensed and libraries markdown files --- CONDENSED.md | 218 +++++++++++++++++++++++++++++++++ LIBRARIES.md | 58 +++++++++ README.md | 335 ++++++++++++++++++++++----------------------------- 3 files changed, 423 insertions(+), 188 deletions(-) create mode 100644 CONDENSED.md create mode 100644 LIBRARIES.md diff --git a/CONDENSED.md b/CONDENSED.md new file mode 100644 index 0000000..08422b0 --- /dev/null +++ b/CONDENSED.md @@ -0,0 +1,218 @@ +Python and Libraries Cheatsheet +=============================== + +Main +---- +``` +if __name__ == '__main__': + main() +``` + +Range +----- +```python +range(, , ) # Negative step for backward. +``` + +List +---- +```python +[::] +``` + +Dictionary +---------- +``` +.items() +.get(, ) +.setdefault(, ) +``` + +Enumerate +--------- +``` +for i, in enumerate() +``` + +Inline +------ +### For +```pythonstub +[i+j for i in range(10) for j in range(10) if i+j > 5] +``` + +### Lambda +``` +lambda , : +``` + +String +------ + +### Print +``` +print(, , end='', sep='', file=) +``` + +### Regex +``` +import re +re.sub(, , ) +re.search(, ) +``` + +### Format +``` +{:} -> ' ' +{:>} -> ' ' +{:^} -> ' ' +{:_} -> '____' +{:.} -> '' +{:.} -> ' ' +{:.f} -> ' 3.14' +``` + +Infinity +-------- +``` +float("inf") +``` + +Class +----- +### Class +``` +class : + def __init__(self, ): + self.a = + def __repr__(self): + return str({'a': self.a}) + def __str__(self): + return str(self.a) +``` + +### Enum +``` +import enum +class (enum.Enum): + = +``` + +### Copy +``` +import copy +copy.copy() +copy.deepcopy() +``` + +Random +------ +``` +import random +random.random() +random.randint(, ) +random.shuffle() +``` + +Datetime +-------- +``` +import datetime +now = datetime.datetime.now() +now.strftime('%Y%m%d') +now.strftime('%Y%m%d%H%M%S') +``` + +System +------ + +### Arguments +``` +import sys +sys.argv +``` + +### Read +``` +with open(, encoding='utf-8') as file: + return file.readlines() +``` + +### Write +``` +with open(, 'w', enconding='utf-8') as file: + file.write() +``` + +### Execute Command +``` +import os +os.popen().read() +``` + +JSON +---- +``` +import json +``` + +### Read +``` +with open(, encoding='utf-8') as file: + return json.load(file) +``` + +### Write +``` +with open(, 'w', enconding='utf-8') as file: + file.write(json.dumps()) +``` + +SQLite +------ +``` +import sqlite3 +db = sqlite3.connect() +``` + +### Read +``` +cursor = db.execute() +if cursor: + cursor.() +db.close() +``` + +### Write +``` +db.execute() +db.commit() +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LIBRARIES.md b/LIBRARIES.md new file mode 100644 index 0000000..ed5aeb7 --- /dev/null +++ b/LIBRARIES.md @@ -0,0 +1,58 @@ +Libraries +========= + + +Plot +---- +``` +import matplotlib +matplotlib.pyplot.plot(, ...) +matplotlib.pyplot.show() +matplotlib.pyplot.savefig() +``` + + +Web +--- +``` +import bottle +``` + +### Run +``` +bottle.run(host='localhost', port=8080) +bottle.run(host='0.0.0.0', port=80, server='cherypy') +``` + +### Static request + +### Dynamic request + +### REST request + + +Curses +------ +``` +import curses +def main(): + curses.wrapper(draw) +def draw(screen): + screen.clear() + screen.addstr(0, 0, "Press ESC to quit.") + while screen.getch() != 27: + pass +``` + + + +Profile +------- +``` +import pycallgraph +graph = pycallgraph.output.GraphvizOutput() +graph.output_file = +whith pycallgraph.PyCallGraph(output=graph): + +``` + diff --git a/README.md b/README.md index 2d008bd..0aa4b32 100644 --- a/README.md +++ b/README.md @@ -1,101 +1,56 @@ Python and Libraries Cheatsheet =============================== -Sum ---- -``` -sum() -``` - -Profile -------- -``` -import pycallgraph -graph = pycallgraph.output.GraphvizOutput() -graph.output_file = -whith pycallgraph.PyCallGraph(output=graph): - -``` - -SQLite ------- -``` -import sqlite3 -db = sqlite3.connect() -``` - -### Read -``` -cursor = db.execute() -if cursor: - cursor.() -db.close() -``` - -### Write -``` -db.execute() -db.commit() -``` - -Curses ------- +Main +---- ``` -import curses -def main(): - curses.wrapper(draw) -def draw(screen): - screen.clear() - screen.addstr(0, 0, "Press ESC to quit.") - while screen.getch() != 27: - pass +if __name__ == '__main__': + main() ``` -Regex +Range ----- ``` -import re -re.sub(, , ) -re.search(, ) -``` - -Threading ---------- -``` -import threading +range() +range(, ) +range(, , ) # Negative step for backward ``` -### Thread +List +---- ``` -thread = threading.Thread(target=, args=(, )) -thread.start() -thread.join() +[::] +.extend() +.sort() +.reverse() +sum() ``` -### Lock -``` -lock = threading.Rlock() -lock.acquire() -lock.release() +Dictionary +---------- +.items() +.get(, ) +.setdefault(, ) ``` -Bottle ------- +Set +--- ``` -import bottle + = set() +.add() +.update() +.union() +.intersection() +.difference() ``` -### Run +Enumerate +--------- ``` -bottle.run(host='localhost', port=8080) -bottle.run(host='0.0.0.0', port=80, server='cherypy') +for i, in enumerate() ``` -### Static request - -### Dynamic request -### REST request Type ---- @@ -107,60 +62,64 @@ import numbers isinstance(, numbers.Number) ``` -Arguments ---------- + + +String +------ ``` -import sys -sys.argv +str.replace(, , ) +.isnumeric() ``` -Enumerate ---------- +### Print ``` -for i, in enumerate() +print(, , end='', sep='', file=) ``` - -System Commands ---------------- -### Read File +### Regex ``` -with open(, encoding='utf-8') as file: - return file.readlines() +import re +re.sub(, , ) +re.search(, ) ``` -### Write to File +### Format ``` -with open(, 'w', enconding='utf-8') as file: - file.write() +'{}'.format() ``` -### Execute Command ``` -import os -os.popen().read() +{:} -> ' ' +{:>} -> ' ' +{:^} -> ' ' +{:_} -> '____' +{:.} -> '' +{:.} -> ' ' +{:.f} -> ' 3.14' ``` - -JSON ----- +### Text Wrap ``` -import json +import textwrap +textwrap.wrap(, ) ``` -### Read File + +Random +------ ``` -with open(, encoding='utf-8') as file: - return json.load(file) +import random +random.random() +random.randint(, ) +random.shuffle() ``` -### Write to File +Infinity +-------- ``` -with open(, 'w', enconding='utf-8') as file: - file.write(json.dumps()) +float("inf") ``` - Datetime -------- ``` @@ -170,14 +129,39 @@ now.strftime('%Y%m%d') now.strftime('%Y%m%d%H%M%S') ``` -String + + + + +Inline ------ +### For ``` -str.replace(, , ) -.isnumeric() +[i+1 for i in range(10)] +[i+1 for i in range(10) if i > 5] +[i+j for i in range(10) for j in range(10)] +``` + +### Lambda +``` +lambda , : +lambda: +``` + +Class +----- +### Class +``` +class : + def __init__(self, ): + self.a = + def __repr__(self): + return str({'a': self.a}) + def __str__(self): + return str(self.a) ``` -Enum +### Enum ---- ``` import enum @@ -185,128 +169,103 @@ class (enum.Enum): = ``` -Copy ----- +### Copy ``` import copy copy.copy() copy.deepcopy() ``` -Infinity --------- -``` -float("inf") -``` -Plot ----- -``` -import matplotlib -matplotlib.pyplot.plot(, ...) -matplotlib.pyplot.show() -matplotlib.pyplot.savefig() -``` +System +------ -List ----- +### Arguments ``` -[::] +import sys +sys.argv ``` -Lambda ------- +### Read File ``` -lambda , : -lambda: +with open(, encoding='utf-8') as file: + return file.readlines() ``` -[For] ------ +### Write to File ``` -[i+1 for i in range(10)] -[i+1 for i in range(10) if i > 5] -[i+j for i in range(10) for j in range(10)] +with open(, 'w', enconding='utf-8') as file: + file.write() ``` -Class ------ +### Execute Command ``` -class : - def __init__(self, ): - self.a = - def __repr__(self): - return str({'a': self.a}) - def __str__(self): - return str(self.a) +import os +os.popen().read() ``` -Random ------- + +JSON +---- ``` -import random -random.random() -random.randint(, ) -random.shuffle() +import json ``` -Range ------ +### Read File ``` -range() -range(, ) -range(, , ) # Negative step for backward +with open(, encoding='utf-8') as file: + return json.load(file) ``` -List ----- +### Write to File ``` -.sort() -.reverse() -.extend() +with open(, 'w', enconding='utf-8') as file: + file.write(json.dumps()) ``` -Print ------ + + +SQLite +------ ``` -print(, , end='', sep='', file=) +import sqlite3 +db = sqlite3.connect() ``` -Format ------- +### Read ``` -'{}'.format() +cursor = db.execute() +if cursor: + cursor.() +db.close() ``` +### Write ``` -{:} -> ' ' -{:>} -> ' ' -{:^} -> ' ' -{:_} -> '____' -{:.} -> '' -{:.} -> ' ' -{:.f} -> ' 3.14' +db.execute() +db.commit() ``` -Text Wrap + +Threading --------- ``` -import textwrap -textwrap.wrap(, ) +import threading ``` -Dictionary ----------- -.items() -.get(, ) -.setdefault(, ) +### Thread +``` +thread = threading.Thread(target=, args=(, )) +thread.start() +thread.join() ``` - - - - - +### Lock +``` +lock = threading.Rlock() +lock.acquire() +lock.release() +```