diff --git a/README.md b/README.md index f7d312aed..c6d14cd75 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ Contents **   ** **7. Libraries:** **      ** **[`Progress_Bar`](#progress-bar)**__,__ **[`Plot`](#plot)**__,__ **[`Table`](#table)**__,__ **[`Curses`](#curses)**__,__ **[`Logging`](#logging)**__,__ **[`Scraping`](#scraping)**__,__ **[`Web`](#web)**__,__ **[`Profile`](#profiling)**__,__ **                                 ** **[`NumPy`](#numpy)**__,__ **[`Image`](#image)**__,__ **[`Audio`](#audio)**__,__ **[`Games`](#pygame)**__,__ **[`Data`](#pandas)**__.__ - Main ---- ```python @@ -25,6 +24,7 @@ if __name__ == '__main__': # Runs main() if file wasn't imported. main() ``` +**[`Back To Contents`](#Contents)** List ---- @@ -65,6 +65,7 @@ list_of_chars = list() .clear() # Removes all items. Also works on dictionary and set. ``` +**[`Back To Contents`](#Contents)** Dictionary ---------- @@ -105,6 +106,7 @@ Counter({'blue': 3, 'red': 2, 'yellow': 1}) ('blue', 3) ``` +**[`Back To Contents`](#Contents)** Set --- @@ -139,6 +141,7 @@ Set = frozenset() ``` +**[`Back To Contents`](#Contents)** Tuple ----- @@ -167,6 +170,7 @@ Point(x=1, y=2) ('x', 'y') ``` +**[`Back To Contents`](#Contents)** Range ----- @@ -181,6 +185,7 @@ from_inclusive = .start to_exclusive = .stop ``` +**[`Back To Contents`](#Contents)** Enumerate --------- @@ -189,6 +194,7 @@ for i, el in enumerate( [, i_start]): ... ``` +**[`Back To Contents`](#Contents)** Iterator -------- @@ -220,6 +226,7 @@ from itertools import count, repeat, cycle, chain, islice = islice(, from_inclusive, …) # `to_exclusive, step_size`. ``` +**[`Back To Contents`](#Contents)** Generator --------- @@ -239,6 +246,7 @@ def count(start, step): (10, 12, 14) ``` +**[`Back To Contents`](#Contents)** Type ---- @@ -298,6 +306,7 @@ True +--------------------+----------+----------+----------+----------+----------+ ``` +**[`Back To Contents`](#Contents)** String ------ @@ -347,6 +356,7 @@ String ``` * **Also: `'isspace()'` checks for `'[ \t\n\r\f\v\x1c-\x1f\x85…]'`.** +**[`Back To Contents`](#Contents)** Regex ----- @@ -386,6 +396,7 @@ import re '\s' == '[ \t\n\r\f\v]' # Matches whitespaces. ``` +**[`Back To Contents`](#Contents)** Format ------ @@ -479,6 +490,7 @@ Format {90:X} # '5A' ``` +**[`Back To Contents`](#Contents)** Numbers ------- @@ -539,6 +551,7 @@ from random import random, randint, choice, shuffle, gauss, seed = ~ # Not (also: - - 1) ``` +**[`Back To Contents`](#Contents)** Combinatorics ------------- @@ -581,6 +594,7 @@ from itertools import product, combinations, combinations_with_replacement, perm ('c', 'a'), ('c', 'b')] # c x x . ``` +**[`Back To Contents`](#Contents)** Datetime -------- @@ -660,6 +674,7 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary = / # How many weeks/years there are in TD. Also '//'. ``` +**[`Back To Contents`](#Contents)** Arguments --------- @@ -677,6 +692,7 @@ def f(): # def f(x=0, y=0): def f(, ): # def f(x, y=0): ``` +**[`Back To Contents`](#Contents)** Splat Operator -------------- @@ -745,6 +761,7 @@ def f(x, *args, z, **kwargs): # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3 head, *body, tail = ``` +**[`Back To Contents`](#Contents)** Inline ------ @@ -810,6 +827,7 @@ Creature = make_dataclass('Creature', ['loc', 'dir']) creature = Creature(point, direction) ``` +**[`Back To Contents`](#Contents)** Imports ------- @@ -822,6 +840,7 @@ import . # Imports a built-in or '/.py'. * **On a filesystem this corresponds to a directory of Python files with an optional init script.** * **Running `'import '` does not automatically provide access to the package's modules unless they are explicitly imported in its init script.** +**[`Back To Contents`](#Contents)** Closure ------- @@ -879,6 +898,7 @@ def get_counter(): (1, 2, 3) ``` +**[`Back To Contents`](#Contents)** Decorator --------- @@ -943,6 +963,7 @@ def add(x, y): return x + y ``` +**[`Back To Contents`](#Contents)** Class ----- @@ -1076,6 +1097,7 @@ from copy import copy, deepcopy = deepcopy() ``` +**[`Back To Contents`](#Contents)** Duck Types ---------- @@ -1208,6 +1230,7 @@ class MyOpen: Hello World! ``` +**[`Back To Contents`](#Contents)** Iterable Duck Types ------------------- @@ -1302,6 +1325,7 @@ class MyAbcSequence(abc.Sequence): * **Other ABCs that generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.** * **Names of their required methods are stored in `'.__abstractmethods__'`.** +**[`Back To Contents`](#Contents)** Enum ---- @@ -1355,6 +1379,7 @@ LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r), ``` * **Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.** +**[`Back To Contents`](#Contents)** Exceptions ---------- @@ -1469,6 +1494,7 @@ class MyInputError(MyError): pass ``` +**[`Back To Contents`](#Contents)** Exit ---- @@ -1480,6 +1506,7 @@ sys.exit() # Prints to stderr and exits with 1. sys.exit() # Exits with passed exit code. ``` +**[`Back To Contents`](#Contents)** Print ----- @@ -1496,6 +1523,7 @@ pprint(, width=80, depth=None, compact=False, sort_dicts=True) ``` * **Levels deeper than 'depth' get replaced by '...'.** +**[`Back To Contents`](#Contents)** Input ----- @@ -1508,6 +1536,7 @@ Input * **Prompt string is printed to the standard output before reading input.** * **Raises EOFError when user hits EOF (ctrl-d/ctrl-z⏎) or input stream gets exhausted.** +**[`Back To Contents`](#Contents)** Command Line Arguments ---------------------- @@ -1534,6 +1563,7 @@ value = args. * **Use `'default='` to set the default value.** * **Use `'type=FileType()'` for files.** +**[`Back To Contents`](#Contents)** Open ---- @@ -1599,6 +1629,7 @@ def write_to_file(filename, text): file.write(text) ``` +**[`Back To Contents`](#Contents)** Paths ----- @@ -1679,6 +1710,7 @@ from pathlib import Path = open() # Opens the file and returns file object. ``` +**[`Back To Contents`](#Contents)** OS Commands ----------- @@ -1735,6 +1767,7 @@ CompletedProcess(args=['bc', '-s'], returncode=0) '2\n' ``` +**[`Back To Contents`](#Contents)** JSON ---- @@ -1760,6 +1793,7 @@ def write_to_json_file(filename, an_object): json.dump(an_object, file, ensure_ascii=False, indent=2) ``` +**[`Back To Contents`](#Contents)** Pickle ------ @@ -1785,6 +1819,7 @@ def write_to_pickle_file(filename, an_object): pickle.dump(an_object, file) ``` +**[`Back To Contents`](#Contents)** CSV --- @@ -1850,6 +1885,7 @@ def write_to_csv_file(filename, rows): writer.writerows(rows) ``` +**[`Back To Contents`](#Contents)** SQLite ------ @@ -1917,6 +1953,7 @@ from mysql import connector .execute('', ) # Replaces '%()s's with values. ``` +**[`Back To Contents`](#Contents)** Bytes ----- @@ -1959,6 +1996,7 @@ def write_bytes(filename, bytes_obj): file.write(bytes_obj) ``` +**[`Back To Contents`](#Contents)** Struct ------ @@ -2001,6 +2039,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03' * **`'f'` - float (4/4)** * **`'d'` - double (8/8)** +**[`Back To Contents`](#Contents)** Array ----- @@ -2015,6 +2054,7 @@ from array import array .write() # Writes array to the binary file. ``` +**[`Back To Contents`](#Contents)** Memory View ----------- @@ -2046,6 +2086,7 @@ Memory View '' = .hex() # Treats mview as a bytes object. ``` +**[`Back To Contents`](#Contents)** Deque ----- @@ -2063,6 +2104,7 @@ from collections import deque .rotate(n=1) # Rotates elements to the right. ``` +**[`Back To Contents`](#Contents)** Threading --------- @@ -2131,6 +2173,7 @@ from queue import Queue = .get_nowait() # Raises queue.Empty exception if empty. ``` +**[`Back To Contents`](#Contents)** Operator -------- @@ -2155,6 +2198,7 @@ last_element = op.methodcaller('pop')() * **They only work on objects with and(), or(), xor() and invert() special methods.** * **Also: `' = &|^ '` and `' = &|^ '`.** +**[`Back To Contents`](#Contents)** Introspection ------------- @@ -2186,6 +2230,7 @@ from inspect import signature = .kind # Member of ParameterKind enum. ``` +**[`Back To Contents`](#Contents)** Metaprogramming --------------- @@ -2273,6 +2318,7 @@ MyMetaClass.__base__ == type # MyMetaClass is a subclass of type. +-------------+-------------+ ``` +**[`Back To Contents`](#Contents)** Eval ---- @@ -2284,6 +2330,7 @@ Eval ValueError: malformed node or string ``` +**[`Back To Contents`](#Contents)** Coroutines ---------- @@ -2350,6 +2397,7 @@ if __name__ == '__main__': ```
+**[`Back To Contents`](#Contents)** Libraries ========= @@ -2365,6 +2413,7 @@ Progress Bar Processing: 100%|████████████████████| 3/3 [00:03<00:00, 1.00s/it] ``` +**[`Back To Contents`](#Contents)** Plot ---- @@ -2378,6 +2427,7 @@ plt.show() # Displays the figure. plt.clf() # Clears the figure. ``` +**[`Back To Contents`](#Contents)** Table ----- @@ -2392,6 +2442,7 @@ with open('test.csv', encoding='utf-8', newline='') as file: print(table) ``` +**[`Back To Contents`](#Contents)** Curses ------ @@ -2421,6 +2472,7 @@ if __name__ == '__main__': wrapper(main) ``` +**[`Back To Contents`](#Contents)** Logging ------- @@ -2465,6 +2517,7 @@ retention=|| * **`''` - Max age of a file.** * **`''` - Max age as a string: `'1 week, 3 days'`, `'2 months'`, ...** +**[`Back To Contents`](#Contents)** Scraping -------- @@ -2489,6 +2542,7 @@ except requests.exceptions.ConnectionError: print("You've got problems with connection.", file=sys.stderr) ``` +**[`Back To Contents`](#Contents)** Web --- @@ -2541,6 +2595,7 @@ def send_json(sport): ['arsenal f.c.', 2.44, 3.29] ``` +**[`Back To Contents`](#Contents)** Profiling --------- @@ -2610,6 +2665,7 @@ with PyCallGraph(drawer): ``` +**[`Back To Contents`](#Contents)** NumPy ----- @@ -2710,6 +2766,7 @@ right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] # Shape: (3, 3) <- [1, 2, 1] ``` +**[`Back To Contents`](#Contents)** Image ----- @@ -2786,6 +2843,7 @@ from PIL import ImageDraw * **Use `'outline='` to set the color of the contours.** * **Colors can be specified as an int, tuple, `'#rrggbb[aa]'` string or a color name.** +**[`Back To Contents`](#Contents)** Animation --------- @@ -2806,6 +2864,7 @@ frames += reversed(frames[1:-1]) imageio.mimsave('test.gif', frames, duration=0.03) ``` +**[`Back To Contents`](#Contents)** Audio ----- @@ -2912,6 +2971,7 @@ engine.say('Sally sells seashells by the seashore.') engine.runAndWait() ``` +**[`Back To Contents`](#Contents)** Synthesizer ----------- @@ -2934,6 +2994,7 @@ samples_b = b''.join(struct.pack(' = .sum/max/mean() # Or: .apply/agg() ``` +**[`Back To Contents`](#Contents)** Plotly ------ @@ -3445,6 +3508,7 @@ if __name__ == '__main__': main() ``` +**[`Back To Contents`](#Contents)** PySimpleGUI ----------- @@ -3457,6 +3521,7 @@ event, values = window.read() print(f'Hello {values[0]}!' if event == 'Ok' else '') ``` +**[`Back To Contents`](#Contents)** Appendix -------- @@ -3532,9 +3597,12 @@ if __name__ == '__main__': main() ``` +**[`Back To Contents`](#Contents)** Index ----- * **Only available in the [PDF](https://transactions.sendowl.com/products/78175486/4422834F/view).** * **Ctrl+F / ⌘F is usually sufficient.** * **Searching `'#'` on the [webpage](https://gto76.github.io/python-cheatsheet/) will limit the search to the titles.** + +**[`Back To Contents`](#Contents)**