Last active
April 10, 2025 13:26
-
-
Save ragusa87/565337380f12b06e4c5b3a8b876ddb51 to your computer and use it in GitHub Desktop.
Python learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "5f5ad518-4f34-48f6-8283-b1d9876ae4ba", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"# Introduction\n", | |
"\n", | |
"I am reconverting myself from PHP to Python. This is some personal note about python that are helping me. Use if you already have knowledge with programming languages.\n", | |
"\n", | |
"You can edit this file with [Jupiter notebook](https://jupyter.org/).\n", | |
"\n", | |
"Hosted at <https://gist.github.com/ragusa87/565337380f12b06e4c5b3a8b876ddb51>\n", | |
"\n", | |
"\n", | |
"# To read\n", | |
"\n", | |
"* [Glossary](https://docs.python.org/3/glossary.html)\n", | |
"* [PEP 8](https://peps.python.org/pep-0008/) + [PEP](https://peps.python.org/pep-0000/#process-and-meta-peps)\n", | |
"* [Abstract Base Class](https://docs.python.org/3/library/abc.html#module-abc) (also PEP 3119)\n", | |
"* <https://docs.python.org/3/tutorial/index.html>\n", | |
"* <https://books.while.ch/books/138/fluent-python/read>\n", | |
"\n", | |
"# Best practices\n", | |
"\n", | |
"Use virtual-env instead of OS-specific packages.\n", | |
"\n", | |
"```bash\n", | |
"if [ ! -e \"$VIRTUAL_ENV/bin\" ]; then\n", | |
" echo \"Creating virtualenv at \\\"$VIRTUAL_ENV\\\"\"\n", | |
" python -m venv \"$VIRTUAL_ENV\"\n", | |
"fi\n", | |
"```\n", | |
"\n", | |
"Compile your requirements\n", | |
"\n", | |
"```bash\n", | |
"\n", | |
"if [ ! -e \"requirements/dev.txt\" ]; then\n", | |
" pip install pip-tools\n", | |
" pip-compile requirements/dev.in\n", | |
"fi\n", | |
"\n", | |
"pip install -r requirements/dev.txt\n", | |
"\n", | |
"if [ ! -e \"requirements/test.txt\" ]; then\n", | |
" pip-compile requirements/test.in\n", | |
"fi\n", | |
"```\n", | |
"Capture requirement\n", | |
"\n", | |
"```bash\n", | |
"pip freeze > requirements.txt\n", | |
"````\n", | |
"\n", | |
"Make sure pip is upgraded\n", | |
"\n", | |
"```bash\n", | |
"python -m ensurepip --upgrade\n", | |
"python -m venv \"$VIRTUAL_ENV\"\n", | |
"python -m pip install --upgrade pip\n", | |
"```\n", | |
"\n", | |
"Lint and analyse your code with tools\n", | |
"\n", | |
"Trendy:\n", | |
"* [uv](https://docs.astral.sh/uv/#highlights) An extremely fast Python package and project manager, written in Rust.\n", | |
"* [ruff](https://github.com/astral-sh/ruff) An extremely fast Python linter and code formatter, written in Rust.\n", | |
"\n", | |
"Old school:\n", | |
"* [black](https://pypi.org/project/black/) Formats your code consistently.\n", | |
"* [isort](https://pypi.org/project/isort/) Sorts your imports (use `profile = \"black\"`)\n", | |
"* [flake8](https://pypi.org/project/flake8/) Ensures compliance with PEP 8 + coding standards. \n", | |
"\n", | |
"Automate deployment\n", | |
"\n", | |
"* [Fabric](https://www.fabfile.org/)\n", | |
"\n", | |
"Package your app with a pyprojet.toml file\n", | |
"\n", | |
"* [pyproject-toml](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/)\n", | |
"\n", | |
"Test your app\n", | |
"\n", | |
"* [pytest](https://pytest.org)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a6fc439e-3138-4ca3-845f-fcc8aadba678", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"# Documentation (convention) - See PEP-8\n", | |
"\n", | |
"<https://docs.python.org/3/tutorial/controlflow.html#documentation-strings>\n", | |
"\n", | |
"Pass triple-quote comment after a function signature. Use Capital letter and end with a period.\n", | |
"The first non-blank line after the first line of the string determines the amount of indentation for the entire documentation string\n", | |
"\n", | |
"```python\n", | |
"def my_function():\n", | |
" \"\"\"Do nothing, but document it.\n", | |
"\n", | |
" No, really, it doesn't do anything.\n", | |
" \"\"\"\n", | |
" pass\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b14b4819-a3c2-4b33-8bf5-1c569c982317", | |
"metadata": {}, | |
"source": [ | |
"# Concepts" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "11406073-6671-4847-a44e-5d084b7a8aca", | |
"metadata": {}, | |
"source": [ | |
"## Coroutine function\n", | |
"Coroutine are like function, but they can be exited/entered at different points.\n", | |
"Ex: `await` and `async`" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f75c4f8e-9fb0-4515-8900-830d5f7e852b", | |
"metadata": {}, | |
"source": [ | |
"## comprehension\n", | |
"Create a new collection (such as a list, set, or dictionary) with the `for` keyword.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "ad73ee75-8aaf-43b4-bcd0-80852db45e3d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "2dd94a8a-137f-4ed2-bbe0-c10a125d7533", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[], [], []]" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[[] for i in range(3)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "1d17090c-5745-44b8-a64c-e0b09b34ab41", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"matrix = [\n", | |
" [1, 2, 3, 4],\n", | |
" [5, 6, 7, 8],\n", | |
" [9, 10, 11, 12],\n", | |
"]\n", | |
"[[row[i] for row in matrix] for i in range(4)]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "aa2ac001-3d1c-4664-898c-dc14186476a0", | |
"metadata": {}, | |
"source": [ | |
"## Copy\n", | |
"\n", | |
"<https://docs.python.org/3/library/copy.html>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "90d533c7-0c24-4bf8-b02c-a27eb8eba17d", | |
"metadata": {}, | |
"source": [ | |
"You can *deep copy* or *shadow copy* list. A shadow one will keep the list content untouched (referencing the same object) and a deep-copy will replace the content with a copy." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "4184bbe4-9f04-4f64-8f9b-89c1f1c32b49", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 4, [5, 6, 7]]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import copy\n", | |
"copy.deepcopy([1,2,4, [5,6,7]])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f81d9177-a542-4e0a-9451-8af1751197ad", | |
"metadata": {}, | |
"source": [ | |
"## Slices\n", | |
"Assignment to slices is also possible" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "5d0899c1-c1dd-47d8-b670-24e1a0faac12", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['a', 'b', 'C', 'D', 'E', 'f', 'g']" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", | |
"letters[2:5] = ['C', 'D', 'E']\n", | |
"letters" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c301c9cd-29d1-4af3-8c1f-ae614e5704e5", | |
"metadata": {}, | |
"source": [ | |
"## Special statements" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "00b601e2-027d-4033-89a8-b83aaa8889b7", | |
"metadata": {}, | |
"source": [ | |
"`else` can be used as clause on Loops" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8da9e1aa-25ee-4ea1-9890-52be8821d2f0", | |
"metadata": {}, | |
"source": [ | |
"`pass` statement => Does nothing. (No-operation)\n", | |
"\n", | |
"`del` Delete a variable\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "64df6a40-3ccb-45e2-b2e7-8bf094a7baec", | |
"metadata": {}, | |
"source": [ | |
"The `nonlocal` statement. (See also the scope chapter)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "f34ba59f-95f1-4b9c-b6c3-9b4671fcf73d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def outer_function():\n", | |
" count = 0 # Enclosing scope variable\n", | |
"\n", | |
" def inner_function():\n", | |
" nonlocal count # Referencing the count variable from the outer function\n", | |
" count += 1\n", | |
" print(\"Count inside inner function:\", count)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "63b33190-26b4-43f2-86cb-1e0cfec755df", | |
"metadata": {}, | |
"source": [ | |
"`match`\n", | |
"\n", | |
"Patterns can look like unpacking assignments, and can be used to bind variables:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "ef4b062a-b8ff-4db6-885a-74134cc5731d", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Y=2\n" | |
] | |
} | |
], | |
"source": [ | |
"point = (0,2)\n", | |
"match point:\n", | |
" case (0, 0):\n", | |
" print(\"Origin\")\n", | |
" case (0, y):\n", | |
" print(f\"Y={y}\")\n", | |
" case (x, 0):\n", | |
" print(f\"X={x}\")\n", | |
" case (x, y):\n", | |
" print(f\"X={x}, Y={y}\")\n", | |
" case _:\n", | |
" raise ValueError(\"Not a point\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "619ff095-a3e6-4eca-b6db-79057d5ae05d", | |
"metadata": {}, | |
"source": [ | |
"Subpatterns may be captured using the as keyword:\n", | |
"\n", | |
"```python\n", | |
"case (Point(x1, y1), Point(x2, y2) as p2): ...\n", | |
"```\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8903d9d0-2c35-45cd-bb1f-4a35a8ae2cd3", | |
"metadata": {}, | |
"source": [ | |
"## Built-in Functions\n", | |
"\n", | |
"<https://docs.python.org/3/library/functions.html#getattr>\n", | |
"\n", | |
"| **Function** | **Description** |\n", | |
"|---------------------|---------------------------------------------------------------------------------|\n", | |
"| `abs()` | Returns the absolute value of a number. |\n", | |
"| `all()` | Returns `True` if all elements in an iterable are true. |\n", | |
"| `any()` | Returns `True` if any element in an iterable is true. |\n", | |
"| `ascii()` | Returns a string representation of an object, escaping non-ASCII characters. |\n", | |
"| `bin()` | Converts an integer to its binary representation. |\n", | |
"| `bool()` | Converts a value to a boolean (`True` or `False`). |\n", | |
"| `bytearray()` | Returns a mutable sequence of bytes. |\n", | |
"| `bytes()` | Returns an immutable sequence of bytes. |\n", | |
"| `callable()` | Checks if the object is callable. |\n", | |
"| `chr()` | Returns the character corresponding to a Unicode code point. |\n", | |
"| `classmethod()` | Converts a method into a class method. |\n", | |
"| `compile()` | Compiles source code into a code object. |\n", | |
"| `complex()` | Creates a complex number. |\n", | |
"| `delattr()` | Deletes an attribute from an object. |\n", | |
"| `dict()` | Creates a dictionary. |\n", | |
"| `dir()` | Returns a list of attributes and methods of an object. |\n", | |
"| `divmod()` | Returns the quotient and remainder of division as a tuple. |\n", | |
"| `enumerate()` | Returns an enumerate object for an iterable, providing indexes and values. |\n", | |
"| `eval()` | Evaluates a Python expression from a string. |\n", | |
"| `exec()` | Executes dynamically created Python code. |\n", | |
"| `filter()` | Filters elements of an iterable based on a function. |\n", | |
"| `float()` | Converts a value to a floating-point number. |\n", | |
"| `format()` | Formats a value into a specified format. |\n", | |
"| `frozenset()` | Returns an immutable set. |\n", | |
"| `getattr()` | Gets the value of an attribute of an object. |\n", | |
"| `globals()` | Returns the dictionary of the current global symbol table. |\n", | |
"| `hasattr()` | Checks if an object has a specified attribute. |\n", | |
"| `hash()` | Returns the hash value of an object. |\n", | |
"| `help()` | Displays the help system for an object. |\n", | |
"| `hex()` | Converts an integer to a hexadecimal string. |\n", | |
"| `id()` | Returns the unique identifier of an object. |\n", | |
"| `input()` | Reads input from the user as a string. |\n", | |
"| `int()` | Converts a value to an integer. |\n", | |
"| `isinstance()` | Checks if an object is an instance of a class or subclass. |\n", | |
"| `issubclass()` | Checks if a class is a subclass of another class. |\n", | |
"| `iter()` | Returns an iterator object. |\n", | |
"| `len()` | Returns the length of an object. |\n", | |
"| `list()` | Creates a list. |\n", | |
"| `locals()` | Returns the dictionary of the current local symbol table. |\n", | |
"| `map()` | Applies a function to each item in an iterable. |\n", | |
"| `max()` | Returns the largest item in an iterable or among arguments. |\n", | |
"| `memoryview()` | Returns a memory view object. |\n", | |
"| `min()` | Returns the smallest item in an iterable or among arguments. |\n", | |
"| `next()` | Retrieves the next item from an iterator. |\n", | |
"| `object()` | Creates a new featureless object. |\n", | |
"| `oct()` | Converts an integer to an octal string. |\n", | |
"| `open()` | Opens a file and returns a file object. |\n", | |
"| `ord()` | Returns the Unicode code point of a character. |\n", | |
"| `pow()` | Returns the result of a number raised to a power. |\n", | |
"| `print()` | Prints objects to the console. |\n", | |
"| `property()` | Returns a property attribute for a class. |\n", | |
"| `range()` | Generates a sequence of numbers. |\n", | |
"| `repr()` | Returns a string representation of an object. |\n", | |
"| `reversed()` | Returns a reversed iterator. |\n", | |
"| `round()` | Rounds a number to a specified number of digits. |\n", | |
"| `set()` | Creates a set. |\n", | |
"| `setattr()` | Sets the value of an attribute of an object. |\n", | |
"| `slice()` | Returns a slice object. |\n", | |
"| `sorted()` | Returns a sorted list from an iterable. |\n", | |
"| `staticmethod()` | Converts a method into a static method. |\n", | |
"| `str()` | Converts a value to a string. |\n", | |
"| `sum()` | Returns the sum of all items in an iterable. |\n", | |
"| `super()` | Returns a proxy object for accessing a superclass method. |\n", | |
"| `tuple()` | Creates a tuple. |\n", | |
"| `type()` | Returns the type of an object or creates a new type. |\n", | |
"| `vars()` | Returns the `__dict__` attribute of an object. |\n", | |
"| `zip()` | Combines multiple iterables into a single iterator of tuples. |\n", | |
"\n", | |
"\n", | |
"`Zip` function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "ef7b7fcd-c5e3-4461-b3f2-d33c391999cf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 'sugar')\n", | |
"(2, 'spice')\n", | |
"(3, 'everything nice')\n" | |
] | |
} | |
], | |
"source": [ | |
"for item in zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):\n", | |
" print(item)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "31890d38-c52c-428a-9a2e-4172dfab047e", | |
"metadata": {}, | |
"source": [ | |
"## Compound statements\n", | |
"Compound statements contain (groups of) other statements" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "873cbb25-09a8-4c10-ab2d-0947391ae05c", | |
"metadata": { | |
"jupyter": { | |
"source_hidden": true | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3\n", | |
"33\n", | |
"333\n" | |
] | |
} | |
], | |
"source": [ | |
"x = 3\n", | |
"y = 33\n", | |
"z = 333\n", | |
"if x < y < z: print(x); print(y); print(z)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bf35e68a-5eee-4d7a-b7c0-147c56d2fa3b", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"## Context\n", | |
"Context can be create using the `with` statement.\n", | |
"\n", | |
"\n", | |
"The with statement is used to wrap the execution of a block with methods defined by a context manager (see [Context Managers](https://docs.python.org/3/reference/datamodel.html#context-managers). This allows common `try…except…finally` usage patterns to be encapsulated for convenient reuse.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "fa6978fc-281f-47ad-a97c-ce04e0e41c89", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"# Using 'with' to open a file for reading\n", | |
"with open('readme.ipynb', 'r') as file:\n", | |
" content = file.read() # Read the content of the file\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d056dc5a-bb64-421e-807a-3ff69fa62044", | |
"metadata": {}, | |
"source": [ | |
"## Arguments\n", | |
"\n", | |
"See <https://docs.python.org/3/glossary.html#term-parameter>\n", | |
"\n", | |
"* Positional\n", | |
"* Named (keyword)\n", | |
"\n", | |
"positional-only: specifies an argument that can be supplied only by position. Positional-only parameters can be defined by including a `/` character in the parameter list of the function definition after them, for example posonly1 and posonly2 in the following:\n", | |
"\n", | |
"```python\n", | |
"def func(posonly1, posonly2, /, positional_or_keyword): ...\n", | |
"```\n", | |
"\n", | |
"Keyword-only parameters can be defined by including a single var-positional parameter or bare `*` in the parameter list of the function definition before them\n", | |
"\n", | |
"```python\n", | |
"def func(arg, *, kw_only1, kw_only2): ...\n", | |
"```\n", | |
"\n", | |
"var-positional: specifies that an arbitrary sequence of positional arguments can be provided (in addition to any positional arguments already accepted by other parameters). Such a parameter can be defined by prepending the parameter name with *, for example args in the following:\n", | |
"\n", | |
"```python\n", | |
"def func(*args, **kwargs): ...\n", | |
"```\n", | |
"\n", | |
"Arbitrary Argument Lists: Use *\n", | |
"\n", | |
"```python\n", | |
"def concat(*args, sep=\"/\"):\n", | |
"```\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5b8c4ba8-7b80-4165-88d3-c3045cd46f08", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"## Unpacking" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e2b7a0fd-e789-49e7-b1f5-8ddbb038567e", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"def battery_status(voltage, state='idle'):\n", | |
" print(\"-- Voltage: \", voltage, \" State: \" , state)\n", | |
"\n", | |
"battery_status(**{\"voltage\": \"15v\", \"state\": \"Loading\"})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "695bc61f-50de-4f8f-95f0-d99b3abb21e7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"list(map(lambda x: x**2, range(10)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0a167583-cd65-41a0-8dec-9875deeaed1e", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"# Types\n", | |
"<https://docs.python.org/3/library/stdtypes.html#typesseq>\n", | |
"<https://docs.python.org/3/library/datatypes.html>\n", | |
"\n", | |
"`None` is equivalent to `null` (in some other language)\n", | |
"\n", | |
"## Sequence types\n", | |
"\n", | |
"\n", | |
"* array.array\n", | |
"* collections.deque\n", | |
"* list\n", | |
"* memoryview\n", | |
"* range\n", | |
"* tuple\n", | |
"* `collections.abc.Sequence`\n", | |
"\n", | |
"\n", | |
"| **Operation** | **Result** |\n", | |
"|------------------------|------------------------------------------------------------------|\n", | |
"| `x in s` | True if an item of `s` is equal to `x`, else False |\n", | |
"| `x not in s` | False if an item of `s` is equal to `x`, else True |\n", | |
"| `s + t` | The concatenation of `s` and `t` |\n", | |
"| `s * n` or `n * s` | Equivalent to adding `s` to itself `n` times |\n", | |
"| `s[i]` | `i`th item of `s`, origin 0 |\n", | |
"| `s[i:j]` | Slice of `s` from `i` to `j` |\n", | |
"| `s[i:j:k]` | Slice of `s` from `i` to `j` with step `k` |\n", | |
"| `len(s)` | Length of `s` |\n", | |
"| `min(s)` | Smallest item of `s` |\n", | |
"| `max(s)` | Largest item of `s` |\n", | |
"| `s.index(x[, i[, j]])` | Index of the first occurrence of `x` in `s` (at or after index `i` and before index `j`) |\n", | |
"| `s.count(x)` | Total number of occurrences of `x` in `s` |\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "33a43d55-0337-4c93-86d6-a11a2244f78c", | |
"metadata": {}, | |
"source": [ | |
"### tuple\n", | |
"Tuples are immutable sequences, typically used to store collections of heterogeneous data. `()`\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "355e7df7-92ac-41b3-98af-7ba2727a6281", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"tuple('abc')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c548eea4-4b45-4cec-b35c-916de7879240", | |
"metadata": {}, | |
"source": [ | |
"### range\n", | |
"The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "77b42bf6-0b7e-4f42-aa87-41725cdf978d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"list(range(0, 30, 5))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "abff16f6-0270-44aa-aa28-e7f713e82c7a", | |
"metadata": {}, | |
"source": [ | |
"### dict\n", | |
"A mapping object maps hashable values to arbitrary objects" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "a0c88334-cdca-4862-a025-2d0a5827a928", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dict(zip(['one', 'two', 'three'], [1, 2, 3]))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d41e5a03-8fe5-400f-8e5b-a8a653a21b65", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dict([('one', 1), ('two', 2), ('three', 3)])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3e56c7bf-69b1-4555-a3c2-3b716b2f46c6", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"list({\"one\": 1, \"two\": 2, \"three\": 3, \"four\": 4})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3850fe4f-0458-4af6-92e1-7f445c5f533a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"list({\"one\": 1, \"two\": 2, \"three\": 3, \"four\": 4}.values())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "30639ab6-50ce-4472-9d73-6fe854b9d602", | |
"metadata": {}, | |
"source": [ | |
"Access via `d[key]`\n", | |
"Raises a `KeyError` if key is not in the map.\n", | |
"\n", | |
"shallow copy via `copy()`\n", | |
"\n", | |
"A ready-only view can be created via `MappingProxyType`\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "dd236aba-44e9-4aec-81f3-b42c7ec1210a", | |
"metadata": {}, | |
"source": [ | |
"### set\n", | |
"A set is an unordered collection with no duplicate elements\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b1ac2d90-b4a4-4305-8452-097a7bffbb63", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"set('abracadabra')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2a91829d-991f-4c5f-8b36-9ac51c40a06f", | |
"metadata": {}, | |
"source": [ | |
"### frozenset\n", | |
"Same as set but immutable. Once created, elements cannot be added or removed." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4f7307f3-d9ea-4e7a-bf70-caa3b9580caf", | |
"metadata": {}, | |
"source": [ | |
"### Lists (mutable)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "080963f3-0abe-46ad-a9cb-290a69bc4b0e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a = [1,2,3] + [4]\n", | |
"print(a)\n", | |
"b = [1,2,3].append(4)\n", | |
"b" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "975e5c26-c289-446f-b61f-71d7cdec1cbd", | |
"metadata": {}, | |
"source": [ | |
"You can use lists as Queue, stack, etc. See <https://docs.python.org/3/tutorial/datastructures.html>\n", | |
"\n", | |
"> Note that items in the sequence s are not copied; they are referenced multiple times" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0f2f5c5e-ef10-48ed-a1e2-2abcd9844f19", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lists = [[]] * 3\n", | |
"lists[0].append(3)\n", | |
"lists" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "aa682c05-2986-4c79-bd86-590d897efeb0", | |
"metadata": {}, | |
"source": [ | |
"## str - Strings (immutable)\n", | |
"Operator such as `*`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "896d5272-bb09-4f63-b7a2-e1106e6d877f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"3 * \"a\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2b3cda3c-ec8b-4c9e-a444-66e133ecbd1a", | |
"metadata": {}, | |
"source": [ | |
"Slicing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b7aad4a0-c62c-4c8f-834c-81ec7d1ddc11", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"'Python'[-2:]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d8f2e220-b349-4998-b0b5-a3b7d28a108d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\"Python\"[0::2] # first : end : step" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d4072fe0-398a-43af-b0ca-c90242eec49e", | |
"metadata": {}, | |
"source": [ | |
"Formatting string using \"format\" see <https://docs.python.org/3/library/string.html#formatspec>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f9acca10-45d5-4acd-b189-34e9d08e41b3", | |
"metadata": {}, | |
"source": [ | |
"## Datetime\n", | |
"\n", | |
"* <https://docs.python.org/3/library/datetime.html#format-codes>\n", | |
"* <https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime>\n", | |
"\n", | |
"Only one concrete tzinfo class, the timezone class, is supplied by the datetime module.\n", | |
"\n", | |
" * datetime.UTC - Alias for the UTC time zone singleton datetime.timezone.utc.\n", | |
"\n", | |
"```\n", | |
"object\n", | |
" timedelta\n", | |
" tzinfo\n", | |
" timezone\n", | |
" time\n", | |
" date\n", | |
" datetime\n", | |
"````\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3b688019-165f-43fa-afcc-eac253a87ded", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from datetime import datetime\n", | |
"import pytz\n", | |
"datetime.now(tz=pytz.timezone(\"Europe/Zurich\"))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ea8d31d1-000c-43fc-a0bf-ea0369b648da", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from datetime import date\n", | |
"today = date.today()\n", | |
"today" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ee6db768-54e9-478f-88fe-b3940304e31c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from datetime import date\n", | |
"\n", | |
"d = date(2002, 12, 31)\n", | |
"\n", | |
"d.replace(day=26)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "feba3708-5eff-40e9-96a7-f727c9af3b3f", | |
"metadata": {}, | |
"source": [ | |
"See also [Calendar class](https://docs.python.org/3/library/calendar.html)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "cc3c3ef8-a9b0-4bdb-b7dc-79f789da0303", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"## Lambda\n", | |
"\n", | |
"Small anonymous functions can be created with the lambda keyword\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "dfd2c578-14d4-4bb0-bfb1-7c4409aa17aa", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def make_incrementor(n):\n", | |
" return lambda x: x + n\n", | |
"f = make_incrementor(32)\n", | |
"\n", | |
"f(10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7844b41f-222f-4d6a-9d43-4809e3c9785d", | |
"metadata": {}, | |
"source": [ | |
"## Generics\n", | |
"Functions (including coroutines), classes and type aliases may contain a type parameter. Generics can also be used.\n", | |
"\n", | |
"```python\n", | |
"type ListOrSet[T] = list[T] | set[T]\n", | |
"```\n", | |
"\n", | |
"```python\n", | |
"def max[T](args: list[T]) -> T:\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7e4123d9-63cf-4293-b3a2-edb11c2c3afb", | |
"metadata": {}, | |
"source": [ | |
"## Enum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "79f35232-b20d-4684-a351-9e0a8034b315", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from enum import Enum\n", | |
"class Color(Enum):\n", | |
" RED = 1\n", | |
" GREEN = 2\n", | |
" BLUE = 3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "6c8c45b6-4ba3-48e3-b9bc-649d43a600a9", | |
"metadata": {}, | |
"source": [ | |
"## Generic\n", | |
"The `type` statement can also be used to create a generic type alias." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "fb810ffa-e83e-4d2e-8015-06727581915e", | |
"metadata": {}, | |
"source": [ | |
"## Struct \n", | |
"\n", | |
"Interpret bytes as packed binary data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0178357e-4637-4fde-9d9b-b4e2b3bbd85f", | |
"metadata": { | |
"jupyter": { | |
"source_hidden": true | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import struct\n", | |
"\n", | |
"struct.pack('>h', 1023)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "19c895a0-3c63-47ad-991e-ddce266b3c0c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from collections import namedtuple\n", | |
"Point = namedtuple('Point', ['x', 'y'])\n", | |
"p = Point(11, y=22) \n", | |
"p" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b793178a-b037-4a75-be95-92f820ffb2dc", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from collections import Counter\n", | |
"cnt = Counter()\n", | |
"for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:\n", | |
" cnt[word] += 1\n", | |
"\n", | |
"cnt\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "86241b56-578a-492e-a884-f2023c8b119d", | |
"metadata": {}, | |
"source": [ | |
"# Looping Techniques\n", | |
"\n", | |
"* items()\n", | |
"* enumerate()\n", | |
"* reversed()\n", | |
"* sorted()\n", | |
"* .." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "6d9e9a16-bdae-4778-962b-390e00bd5b58", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for i, v in enumerate(['tic', 'tac', 'toe']):\n", | |
" print(i, v)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5b17ad3c-4a98-41a5-856a-86af87d16d38", | |
"metadata": {}, | |
"source": [ | |
"Remove duplicates with set + sorted:\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f4359c8e-6771-435a-ac16-b58eeb63f974", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n", | |
"sorted(set(basket))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "acf7fc8e-3896-4aac-a294-6f5509e0ce55", | |
"metadata": {}, | |
"source": [ | |
"# Import\n", | |
"\n", | |
"```python\n", | |
"from fibo import fib, fib2\n", | |
"from fibo import fib as fibonacci\n", | |
"```\n", | |
"\n", | |
"## Modules\n", | |
"\n", | |
"\n", | |
"\n", | |
"the interpreter first searches for a built-in module (see `sys.builtin_module_names`). If not found, it then searches for a file named `fib.py` in a list of directories given by the variable `sys.path`.\n", | |
"\n", | |
"`sys.path` is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable `PYTHONPATH`,\n", | |
"\n", | |
"\n", | |
"“Compiled” Python files are kept at __pycache__ dir.\n", | |
"\n", | |
"Currently defined model can be listed using `dir()`.\n", | |
"\n", | |
"Also see\n", | |
"```python\n", | |
"import builtins\n", | |
"```\n", | |
"\n", | |
"\n", | |
"## Packages\n", | |
"Packages are a way of structuring Python’s module namespace by using “dotted module names”\n", | |
"\n", | |
"\n", | |
"The `__init__.py` files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature). \n", | |
"\n", | |
"In the simplest case, `__init__.py` can just be **an empty file**, but it can also execute initialization code for the package or set the `__all__` variable.\n", | |
"\n", | |
"```python\n", | |
"__all__ = [\"echo\", \"surround\", \"reverse\"]\n", | |
"```\n", | |
"All is used as wildcard when you use code like this `from sound.effects import *`\n", | |
"\n", | |
"\n", | |
"You can use dotted notation too.\n", | |
"\n", | |
"```python\n", | |
"from . import echo\n", | |
"from .. import formats\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7aa342ed-d625-430b-9f14-01ec0b7bfda9", | |
"metadata": {}, | |
"source": [ | |
"# Input and Output" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ba3f13a8-d059-48a3-9e2d-c58c118f47c1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"year = 2016\n", | |
"event = 'Referendum'\n", | |
"f'Results of the {year} {event}'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b5fe7846-c9d4-470b-805c-dce56bea1885", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"'{:-9} YES votes, {:2.2%}'.format(42_572_654, 85_705_149)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "86969c7d-ce56-4e19-85ea-5568b2a1b670", | |
"metadata": {}, | |
"source": [ | |
"Use `rep()` for the object representation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "5ccee2bc-7cfb-4953-a9ec-986e890f67f7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"repr((1,2,3,4))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "240f7c64-9399-45db-8183-4ed1e85e4599", | |
"metadata": {}, | |
"source": [ | |
"Passing an integer after the `:` will cause that field to be a minimum number of characters wide." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e6605625-243e-4bd6-8c41-f2aaf6493312", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}\n", | |
"\n", | |
"for name, phone in table.items():\n", | |
" print(f'{name:10} ==> {phone:10d}')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e90750ec-34aa-4b22-9484-f0e5bbaa6c3a", | |
"metadata": {}, | |
"source": [ | |
"* `str.rjust()` : right-justifies a string in a field of a given width by padding it with spaces on the left" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d195ba2b-33e9-488d-a2b6-73284bfd2270", | |
"metadata": {}, | |
"source": [ | |
"## JSON" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f94c047b-2282-47c9-a5f1-816467c04719", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"\n", | |
"x = [1, 'simple', 'list']\n", | |
"\n", | |
"json.dumps(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3e9299f9-1573-4e5c-85e4-f8f3fe5b6fb2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"json.loads('[\"a\", \"simple\", \"list\"]') # loads is for string, load is for file." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5b2e8391-343f-43af-8f04-b2e3f10dbf57", | |
"metadata": {}, | |
"source": [ | |
"See also `pickle` to handle serialization." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a511ef86-383b-48f0-ad48-52ce1e9dba7e", | |
"metadata": {}, | |
"source": [ | |
"# Exceptions\n", | |
"Read:\n", | |
"* [Tutorial](https://docs.python.org/3/tutorial/errors.html)\n", | |
"* [Build in exceptions](https://docs.python.org/3/library/exceptions.html#bltin-exceptions)\n", | |
"\n", | |
"Example:\n", | |
"* ZeroDivisionError\n", | |
"* NameError\n", | |
"* TypeError" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1b560812-dbd3-4677-801a-0566d6f0896f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"try:\n", | |
" pass # do\n", | |
"except* TypeError as e:\n", | |
" print(f'caught {type(e)} with nested {e.exceptions}')\n", | |
"except* OSError as e:\n", | |
" print(f'caught {type(e)} with nested {e.exceptions}')\n", | |
" pass # fallback\n", | |
" raise TypeError\n", | |
"finally:\n", | |
" pass # -- close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "05760416-c5a1-4fef-89a7-9a38332dc76d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"try:\n", | |
" with open('readme-example.ipynb', 'r') as file:\n", | |
" content = file.read()\n", | |
"except FileNotFoundError:\n", | |
" print(\"Error: The file was not found.\")\n", | |
"except PermissionError:\n", | |
" print(\"Error: You don't have permission to read the file.\")\n", | |
"except Exception as e:\n", | |
" print(f\"An unexpected error occurred: {e}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ab024762-ed76-4314-be1f-2e40cfd439be", | |
"metadata": {}, | |
"source": [ | |
"# The Python Standard Library\n", | |
"\n", | |
"Lots of librairies are available with Python. I will detail only some of them.\n", | |
"\n", | |
"<https://docs.python.org/3/library/index.html>\n", | |
"\n", | |
"* Numeric Types — int, float, complex\n", | |
"* Bitwise Operations on Integer Types (`| & ^ << >>`)\n", | |
"* Boolean Type - bool\n", | |
"* Iterator Types (`__iter__()`)\n", | |
"* Generator Types (iter + yield)\n", | |
"* Sequence Types — list, tuple, range\n", | |
"* Mutable vs immutable\n", | |
"* Lists, Tuples, Range, etc\n", | |
"* String methods. etc.\n", | |
"* Binary Sequence Types — bytes, bytearray, memoryview\n", | |
"* Set Types — set, frozenset\n", | |
"* Mapping Types — dict\n", | |
"* Context Manager Types\n", | |
"* Generic Alias Type\n", | |
"* Union Type" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "93c15ca9-1b9e-47b6-af21-144b6a401ce1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def square(number: int | float) -> int | float:\n", | |
" return number ** 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e4374f1c-a892-4b85-b252-ff600b02a55e", | |
"metadata": {}, | |
"source": [ | |
"* etc." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "27f56205-26c7-4356-9611-c5801b338286", | |
"metadata": {}, | |
"source": [ | |
"## Text Processing Services\n", | |
"<https://docs.python.org/3/library/text.html>\n", | |
"\n", | |
"## Regular expression\n", | |
"<https://docs.python.org/3/library/re.html>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1c4f9e8a-fa01-43aa-8e2b-9986e6bf54b0", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import re\n", | |
"m = re.search('(?<=abc)def', 'abcdef')\n", | |
"m.group(0)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "64ac7dfc-9847-4733-8a95-955ab5b2d4f7", | |
"metadata": {}, | |
"source": [ | |
"## difflib — Helpers for computing deltas\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "03984eb7-1b8f-4b47-a7e4-137e86b77904", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"from difflib import *\n", | |
"\n", | |
"s1 = ['bacon\\n', 'eggs\\n', 'ham\\n', 'guido\\n']\n", | |
"s2 = ['python\\n', 'eggy\\n', 'hamster\\n', 'guido\\n']\n", | |
"\n", | |
"sys.stdout.writelines(context_diff(s1, s2))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "7b1fbfb6-9da4-4604-a10b-193791dd13fb", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# textwrap — Text wrapping and filling" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "cd0a4942-f029-4ffd-a9b6-8855dfc63b38", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import textwrap\n", | |
"textwrap.shorten(\"Hello world!\", width=11)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "eea74fb7-f2f2-43d9-8f99-a8761c3d4aa1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# unicodedata" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f39b01f7-d1ba-4c33-9ee8-370c61c1a645", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import unicodedata\n", | |
"unicodedata.lookup('LEFT CURLY BRACKET')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "497a4612-15db-4e9c-acee-88ba029a1d65", | |
"metadata": {}, | |
"source": [ | |
"## Binary Data Services\n", | |
"\n", | |
"<https://docs.python.org/3/library/binary.html>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "81ed8b66-58a8-4d72-8159-fc41beb96ab1", | |
"metadata": {}, | |
"source": [ | |
"## codecs — Codec registry and base classes\n", | |
"(Encoding string for example)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f7ac202d-96b1-45c9-b354-d3210d0163c4", | |
"metadata": {}, | |
"source": [ | |
"## Scope\n", | |
"A scope is a textual region of a Python program where a namespace is directly accessible.\n", | |
"A special quirk of Python is that – if no `global` or `nonlocal` statement is in effect – assignments to names always go into the innermost scope.\n", | |
"\n", | |
"\n", | |
"If a name is declared global, then all references and assignments go directly to the \"next-to-last\" scope containing the module’s global names. To rebind variables found outside of the innermost scope, the `nonlocal` statement can be used; if not declared nonlocal, those variables are read-only **an attempt to write to such a variable will simply create a new local variable in the innermost scope**, leaving the identically named outer variable unchanged).\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a6618839-8279-4672-899e-56b54eb7ee7c", | |
"metadata": {}, | |
"source": [ | |
"def scope_test():\n", | |
" def do_local():\n", | |
" spam = \"local spam\"\n", | |
"\n", | |
" def do_nonlocal():\n", | |
" nonlocal spam\n", | |
" spam = \"nonlocal spam\"\n", | |
"\n", | |
" def do_global():\n", | |
" global spam\n", | |
" spam = \"global spam\"\n", | |
"\n", | |
" spam = \"test spam\"\n", | |
" do_local()\n", | |
" print(\"After local assignment:\", spam) # test spam\n", | |
" do_nonlocal()\n", | |
" print(\"After nonlocal assignment:\", spam) # nonlocale spam\n", | |
" do_global()\n", | |
" print(\"After global assignment:\", spam) # nonlocale spam\n", | |
"\n", | |
"scope_test() # global spam\n", | |
"print(\"In global scope:\", spam)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "7cd0117d-769a-4f96-9788-2c06fd94d09c", | |
"metadata": {}, | |
"source": [ | |
"## collections\n", | |
"\n", | |
"This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, `dict`, `list`, `set`, and `tuple`.\n", | |
"\n", | |
"| Collection Type | Description |\n", | |
"|-------------------|------------------------------------------------------------------|\n", | |
"| `namedtuple()` | Factory function for creating tuple subclasses with named fields |\n", | |
"| `deque` | The name is pronounced “deck” and is short for “double-ended queue\" |\n", | |
"| `ChainMap` | Dict-like class for creating a single view of multiple mappings. It is often much faster than creating a new dictionary and running multiple update() calls. |\n", | |
"| `Counter` | Dict subclass for counting hashable objects |\n", | |
"| `OrderedDict` | Dict subclass that remembers the order entries were added. They have become **less important** now that the built-in dict class gained the ability to remember insertion order |\n", | |
"| `defaultdict` | Dict subclass that calls a factory function to supply missing values |\n", | |
"| `UserDict` | Wrapper around dictionary objects for easier dict subclassing |\n", | |
"| `UserList` | Wrapper around list objects for easier list subclassing |\n", | |
"| `UserString` | Wrapper around string objects for easier string subclassing |\n", | |
"| `collections.abc` | See the ABC chapter |\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "961abdec-0221-4213-b55d-9a01c8ca418b", | |
"metadata": {}, | |
"source": [ | |
"## heapq — Heap queue algorithm\n", | |
"\n", | |
"Heaps are binary trees for which every parent node has a value less than or equal to any of its children" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d438b72b-b91b-43cd-ad26-dce5ff0972fe", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from heapq import heappush, heappop\n", | |
"def heapsort(iterable):\n", | |
" h = []\n", | |
" for value in iterable:\n", | |
" heappush(h, value)\n", | |
" return [heappop(h) for i in range(len(h))]\n", | |
"\n", | |
"heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "d94f318b-c7a7-4a3b-8fd9-11a0ff2804d3", | |
"metadata": {}, | |
"source": [ | |
"## bisect\n", | |
"\n", | |
"* <https://docs.python.org/3/library/bisect.html>\n", | |
" \n", | |
"This module provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over linear searches or frequent resorting.\n", | |
"\n", | |
"The module is called bisect because it uses a basic bisection algorithm to do its work. " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5900c55b-fe24-4f7f-a264-f014adfdcd92", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"source": [ | |
"## Array \n", | |
"\n", | |
"[array — Efficient arrays of numeric values](https://docs.python.org/3/library/array.html)\n", | |
"\n", | |
"\n", | |
"| Typecode | C Type | Python Type | Description |\n", | |
"|----------|------------------|---------------|---------------------------------|\n", | |
"| `b` | signed char | int | Represents an integer (1 byte) |\n", | |
"| `B` | unsigned char | int | Represents an integer (1 byte) |\n", | |
"| `u` | wchar_t | Unicode char | Represents a Unicode character |\n", | |
"| `h` | signed short | int | Represents an integer (2 bytes)|\n", | |
"| `H` | unsigned short | int | Represents an integer (2 bytes)|\n", | |
"| `i` | signed int | int | Represents an integer (4 bytes)|\n", | |
"| `I` | unsigned int | int | Represents an integer (4 bytes)|\n", | |
"| `l` | signed long | int | Represents an integer (4 bytes)|\n", | |
"| `L` | unsigned long | int | Represents an integer (4 bytes)|\n", | |
"| `q` | signed long long | int | Represents an integer (8 bytes)|\n", | |
"| `Q` | unsigned long long | int | Represents an integer (8 bytes)|\n", | |
"| `f` | float | float | Represents a floating-point number (4 bytes) |\n", | |
"| `d` | double | float | Represents a floating-point number (8 bytes) |\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e0ff8878-f950-479a-b72e-ae6c74078bfd", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from array import array\n", | |
"a = array('l', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])\n", | |
"a.count(5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8c9eaded-a2ce-4476-9c34-cd17a5b1a905", | |
"metadata": {}, | |
"source": [ | |
"## weakref\n", | |
"\n", | |
"The weakref module allows the Python programmer to create weak references to objects." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "07dbdd8a-4cca-4b68-9569-7c8cdf9bfe03", | |
"metadata": {}, | |
"source": [ | |
"## typing\n", | |
"\n", | |
"* <https://docs.python.org/3/library/types.html#standard-interpreter-types>\n", | |
"\n", | |
"Dynamic type creation and names for built-in types\n", | |
"\n", | |
"\n", | |
"| Type Name | Description |\n", | |
"|-----------------------|------------------------------------------------|\n", | |
"| `types.NoneType` | The type of the `None` object. |\n", | |
"| `types.FunctionType` | The type of regular functions (`def`). |\n", | |
"| `types.LambdaType` | The type of lambda functions. |\n", | |
"| `types.MethodType` | The type of methods of objects. |\n", | |
"| `types.BuiltinFunctionType` | The type of built-in functions like `len`. |\n", | |
"| `types.BuiltinMethodType` | The type of built-in methods. |\n", | |
"| `types.ModuleType` | The type of Python modules. |\n", | |
"| `types.TracebackType` | The type of traceback objects. |\n", | |
"| `types.FrameType` | The type of stack frames. |\n", | |
"| `types.CodeType` | The type of code objects (e.g., compiled functions). |\n", | |
"| `types.GeneratorType` | The type of generator objects. |\n", | |
"| `types.CoroutineType` | The type of coroutine objects. |\n", | |
"| `types.AsyncGeneratorType` | The type of asynchronous generator objects. |\n", | |
"| `types.MappingProxyType` | A read-only view of a dictionary. |\n", | |
"| `types.SimpleNamespace` | A simple object for attribute assignments. |\n", | |
"\n", | |
"\n", | |
"`TypeVar`\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "6aadd4bc-4a75-4219-a14f-e8a04d749d1d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from typing import TypeVar, Generic\n", | |
"\n", | |
"T = TypeVar(\"T\") # Define a type variable that can be any type\n", | |
"\n", | |
"class Foo(Generic[T]):\n", | |
" def __init__(self, value: T):\n", | |
" self.value = value\n", | |
"\n", | |
" def get_value(self) -> T:\n", | |
" \"\"\"Returns the stored value, with type T.\"\"\"\n", | |
" return self.value\n", | |
"\n", | |
"foo_int = Foo(42)\n", | |
"print(foo_int.get_value())\n", | |
"\n", | |
"foo_str = Foo(\"Hello, Generics!\")\n", | |
"print(foo_str.get_value())\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ee721c14-3002-46b9-a220-102da4d900f2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## pprint\n", | |
"\n", | |
"Data pretty printer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "875c6419-3d2f-4caa-8d80-1adcefd332e8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pprint\n", | |
"stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']\n", | |
"\n", | |
"pp = pprint.PrettyPrinter(width=10, compact=False)\n", | |
"pp.pprint(stuff)\n", | |
"stuff\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b1b68f58-3fb8-4cc7-aec0-82ed88ca5350", | |
"metadata": {}, | |
"source": [ | |
"## reprlib\n", | |
"\n", | |
"Alternate `repr()` implementation¶\n", | |
"\n", | |
"<https://docs.python.org/3/library/reprlib.html>\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "717ca0b6-65f9-4f00-9ce6-e9ca39e55323", | |
"metadata": {}, | |
"source": [ | |
"## Math & Number\n", | |
"\n", | |
"<https://docs.python.org/3/library/numeric.html>\n", | |
"\n", | |
"* The `numbers` module defines an abstract hierarchy of numeric types.\n", | |
"* The `math` and `cmath` modules contain various mathematical functions for floating-point and complex numbers.\n", | |
"* The `decimal` module supports exact representations of decimal numbers, using arbitrary precision arithmetic.\n", | |
"* The `random` module generate pseudo-random numbers.\n", | |
"* The `statistics` module handle mathematical statistics functions." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "94bc2707-7599-4c3c-8b6c-f1d8998380e8", | |
"metadata": {}, | |
"source": [ | |
"## Functional Programming Modules\n", | |
"\n", | |
"<https://docs.python.org/3/library/functional.html>\n", | |
"\n", | |
"\n", | |
"* `itertools` — Functions creating iterators for efficient looping\n", | |
"* `functools` — Higher-order functions and operations on callable objects\n", | |
"* `operator` — Standard operators as functions\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a2d60b2a-bf05-42a1-9af5-5db0a781f425", | |
"metadata": {}, | |
"source": [ | |
"## pathlib\n", | |
"\n", | |
"Object-oriented filesystem paths\n", | |
"\n", | |
"* <https://docs.python.org/3/library/pathlib.html>\n", | |
"\n", | |
"Less advanced alternative is [os.path](https://docs.python.org/3/library/os.path.html)\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b8191704-0b14-400d-b04e-a1fe9afd381c", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pathlib import Path\n", | |
"p = Path('.')\n", | |
"[x for x in p.iterdir() if x.is_dir()]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "db5f96e4-02f5-440f-a371-76ff7652058b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pathlib import PurePosixPath\n", | |
"PurePosixPath('/etc').joinpath('hosts')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ab7606ce-af52-4966-9308-49fc85d39511", | |
"metadata": {}, | |
"source": [ | |
"## filecmp\n", | |
"\n", | |
"File and Directory Comparisons\n", | |
"<https://docs.python.org/3/library/filecmp.html>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b30e9019-1555-4733-a356-753d0a1cad95", | |
"metadata": {}, | |
"source": [ | |
"## Data Persistence\n", | |
"\n", | |
"<https://docs.python.org/3/library/persistence.html>\n", | |
"\n", | |
"* `pickle` — Python object serialization\n", | |
"* `dbm` database with implementations such as `dbm.sqlite3` — SQLite backend for dbm\n", | |
"* `shelve` — Python object persistence\n", | |
"* `marshal` — Internal Python object serialization\n", | |
"* `sqlite3` — DB-API 2.0 interface for SQLite databases" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b447c125-204d-4fe7-a2bf-ba6fa240c6d5", | |
"metadata": {}, | |
"source": [ | |
"## Data Compression and Archiving\n", | |
"\n", | |
"<https://docs.python.org/3/library/archiving.html>\n", | |
"\n", | |
"* `zlib`, `gzip`, `bzip2` and `lzma` algorithms\n", | |
" \n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "57654165-014f-401e-afec-df1e5905f681", | |
"metadata": {}, | |
"source": [ | |
"## File Formats\n", | |
"\n", | |
"<https://docs.python.org/3/library/fileformats.html>\n", | |
"\n", | |
"* `csv` - CSV File Reading and Writing\n", | |
"* `configparser` - Configuration file parser\n", | |
"* `tomllib` - Parse TOML files\n", | |
"* `netrc` - netrc file processing\n", | |
"* `plistlib` - Generate and parse Apple .plist files" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "783222f6-e19e-4de1-aa03-2356c7e1c6d3", | |
"metadata": {}, | |
"source": [ | |
"## Cryptographic Services\n", | |
"\n", | |
"<https://docs.python.org/3/library/crypto.html>\n", | |
"\n", | |
"* `hashlib` - Secure hashes and message digests\n", | |
"* `hmac` - Keyed-Hashing for Message Authentication\n", | |
"* `secrets` - Generate secure random numbers for managing secrets\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "477d4886-e9e7-45dd-bb23-501a3dea8d17", | |
"metadata": {}, | |
"source": [ | |
"## Internet Protocols and Support\n", | |
"\n", | |
"<https://docs.python.org/3/library/internet.html>\n", | |
"\n", | |
"| Python Module | Description |\n", | |
"|--------------------------|-----------------------------------------------------------------------------|\n", | |
"| [http.client](https://docs.python.org/3/library/http.client.html) | Provides classes for HTTP client functionality, including making requests. |\n", | |
"| [urllib](https://docs.python.org/3/library/urllib.html) | A module for working with URLs and making HTTP requests, including secure ones. |\n", | |
"| [requests](https://requests.readthedocs.io/en/latest/) | A popular third-party library for simplifying HTTP requests and handling responses. |\n", | |
"| [ftplib](https://docs.python.org/3/library/ftplib.html) | A built-in module for FTP client functionality, enabling file transfers over FTP. |\n", | |
"| [paramiko](http://docs.paramikro.org/) | A third-party library providing SSH and SFTP support for secure remote connections and file transfers. |\n", | |
"| [smtplib](https://docs.python.org/3/library/smtplib.html) | A built-in module for sending emails via the Simple Mail Transfer Protocol (SMTP). |\n", | |
"| [poplib](https://docs.python.org/3/library/poplib.html) | A built-in module for retrieving email messages from a POP3 server. |\n", | |
"| [imaplib](https://docs.python.org/3/library/imaplib.html) | A built-in module for interacting with mail servers using the IMAP protocol. |\n", | |
"| [telnetlib](https://docs.python.org/3/library/telnetlib.html) | A built-in module for Telnet client functionality to interact with remote servers. |\n", | |
"| [socket](https://docs.python.org/3/library/socket.html) | A built-in module providing low-level networking interfaces for TCP, UDP, and other protocols. |\n", | |
"\n", | |
"And more..\n", | |
"\n", | |
"| Third-party Python Module | Description |\n", | |
"|--------------------------|-----------------------------------------------------------------------------|\n", | |
"| [websockets](https://websockets.readthedocs.io/en/stable/) | A third-party library for WebSocket protocol, supporting full-duplex communication. |\n", | |
"| [scapy](https://scapy.readthedocs.io/en/latest/) | A powerful third-party library for network packet manipulation, supporting protocols like ICMP, ARP, and more. |\n", | |
"| [paho-mqtt](https://www.eclipse.org/paho/index.php?page=docs/index.php) | A third-party MQTT client library for lightweight messaging, typically used in IoT. |\n", | |
"| [dns.resolver](https://dnspython.readthedocs.io/en/latest/) | A third-party library for DNS resolution and querying. |\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "2bdb3e99-67bc-4fc9-a7f3-cbf536cf7c30", | |
"metadata": {}, | |
"source": [ | |
"## Debug\n", | |
"\n", | |
"<https://docs.python.org/3/library/debug.html>\n", | |
"\n", | |
"The bdb module handles basic debugger functions, like setting breakpoints or managing execution via the debugger.\n", | |
"\n", | |
"* <https://www.jetbrains.com/help/pycharm/part-1-debugging-python-code.html#start-debugger-session>\n", | |
"* <https://code.visualstudio.com/docs/python/debugging>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3b8df3f1-6334-4458-9a0d-6dec509b81d3", | |
"metadata": {}, | |
"source": [ | |
"## unittest\n", | |
"\n", | |
"* unittest — Unit testing framework\n", | |
"* unittest.mock — mock object library <https://docs.python.org/3/library/unittest.mock.html>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "38fbf55f-e121-439b-94ce-40739f1d7334", | |
"metadata": {}, | |
"source": [ | |
"# Class\n", | |
"As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be **modified further after creation**. It is a mixture of the class mechanisms found in C++ and Modula-3\n", | |
"\n", | |
"Class definitions play some neat tricks with namespaces, and you need to know how scopes and namespaces work to fully understand what’s going on. Incidentally, knowledge about this subject is useful for any advanced Python programmer.\n", | |
"\n", | |
"Writable attributes may be deleted with the `del` statement. \n", | |
"\n", | |
"\n", | |
"`new` keyword: Calling the class name with arguments will constructs and returns an instance => **there is no new keyword**.\n", | |
"\n", | |
"`private`: By convention named with `__`. The double underscore triggers name mangling, meaning the method name is changed internally (e.g., __private_method becomes _MyClass__private_method), making it harder to accidentally access it from outside the class.\n", | |
"\n", | |
"`protected`: By convention named with `_`. The single underscore is a convention that suggests the method is protected, meaning it should be used within the class or by subclasses, but it won't prevent access directly (it’s just a hint to developers).\n", | |
"\n", | |
"\n", | |
"`extends`: There is no extends keyword. But you can use inheritance like that:\n", | |
"```python\n", | |
"class Child(Parent):\n", | |
"```\n", | |
"\n", | |
"`interface`: There is no interface keyword. Instead use an Abstract Base Classes (ABCs)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "911deb99-c79e-488a-bc48-af57b2f9441b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Foo(object):\n", | |
" pass\n", | |
"\n", | |
"f = Foo()\n", | |
"type(f) " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "62a050fd-eb25-41a3-9e84-813da445664b", | |
"metadata": {}, | |
"source": [ | |
"Usage of `isinstance()` and `issubclass()` via Abstract Base Classes (ABC), see also `type(obj)`\n", | |
"\n", | |
"\n", | |
"### Duck Typing\n", | |
"\n", | |
"As an aternative to ABC classes, Python also uses duck typing as part of its dynamic nature. With duck typing, the type or class of an object is determined by what methods it implements, rather than its inheritance hierarchy. So, if an object has the methods that are expected by some code, it can be treated as an instance of a certain \"interface,\" even if it doesn't explicitly implement that interface.\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "84df901e-db14-4e9e-a80e-e7068745f364", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Bird:\n", | |
" def speak(self):\n", | |
" return \"Tweet\"\n", | |
" \n", | |
" def move(self):\n", | |
" return \"Flying\"\n", | |
"\n", | |
"class Car:\n", | |
" def speak(self):\n", | |
" return \"Beep\"\n", | |
" \n", | |
" def move(self):\n", | |
" return \"Driving\"\n", | |
"\n", | |
"def interact_with_animal(animal):\n", | |
" print(animal.speak())\n", | |
" print(animal.move())\n", | |
"\n", | |
"bird = Bird()\n", | |
"car = Car()\n", | |
"\n", | |
"interact_with_animal(bird) # Works fine with Bird\n", | |
"interact_with_animal(car) # Works fine with Car" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "47746d67-ac34-4eb8-838f-f300fd4f783c", | |
"metadata": {}, | |
"source": [ | |
"### Python Special Attributes (3.2.10.1)\n", | |
"\n", | |
"| **Attribute** | **Meaning** |\n", | |
"|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n", | |
"| `type.__name__` | The class’s name. See also: `__name__` attributes. |\n", | |
"| `type.__qualname__` | The class’s qualified name. See also: `__qualname__` attributes. |\n", | |
"| `type.__module__` | The name of the module in which the class was defined. |\n", | |
"| `type.__dict__` | A mapping proxy providing a read-only view of the class’s namespace. See also: `__dict__` attributes. |\n", | |
"| `type.__bases__` | A tuple containing the class’s bases. For example, for a class defined as `class X(A, B, C)`, `X.__bases__` will be `(A, B, C)`. |\n", | |
"| `type.__doc__` | The class’s documentation string, or `None` if undefined. Not inherited by subclasses. |\n", | |
"| `type.__annotations__` | A dictionary containing variable annotations collected during class body execution. See [Annotations Best Practices](https://docs.python.org/3/howto/annotations.html). - **Caution**: Accessing the `__annotations__` attribute of a class object directly may yield incorrect results in the presence of metaclasses. Additionally, the attribute may not exist for some classes. Use `inspect.get_annotations()` to retrieve class annotations safely. |\n", | |
"| `type.__type_params__` | A tuple containing the type parameters of a generic class. *(Added in version 3.12)* |\n", | |
"| `type.__static_attributes__`| A tuple containing names of attributes of this class which are assigned through `self.X` from any function in its body. *(Added in version 3.13)* |\n", | |
"| `type.__firstlineno__` | The line number of the first line of the class definition, including decorators. Setting the `__module__` attribute removes the `__firstlineno__` item from the type’s dictionary. *(Added in version 3.13)* |\n", | |
"| `type.__mro__` | The tuple of classes that are considered when looking for base classes during method resolution. |\n", | |
"\n", | |
"In addition to the special attributes described above, all Python classes also have the following two methods available:\n", | |
"\n", | |
"| **Attribute** | **Meaning** |\n", | |
"|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n", | |
"| `type.mro()` | This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in __mro__. \n", | |
"| `type.__subclasses__()` | Each class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. The list is in definition order.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "75f970b8-7924-4937-a982-9e71ce393e0e", | |
"metadata": {}, | |
"source": [ | |
"### Python Special Attributes and Methods\n", | |
"\n", | |
"| **Concept** | **Meaning** |\n", | |
"|---------------------|-------------------------------------------------------------------------------------------------------------|\n", | |
"| `__self__` | Refers to the instance of the class to which a method is bound. Used in bound method objects. |\n", | |
"| `__dict__` | A dictionary or mapping object containing the writable attributes of an object. |\n", | |
"| `__getattr__()` | A method called when an attribute is not found in the usual way. Used to define custom attribute access. |\n", | |
"| `__setattr__()` | A method called to set an attribute on an object. Useful for customizing attribute assignment behavior. |\n", | |
"| `__delattr__()` | A method called to delete an attribute from an object. Useful for customizing attribute deletion behavior. |\n", | |
"| `__class__` | The class to which an object belongs. This attribute can be used to check or inspect the type of an object. |\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "40f3dbce-3354-49e0-9935-e80e2c348989", | |
"metadata": {}, | |
"source": [ | |
"Slice objects are used to represent slices for __getitem__() methods. They are also created by the built-in slice() function." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "71e33972-0f88-435b-8f6f-80f020d6266b", | |
"metadata": {}, | |
"source": [ | |
"## Python Special Methods\n", | |
"\n", | |
"| **Method** | **Meaning** |\n", | |
"|----------------------|-----------------------------------------------------------------------------------------------------------------|\n", | |
"| `__new__` | Called to create a new instance of a class. Used before `__init__` and allows customization of instance creation. Is a static method |\n", | |
"| `__init__` | Called after an instance is created to initialize it. Typically used for setting up attributes. Not invoked if __new__ doesnt' return an instance. |\n", | |
"| `__del__` | Called when an object is about to be destroyed. Useful for cleanup operations. See also `weakref.finalize`. Delete exception are ignored and printed to stderr (see [doc](https://docs.python.org/3/reference/datamodel.html#object.__del__) for more info) |\n", | |
"| `__repr__` | Called by `repr()` to provide a developer-friendly (informal) string representation of an object. |\n", | |
"| `__str__` | Called by `str()` and `print()` to provide a user-friendly string representation of an object. |\n", | |
"| `__bytes__` | Called by `bytes()` to return a byte representation of an object. |\n", | |
"| `__format__` | Called by `format()` and string formatting operations (e.g., `\"{:format}\".format(obj)`). |\n", | |
"| `__lt__` | Defines behavior for the `<` (less than) operator. |\n", | |
"| `__le__` | Defines behavior for the `<=` (less than or equal to) operator. |\n", | |
"| `__eq__` | Defines behavior for the `==` (equality) operator. |\n", | |
"| `__ne__` | Defines behavior for the `!=` (not equal to) operator. |\n", | |
"| `__gt__` | Defines behavior for the `>` (greater than) operator. |\n", | |
"| `__ge__` | Defines behavior for the `>=` (greater than or equal to) operator. |\n", | |
"| `__hash__` | Defines behavior for the hash value of an object. Used in hashing operations like `dict` keys or `set`. |\n", | |
"| `__bool__` | Called by `bool()` to determine the truth value of an object. |\n", | |
"\n", | |
"\n", | |
"New and Init are used together to instanciate a class." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "43875c38-8289-40d2-b293-61e59c0cfc9f", | |
"metadata": {}, | |
"source": [ | |
"## ABC class" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "2c8be47d-9273-4c3e-b61d-9699623296a9", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from abc import ABC, abstractmethod\n", | |
"\n", | |
"# Define an abstract base class\n", | |
"class Shape(ABC):\n", | |
" @abstractmethod\n", | |
" def area(self):\n", | |
" \"\"\"Calculate the area of the shape\"\"\"\n", | |
" pass\n", | |
"\n", | |
" @abstractmethod\n", | |
" def perimeter(self):\n", | |
" \"\"\"Calculate the perimeter of the shape\"\"\"\n", | |
" pass\n", | |
"\n", | |
"class Rectangle(Shape):\n", | |
" def __init__(self, width, height):\n", | |
" self.width = width\n", | |
" self.height = height\n", | |
"\n", | |
" def area(self):\n", | |
" return self.width * self.height\n", | |
"\n", | |
" def perimeter(self):\n", | |
" return 2 * (self.width + self.height)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "84aa33ae-d3c4-434b-b565-e97cb3bbfbcb", | |
"metadata": {}, | |
"source": [ | |
"You can for example implement `collections.abc` to have a class acting like a collection.\n", | |
"\n", | |
"See <https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes>\n", | |
"\n", | |
"## Generics\n", | |
"Generic classes implicitly inherit from `typing.Generic`.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "6a5aa3bb-4427-40b7-ab52-734a44c7d4b8", | |
"metadata": { | |
"editable": true, | |
"slideshow": { | |
"slide_type": "" | |
}, | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.13.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment