diff --git a/index.html b/index.html index fcad74c..c762fb9 100644 --- a/index.html +++ b/index.html @@ -120,8 +120,8 @@ product_of_elems = functools.reduce(lambda out list_of_chars = list(<str>)
<list>.insert(<int>, <el>) # Inserts item at index and moves the rest to the right.
<el> = <list>.pop([<int>]) # Removes and returns item at index or from the end.
@@ -386,7 +386,7 @@ Point(x=1, y=2
- Options can be generated dynamically:
f'{<el>:{<str/int>}[…]}'
.
-- Adding
'!r'
before the colon converts object to string by calling its repr() method (p. 14).
+- Adding
'!r'
before the colon converts object to string by calling its repr() method.
Strings
{'abcde':10} # 'abcde '
{'abcde':10.3} # 'abc '
@@ -750,7 +750,7 @@ player = Player(point, direction) #
#Decorator
- A decorator takes a function, adds some functionality and returns it.
-- It can be any callable, but is usually implemented as a function that returns a closure.
+- It can be any callable, but is usually implemented as a function that returns a closure.
@decorator_name
def function_that_gets_passed_to_decorator():
...
@@ -894,9 +894,9 @@ Z = dataclasses.make_dataclass('Z', [
-Objects can be made sortable with 'order=True'
and immutable with 'frozen=True'
.
-For object to be hashable, all attributes must be hashable and 'frozen' must be True.
-Function field() is needed because '<attr_name>: list = []'
would make a list that is shared among all instances. Its 'default_factory' argument can be any callable (p. 17).
+Objects can be made sortable with 'order=True'
and immutable with 'frozen=True'
.
+For object to be hashable, all attributes must be hashable and 'frozen' must be True.
+Function field() is needed because '<attr_name>: list = []'
would make a list that is shared among all instances. Its 'default_factory' argument can be any callable.
For attributes of arbitrary type use 'typing.Any'
.
Inline:
from dataclasses import make_dataclass
@@ -998,10 +998,10 @@ Z = dataclasses.make_dataclass('Z', [1, 2, 3)
Python has many different iterator objects:
-- Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p. 3).
-- Objects returned by the itertools module, such as count, repeat and cycle (p. 3).
-- Generators returned by the generator functions (p. 4) and generator expressions (p. 11).
-- File objects returned by the open() function (p. 22), etc.
+- Sequence iterators returned by the iter() function, such as list_iterator and set_iterator.
+- Objects returned by the itertools module, such as count, repeat and cycle.
+- Generators returned by the generator functions and generator expressions.
+- File objects returned by the open() function, etc.
Callable
- All functions and classes have a call() method, hence are callable.
- When this cheatsheet uses
'<function>'
as an argument, it actually means '<callable>'
.
@@ -1452,7 +1452,7 @@ shutil.rmtree(<path>) # Deletes t
- Paths can be either strings, Paths or DirEntry objects.
-- Functions report OS related errors by raising OSError or one of its subclasses (p. 23).
+- Functions report OS related errors by raising either OSError or one of its subclasses.
Shell Commands
<pipe> = os.popen('<command>') # Executes command in sh/cmd. Returns its stdout pipe.
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
@@ -1514,8 +1514,8 @@ CompletedProcess(args=['bc', 'newline=""'
argument, or newlines embedded inside quoted fields will not be interpreted correctly!
-
- To print the spreadsheet to the console use Tabulate library (p. 34).
-- For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library (p. 46).
+- To print the spreadsheet to the console use Tabulate library.
+- For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library.
- Reader accepts any iterator of strings, not just files.
Write
<writer> = csv.writer(<file>) # Also: `dialect='excel', delimiter=','`.
@@ -1588,7 +1588,7 @@ CompletedProcess(args=['bc', ISO formatted strings.
Example
Values are not actually saved in this example because 'conn.commit()'
is omitted!
>>> conn = sqlite3.connect('test.db')
>>> conn.execute('CREATE TABLE person (person_id INTEGER PRIMARY KEY, name, height)')
@@ -1761,7 +1761,7 @@ CompletedProcess(args=['bc', Thread Pool Executor
- Object that manages thread execution.
-- An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be pickable (p. 25).
+- An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be pickable.
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: …`
<Exec>.shutdown(wait=True) # Blocks until all threads finish executing.
@@ -1898,7 +1898,7 @@ ValueError: malformed node or string
- Coroutine definition starts with
'async'
and its call with 'await'
.
'asyncio.run(<coroutine>)'
is the main entry point for asynchronous programs.
- Functions wait(), gather() and as_completed() start multiple coroutines at the same time.
-- Asyncio module also provides its own Queue, Event, Lock and Semaphore classes (p. 30).
+- Asyncio module also provides its own Queue, Event, Lock and Semaphore classes.
Runs a terminal game where you control an asterisk that must avoid numbers:
import asyncio, collections, curses, curses.textpad, enum, random
P = collections.namedtuple('P', 'x y') # Position
@@ -2081,7 +2081,7 @@ app.run()
- Starts the app on
'http://localhost:5000'
.
-- A WSGI server like Waitress and a HTTP server such as Nginx are needed to run globally.
+- A WSGI server like Waitress and a HTTP server such as Nginx are needed to run globally.
Static Request
@app.route('/img/<path:filename>')
def serve_file(filename):
@@ -2162,7 +2162,7 @@ drawer = cg.output.GraphvizOutput(output_file=filename)
-- The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.
+- The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.
#NumPy
Array manipulation mini-language. It can run up to one hundred times faster than the equivalent Python code. An even faster alternative that runs on a GPU is called CuPy.
# $ pip3 install numpy
import numpy as np