Browse Source

Added condensed and libraries markdown files

pull/1/head
Jure Šorn 7 years ago
parent
commit
8be850c556
3 changed files with 423 additions and 188 deletions
  1. 218
      CONDENSED.md
  2. 58
      LIBRARIES.md
  3. 335
      README.md

218
CONDENSED.md

@ -0,0 +1,218 @@
Python and Libraries Cheatsheet
===============================
Main
----
```
if __name__ == '__main__':
main()
```
Range
-----
```python
range(<from inclusive>, <to exclusive>, <step size>) # Negative step for backward.
```
List
----
```python
<list>[<inclusive from>:<exclusive to>:<step size>]
```
Dictionary
----------
```
<dict>.items()
<dict>.get(<key>, <default>)
<dict>.setdefault(<key>, <default>)
```
Enumerate
---------
```
for i, <el> in enumerate(<list/dict/set>)
```
Inline
------
### For
```pythonstub
[i+j for i in range(10) for j in range(10) if i+j > 5]
```
### Lambda
```
lambda <arg1>, <arg2>: <return value>
```
String
------
### Print
```
print(<el1>, <el2>, end='', sep='', file=<file>)
```
### Regex
```
import re
re.sub(<regex>, <new>, <text>)
re.search(<regex>, <text>)
```
### Format
```
{:<min width>} -> '<el> '
{:><min width>} -> ' <el>'
{:^<min width>} -> ' <el> '
{:_<min width>} -> '<el>____'
{:.<max width>} -> '<e>'
{:<max widht>.<min width>} -> ' <e>'
{:<max width>.<no of decimals>f} -> ' 3.14'
```
Infinity
--------
```
float("inf")
```
Class
-----
### Class
```
class <name>:
def __init__(self, <arg>):
self.a = <arg>
def __repr__(self):
return str({'a': self.a})
def __str__(self):
return str(self.a)
```
### Enum
```
import enum
class <name>(enum.Enum):
<value> = <index>
```
### Copy
```
import copy
copy.copy(<object>)
copy.deepcopy(<object>)
```
Random
------
```
import random
random.random()
random.randint(<from inclusive>, <to inclusive>)
random.shuffle(<list>)
```
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(<filename>, encoding='utf-8') as file:
return file.readlines()
```
### Write
```
with open(<filename>, 'w', enconding='utf-8') as file:
file.write(<text>)
```
### Execute Command
```
import os
os.popen(<command>).read()
```
JSON
----
```
import json
```
### Read
```
with open(<filename>, encoding='utf-8') as file:
return json.load(file)
```
### Write
```
with open(<filename>, 'w', enconding='utf-8') as file:
file.write(json.dumps(<object>))
```
SQLite
------
```
import sqlite3
db = sqlite3.connect(<filename>)
```
### Read
```
cursor = db.execute(<query>)
if cursor:
cursor.<fetchone/fetchall>()
db.close()
```
### Write
```
db.execute(<query>)
db.commit()
```

58
LIBRARIES.md

@ -0,0 +1,58 @@
Libraries
=========
Plot
----
```
import matplotlib
matplotlib.pyplot.plot(<data>, ...)
matplotlib.pyplot.show()
matplotlib.pyplot.savefig(<filename>)
```
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 = <filename>
whith pycallgraph.PyCallGraph(output=graph):
<code>
```

335
README.md

@ -1,101 +1,56 @@
Python and Libraries Cheatsheet Python and Libraries Cheatsheet
=============================== ===============================
Sum
---
```
sum(<list>)
```
Profile
-------
```
import pycallgraph
graph = pycallgraph.output.GraphvizOutput()
graph.output_file = <filename>
whith pycallgraph.PyCallGraph(output=graph):
<code>
```
SQLite
------
```
import sqlite3
db = sqlite3.connect(<filename>)
```
### Read
```
cursor = db.execute(<query>)
if cursor:
cursor.<fetchone/fetchall>()
db.close()
```
### Write
```
db.execute(<query>)
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(<regex>, <new>, <text>)
re.search(<regex>, <text>)
```
Threading
---------
```
import threading
range(<to exclusive>)
range(<from inclusive>, <to exclusive>)
range(<from inclusive>, <to exclusive>, <step size>) # Negative step for backward
``` ```
### Thread
List
----
``` ```
thread = threading.Thread(target=<function>, args=(<first arg>, ))
thread.start()
thread.join()
<list>[<inclusive from>:<exclusive to>:<step size>]
<list>.extend(<list>)
<list>.sort()
<list>.reverse()
sum(<list>)
``` ```
### Lock
```
lock = threading.Rlock()
lock.acquire()
lock.release()
Dictionary
----------
<dict>.items()
<dict>.get(<key>, <default>)
<dict>.setdefault(<key>, <default>)
``` ```
Bottle
------
Set
---
``` ```
import bottle
<set> = set()
<set>.add(<el>)
<set>.update(<set>)
<set>.union(<set>)
<set>.intersection(<set>)
<set>.difference(<set>)
``` ```
### Run
Enumerate
---------
``` ```
bottle.run(host='localhost', port=8080)
bottle.run(host='0.0.0.0', port=80, server='cherypy')
for i, <el> in enumerate(<list>)
``` ```
### Static request
### Dynamic request
### REST request
Type Type
---- ----
@ -107,60 +62,64 @@ import numbers
isinstance(<el>, numbers.Number) isinstance(<el>, numbers.Number)
``` ```
Arguments
---------
String
------
``` ```
import sys
sys.argv
str.replace(<text>, <old>, <new>)
<str>.isnumeric()
``` ```
Enumerate
---------
### Print
``` ```
for i, <el> in enumerate(<list>)
print(<el1>, <el2>, end='', sep='', file=<file>)
``` ```
System Commands
---------------
### Read File
### Regex
``` ```
with open(<filename>, encoding='utf-8') as file:
return file.readlines()
import re
re.sub(<regex>, <new>, <text>)
re.search(<regex>, <text>)
``` ```
### Write to File
### Format
``` ```
with open(<filename>, 'w', enconding='utf-8') as file:
file.write(<text>)
'{}'.format(<el>)
``` ```
### Execute Command
``` ```
import os
os.popen(<command>).read()
{:<min width>} -> '<el> '
{:><min width>} -> ' <el>'
{:^<min width>} -> ' <el> '
{:_<min width>} -> '<el>____'
{:.<max width>} -> '<e>'
{:<max widht>.<min width>} -> ' <e>'
{:<max width>.<no of decimals>f} -> ' 3.14'
``` ```
JSON
----
### Text Wrap
``` ```
import json
import textwrap
textwrap.wrap(<text>, <width>)
``` ```
### Read File
Random
------
``` ```
with open(<filename>, encoding='utf-8') as file:
return json.load(file)
import random
random.random()
random.randint(<from inclusive>, <to inclusive>)
random.shuffle(<list>)
``` ```
### Write to File
Infinity
--------
``` ```
with open(<filename>, 'w', enconding='utf-8') as file:
file.write(json.dumps(<object>))
float("inf")
``` ```
Datetime Datetime
-------- --------
``` ```
@ -170,14 +129,39 @@ now.strftime('%Y%m%d')
now.strftime('%Y%m%d%H%M%S') now.strftime('%Y%m%d%H%M%S')
``` ```
String
Inline
------ ------
### For
``` ```
str.replace(<text>, <old>, <new>)
<str>.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 <arg1>, <arg2>: <return value>
lambda: <return value>
```
Class
-----
### Class
```
class <name>:
def __init__(self, <arg>):
self.a = <arg>
def __repr__(self):
return str({'a': self.a})
def __str__(self):
return str(self.a)
``` ```
Enum
### Enum
---- ----
``` ```
import enum import enum
@ -185,128 +169,103 @@ class <name>(enum.Enum):
<value> = <index> <value> = <index>
``` ```
Copy
----
### Copy
``` ```
import copy import copy
copy.copy(<object>) copy.copy(<object>)
copy.deepcopy(<object>) copy.deepcopy(<object>)
``` ```
Infinity
--------
```
float("inf")
```
Plot
----
```
import matplotlib
matplotlib.pyplot.plot(<data>, ...)
matplotlib.pyplot.show()
matplotlib.pyplot.savefig(<filename>)
```
System
------
List
----
### Arguments
``` ```
<list>[<inclusive from>:<exclusive to>:<step size>]
import sys
sys.argv
``` ```
Lambda
------
### Read File
``` ```
lambda <arg1>, <arg2>: <return value>
lambda: <return value>
with open(<filename>, 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(<filename>, 'w', enconding='utf-8') as file:
file.write(<text>)
``` ```
Class
-----
### Execute Command
``` ```
class <name>:
def __init__(self, <arg>):
self.a = <arg>
def __repr__(self):
return str({'a': self.a})
def __str__(self):
return str(self.a)
import os
os.popen(<command>).read()
``` ```
Random
------
JSON
----
``` ```
import random
random.random()
random.randint(<from inclusive>, <to inclusive>)
random.shuffle(<list>)
import json
``` ```
Range
-----
### Read File
``` ```
range(<to exclusive>)
range(<from inclusive>, <to exclusive>)
range(<from inclusive>, <to exclusive>, <step size>) # Negative step for backward
with open(<filename>, encoding='utf-8') as file:
return json.load(file)
``` ```
List
----
### Write to File
``` ```
<list>.sort()
<list>.reverse()
<list>.extend(<list>)
with open(<filename>, 'w', enconding='utf-8') as file:
file.write(json.dumps(<object>))
``` ```
Print
-----
SQLite
------
``` ```
print(<el1>, <el2>, end='', sep='', file=<file>)
import sqlite3
db = sqlite3.connect(<filename>)
``` ```
Format
------
### Read
``` ```
'{}'.format(<el>)
cursor = db.execute(<query>)
if cursor:
cursor.<fetchone/fetchall>()
db.close()
``` ```
### Write
``` ```
{:<min width>} -> '<el> '
{:><min width>} -> ' <el>'
{:^<min width>} -> ' <el> '
{:_<min width>} -> '<el>____'
{:.<max width>} -> '<e>'
{:<max widht>.<min width>} -> ' <e>'
{:<max width>.<no of decimals>f} -> ' 3.14'
db.execute(<query>)
db.commit()
``` ```
Text Wrap
Threading
--------- ---------
``` ```
import textwrap
textwrap.wrap(<text>, <width>)
import threading
``` ```
Dictionary
----------
<dict>.items()
<dict>.get(<key>, <default>)
<dict>.setdefault(<key>, <default>)
### Thread
```
thread = threading.Thread(target=<function>, args=(<first arg>, ))
thread.start()
thread.join()
``` ```
### Lock
```
lock = threading.Rlock()
lock.acquire()
lock.release()
```

Loading…
Cancel
Save