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