Browse Source

Reddit corrections

pull/3/head
Jure Šorn 7 years ago
parent
commit
29d2bc7c5a
1 changed files with 23 additions and 8 deletions
  1. 31
      README.md

31
README.md

@ -15,6 +15,7 @@ List
---- ----
```python ```python
<list>[from_inclusive : to_exclusive : step_size] <list>[from_inclusive : to_exclusive : step_size]
<list>.append(<el>)
<list>.extend(<list>) <list>.extend(<list>)
<list>.sort() <list>.sort()
<list>.reverse() <list>.reverse()
@ -58,7 +59,6 @@ Set
<set>.union(<set>) <set>.union(<set>)
<set>.intersection(<set>) <set>.intersection(<set>)
<set>.difference(<set>) <set>.difference(<set>)
frozenset() # Is hashable and can be used as a key in dictionary.
``` ```
### Frozenset ### Frozenset
@ -185,6 +185,8 @@ re.search(<regex>, text)
>>> person = {'name': 'Jean-Luc', 'height': 187.1} >>> person = {'name': 'Jean-Luc', 'height': 187.1}
>>> '{p[height]:.0f}'.format(p=person) >>> '{p[height]:.0f}'.format(p=person)
'187' '187'
>>> f"{person['height']:.0f}"
'187'
``` ```
### Text Wrap ### Text Wrap
@ -392,9 +394,20 @@ import os
os.popen(<command>).read() os.popen(<command>).read()
``` ```
#### Or:
```python
>>> import subprocess
>>> a = subprocess.run(['ls', '-a'], stdout=subprocess.PIPE)
>>> a.stdout
b'.\n..\nfile1.txt\nfile2.txt\n'
>>> a.returncode
0
```
### Input ### Input
```python ```python
file_name = input('Enter a file name: ')
filename = input('Enter a file name: ')
``` ```
#### Prints lines until EOF: #### Prints lines until EOF:
@ -414,13 +427,15 @@ import json
### Read File ### Read File
```python ```python
with open(file_name, encoding='utf-8') as file:
with open(filename, encoding='utf-8') as file:
return json.load(file) return json.load(file)
``` ```
### Write to File ### Write to File
```python ```python
with open(file_name, 'w', enconding='utf-8') as file:
with open(filename, 'w', encoding='utf-8') as file:
json.dump(<object>, file)
# Or
file.write(json.dumps(<object>)) file.write(json.dumps(<object>))
``` ```
@ -428,7 +443,7 @@ SQLite
------ ------
```python ```python
import sqlite3 import sqlite3
db = sqlite3.connect(file_name)
db = sqlite3.connect(filename)
``` ```
### Read ### Read
@ -746,13 +761,13 @@ timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
```python ```python
import pycallgraph import pycallgraph
graph = pycallgraph.output.GraphvizOutput() graph = pycallgraph.output.GraphvizOutput()
graph.output_file = get_file_name()
graph.output_file = get_filename()
with pycallgraph.PyCallGraph(output=graph): with pycallgraph.PyCallGraph(output=graph):
<code_to_be_profiled> <code_to_be_profiled>
``` ```
#### Utility code for unique PNG filenames: #### Utility code for unique PNG filenames:
```python ```python
def get_file_name():
def get_filename():
return "{}-{}.png".format("profile", get_current_datetime_string()) return "{}-{}.png".format("profile", get_current_datetime_string())
def get_current_datetime_string(): def get_current_datetime_string():
@ -769,7 +784,7 @@ Audio
```python ```python
import wave, struct import wave, struct
frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>] frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>]
wf = wave.open(file_name, 'wb')
wf = wave.open(filename, 'wb')
wf.setnchannels(1) wf.setnchannels(1)
wf.setsampwidth(4) wf.setsampwidth(4)
wf.setframerate(44100) wf.setframerate(44100)

Loading…
Cancel
Save