pep links + links throughout the thing + with + async for
This commit is contained in:
parent
d8b9911193
commit
ec8607093e
125
main.ipynb
125
main.ipynb
@ -4,7 +4,6 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "9fdeb776-f17c-42a5-84fb-765c19d5c6c3",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
@ -15,7 +14,6 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "b3969792-c3ea-490b-8f5a-463da33ce0c4",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
@ -30,11 +28,18 @@
|
||||
"* https://docs.python.org/3/\n",
|
||||
" * https://docs.python.org/3/tutorial/ read through this\n",
|
||||
" * https://docs.python.org/3/library/ before you start implementing something or looking for pip packages\n",
|
||||
" * https://docs.python.org/3/library/asyncio.html asynchronous I/O\n",
|
||||
" * https://docs.python.org/3/library/ctypes.html to load C-like DLLs\n",
|
||||
" * https://docs.python.org/3/library/unittest.html one of testing options\n",
|
||||
" * https://docs.python.org/3/extending/ if you need C\n",
|
||||
" * https://docs.python.org/3/whatsnew/ there's a lot of important stuff posted there, actually (e.g. `match` keyword)\n",
|
||||
" * https://docs.python.org/3/reference/ in-depth about python itself\n",
|
||||
" * https://docs.python.org/3/reference/datamodel.html the most important part from that reference\n",
|
||||
"* https://peps.python.org/\n",
|
||||
" * https://peps.python.org/pep-0008/ python style guide\n",
|
||||
" * https://peps.python.org/pep-0020 the zen of python\n",
|
||||
" * https://peps.python.org/pep-0257 docstrings\n",
|
||||
" * ~~https://peps.python.org/pep-0333 please no~~\n",
|
||||
"* https://wiki.python.org/moin/FrontPage at the moment of writing, haven't read much of it"
|
||||
]
|
||||
},
|
||||
@ -49,14 +54,15 @@
|
||||
"# Introduction\n",
|
||||
"This notebook mostly features topics that are (IMO) overlooked.\n",
|
||||
"* environment; there are a lot of choices for where to develop and where to deploy; I'll try to gather and classify as much viable options as I can.\n",
|
||||
"* typing; not everyone knows about annotations' existence (let alone that of the module); typing greatly enhances programmer's performance in any PL, and python is not an exception to that.\n",
|
||||
"* data model; python has a very strict and simple data model behind it; once you understand it, things like \"why this attribute returns a totally different thing\" will be more obvious.\n",
|
||||
"* [typing](https://docs.python.org/3/library/typing.html); not everyone knows about annotations' existence (let alone that of the module); typing greatly enhances programmer's performance in any PL, and python is not an exception to that.\n",
|
||||
"* [data model](https://docs.python.org/3/reference/datamodel.html); python has a very strict and simple data model behind it; once you understand it, things like [\"why this attribute returns a totally different thing\"](https://docs.python.org/3/reference/datamodel.html#object.__getattr__) will be more obvious.\n",
|
||||
"* working with files; there are approaches beyond `open`+`.write`/`.read`.\n",
|
||||
"* asyncio; the only correct way to do IO, if you don't wish to waste time and memory.\n",
|
||||
"* obscure new features; `match`, `Self`, `type | type`, etc.; they are important, but not omnipresent as per their (in-language) novelty.\n",
|
||||
"* styling;\n",
|
||||
"* [asyncio](https://docs.python.org/3/library/asyncio.html); the only correct way to do IO, if you don't wish to waste time and memory.\n",
|
||||
"* obscure new features; [`match`](https://docs.python.org/3/tutorial/controlflow.html#match-statements), [`Self`](https://peps.python.org/pep-0673/), [`type | type`](https://peps.python.org/pep-0604/), etc.; they are important, but not omnipresent as per their (in-language) [novelty](https://docs.python.org/3/whatsnew/index.html).\n",
|
||||
"* [styling](https://peps.python.org/pep-0008/).\n",
|
||||
"* testing (no link, because testing is quite diverse); not only unit tests; I'm still relatively incompetent at it, so things will be described from quite a limited perspective.\n",
|
||||
"\n",
|
||||
"Most sections will eventually include \"prerequisites\" part (things you need to know before looking into this section); things required are mostly found in the official tutorial."
|
||||
"Most sections will eventually include \"***prerequisites:***\" part (things you need to know before looking into this section); things required are mostly found in the official tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -74,16 +80,18 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "07390193-fdde-4bee-aec4-f5b3227dea06",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"# typing\n",
|
||||
"# [typing](https://docs.python.org/3/library/typing.html)\n",
|
||||
"module: https://docs.python.org/3/library/typing.html \n",
|
||||
"relevant PEP: https://peps.python.org/pep-0484/\n",
|
||||
"\n",
|
||||
"Even a dynamically typed language can sometimes, at least, declare types. Python has built-in support for that via annotations.\n",
|
||||
"\n",
|
||||
"***prerequisites:** functions*\n",
|
||||
"***prerequisites:** [functions](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)*\n",
|
||||
"\n",
|
||||
"***also mentioned here:** `match`*"
|
||||
"***also mentioned here:** [`match`](https://docs.python.org/3/tutorial/controlflow.html#match-statements)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -100,6 +108,7 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "990298fd-f154-45c7-b9a7-af63ae6e7c4e",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
@ -110,11 +119,10 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "de9d52c9-6df3-4427-b316-587b56347cd4",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### For a variable"
|
||||
"### For a [variable](https://peps.python.org/pep-0526/)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -133,11 +141,10 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "ccde8fee-3003-4842-bec8-8f118e95b17e",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### For a function"
|
||||
"### For a [function](https://docs.python.org/3/library/typing.html#callable)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -160,11 +167,10 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "36c05f51-5422-41c7-a461-7f766c04b088",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Of a generic"
|
||||
"### Of a [generic](https://docs.python.org/3/library/typing.html#generics)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -182,11 +188,13 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "a52cf44c-aa01-40c9-9adc-e838da385b28",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### Of a union"
|
||||
"### Of a [union](https://docs.python.org/3/library/stdtypes.html#types-union)\n",
|
||||
"see also:\n",
|
||||
"https://peps.python.org/pep-0604/\n",
|
||||
"https://docs.python.org/3/library/typing.html#typing.Union"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -206,11 +214,10 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "82158316-a752-4bcf-bd6f-c4cde017096b",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### example with match\n",
|
||||
"### example with [`match`](https://docs.python.org/3/tutorial/controlflow.html#match-statements)\n",
|
||||
"though annotations don't usually influence anything during runtime, they make programming easier"
|
||||
]
|
||||
},
|
||||
@ -253,10 +260,10 @@
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"# Data model\n",
|
||||
"# [Data model](https://docs.python.org/3/reference/datamodel.html)\n",
|
||||
"https://docs.python.org/3/reference/datamodel.html\n",
|
||||
"\n",
|
||||
"***prerequisites:** classes*"
|
||||
"***prerequisites:** [classes](https://docs.python.org/3/tutorial/classes.html)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -268,11 +275,13 @@
|
||||
},
|
||||
"source": [
|
||||
"# Working with files\n",
|
||||
"There are a lot of ways, and directly using `open`+`.write`/`.read` is probably not fit for you task.\n",
|
||||
"related tutorial section: https://docs.python.org/3/tutorial/inputoutput.html\n",
|
||||
"\n",
|
||||
"***prerequisites:** variables, strings*\n",
|
||||
"There are a lot of ways, and directly using `open`+`.write`/`.read` is probably not fit for you task. So I decided not to link one specific external article to this section.\n",
|
||||
"\n",
|
||||
"***also mentioned here:** `with`*"
|
||||
"***prerequisites:** variables, [strings](https://docs.python.org/3/tutorial/introduction.html#strings)*\n",
|
||||
"\n",
|
||||
"***also mentioned here:** [`with`](https://peps.python.org/pep-0343/)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -283,14 +292,14 @@
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## `pathlib.Path`\n",
|
||||
"One of the most overlooked python's modules is `pathlib`.\n",
|
||||
"Even more rare is the knowledge that `Path` objects have `.open`, `.write_*` and `.read_*` methods."
|
||||
"## [`pathlib`](https://docs.python.org/3/library/pathlib.html), [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)\n",
|
||||
"One of the most overlooked python's modules is [`pathlib`](https://docs.python.org/3/library/pathlib.html).\n",
|
||||
"Even more rare is the knowledge that [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) objects have [`.open`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.open), [`.write_*`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.write_text) and [`.read_*`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.read_text) methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 12,
|
||||
"id": "b25fe82a-f87e-425d-81b4-bf1c0c2baf0f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -322,7 +331,8 @@
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## File(-like) objects\n",
|
||||
"## File(-like) objects -- [streams](https://docs.python.org/3/library/io.html).\n",
|
||||
"https://docs.python.org/3/library/io.html\n",
|
||||
"Returned by `open(...)` and `pathlib.Path(...).open(...)`."
|
||||
]
|
||||
},
|
||||
@ -357,6 +367,22 @@
|
||||
" print(line)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "be63bbdc-4a21-4ed5-b0cb-0dd5f8a447c0",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"# The [`with`](https://peps.python.org/pep-0343/) statement.\n",
|
||||
"~~AKA the reason I can't quit python~~\n",
|
||||
"\n",
|
||||
"statement itself: https://docs.python.org/3/reference/compound_stmts.html#with \n",
|
||||
"relevant PEP: https://peps.python.org/pep-0343/ \n",
|
||||
"the datamodel it represents: https://docs.python.org/3/reference/datamodel.html#context-managers"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6519c846-90fd-4828-9861-3cac9954880f",
|
||||
@ -365,13 +391,15 @@
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"# asyncio\n",
|
||||
"# [asyncio](https://docs.python.org/3/library/asyncio.html)\n",
|
||||
"If you do anything with IO, you should probably do it **asynchronously** \n",
|
||||
"https://docs.python.org/3/library/asyncio.html\n",
|
||||
"\n",
|
||||
"***prerequisites:** functions*\n",
|
||||
"***prerequisites:** [functions](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)*\n",
|
||||
"\n",
|
||||
"***also mentioned here:** `with`*"
|
||||
"***recommended before reading:** [classes](https://docs.python.org/3/tutorial/classes.html)*\n",
|
||||
"\n",
|
||||
"***also mentioned here:** [`with`](https://peps.python.org/pep-0343/)*"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -397,7 +425,7 @@
|
||||
"Due to \\{1) limitations in performance 2) single-threaded environment (that can't allow any freezes) 3) constantly working with high-latency IO (because internet, client- and server-side) of high frequency (and high (number-wise) loads for servers)\\}, JS kind of has to resort to using an event loop. \n",
|
||||
"All IO in JS is forced to be async. \n",
|
||||
"In browsers that is to prevent incompetent or malicious actors from killing your browser (or parts of it). \n",
|
||||
"In Node that is to prevent obviously unnecessary blocking of code to achieve better performance. \n",
|
||||
"In Node that is to prevent unnecessary blocking of code to achieve better performance. \n",
|
||||
"Ryan Dahl on Node.js: \n",
|
||||
"https://youtu.be/ztspvPYybIY \n",
|
||||
"https://youtu.be/jo_B4LTHi3I"
|
||||
@ -412,20 +440,19 @@
|
||||
},
|
||||
"source": [
|
||||
"## Level 0: working inside async framework\n",
|
||||
"If you've ever worked with `aiohttp` or `discord.py`/`disnake`, you've probably encountered `async`/`await` syntax. \n",
|
||||
"If you've ever worked with [`aiohttp`](https://docs.aiohttp.org/) or [`discord.py`](https://discordpy.readthedocs.io/)/[`disnake`](https://docs.disnake.dev/), you've probably encountered [`async`/`await` syntax](https://peps.python.org/pep-0492/). \n",
|
||||
"It may seems like these frameworks just make you put this magic words here and there. \n",
|
||||
"But there are reasons for both these magic words."
|
||||
"But there are reasons for both of these magic words."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d46018f9-8609-4408-ab97-48dbea0bfd7c",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### `async def` -- define an asynchronous function (coroutine)\n",
|
||||
"### `async def` -- define an asynchronous function ([coroutine](https://docs.python.org/3/library/asyncio-task.html))\n",
|
||||
"`async` here signals to python that the function might use `await`s inside it. \n",
|
||||
"Statements *inside* coroutines are guaranteed to happen in sequence with each other"
|
||||
]
|
||||
@ -440,7 +467,7 @@
|
||||
"source": [
|
||||
"### `await` -- virtually block on a future or on another coroutine and return result\n",
|
||||
"While coroutine `await`s, other coroutines can be (and, probably, are) doing their own stuff. \n",
|
||||
"Any code between `await`s happens sequentially with all the other code, i.e. unlike `threading`, `async` does not mix statements unpredictably. \n",
|
||||
"Any code between `await`s happens sequentially with all the other code, i.e. unlike [`threading`](https://docs.python.org/3/library/threading.html), `async` does not mix statements unpredictably. \n",
|
||||
"You always have the exclusive and direct control of data in code fragments where you don't `await`. \n",
|
||||
"Expression `(await f(...))` where `f` is defined as `async def f(...): ...` with `return x` evaluates to that `x`. \n",
|
||||
"Expression `(await future)` where `f` is `asyncio.Future` that has someone call `.set_result(x)` evaluates to that `x`. "
|
||||
@ -450,14 +477,22 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "f9cf426c-94fa-4907-b182-a759d0934080",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"### `async with` -- asynchronous context management\n",
|
||||
"### [`async with` -- asynchronous context management](https://peps.python.org/pep-0492/#asynchronous-context-managers-and-async-with)\n",
|
||||
"Same as `with`, but `async def __aenter__`/`async def __aexit__` instead of `def __enter__`/`def __exit__`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3883d33f-730f-4ccb-b4be-3f905bafc607",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### [`async for` -- asynchronous iteration](https://peps.python.org/pep-0492/#asynchronous-iterators-and-async-for)\n",
|
||||
"Same as `for`, but `async def` instead of `def` for `yield`-based generators and `__aiter__`/`__anext__` instead of `__iter__`/`__next__` for iterators/iterables."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "21f08324-0b14-4b35-bc6e-7a7329520ed9",
|
||||
@ -466,7 +501,7 @@
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## Level 1: using high-level APIs"
|
||||
"## Level 1: using [high-level APIs](https://docs.python.org/3/library/asyncio-api-index.html)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -477,7 +512,7 @@
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"## Level 2: using low-level APIs"
|
||||
"## Level 2: using [low-level APIs](https://docs.python.org/3/library/asyncio-llapi-index.html)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user