From 1b15875e72cea313e41215bdea49c2a7b714b1bc Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 11 Nov 2022 13:37:52 +0000 Subject: [PATCH] " " --- main.ipynb | 195 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 171 insertions(+), 24 deletions(-) diff --git a/main.ipynb b/main.ipynb index 71be2d1..a31ddc4 100644 --- a/main.ipynb +++ b/main.ipynb @@ -26,21 +26,21 @@ "metadata": {}, "source": [ "* https://docs.python.org/3/\n", - " * https://docs.python.org/3/tutorial/ \\\n", + " * https://docs.python.org/3/tutorial/ \n", " read through this\n", - " * https://docs.python.org/3/library/ \\\n", + " * https://docs.python.org/3/library/ \n", " before you start implementing something or looking for pip packages\n", - " * https://docs.python.org/3/library/ctypes.html \\\n", + " * https://docs.python.org/3/library/ctypes.html \n", " to load C-like DLLs\n", - " * https://docs.python.org/3/extending/ \\\n", + " * https://docs.python.org/3/extending/ \n", " if you need C\n", - " * https://docs.python.org/3/whatsnew/ \\\n", + " * https://docs.python.org/3/whatsnew/ \n", " there's a lot of important stuff posted there, actually (e.g. `match` keyword)\n", - " * https://docs.python.org/3/reference/ \\\n", + " * https://docs.python.org/3/reference/ \n", " in-depth about python itself\n", - " * https://docs.python.org/3/reference/datamodel.html \\\n", + " * https://docs.python.org/3/reference/datamodel.html \n", " the most important part from that reference\n", - "* https://wiki.python.org/moin/FrontPage \\\n", + "* https://wiki.python.org/moin/FrontPage \n", " at the moment of writing, haven't read much of it" ] }, @@ -55,6 +55,153 @@ "Even a dynamically typed language can sometimes, at least, declare types. Python has built-in support for that via annotations." ] }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5477e321-dc9a-4c9a-a4b6-a2549c0c0d37", + "metadata": {}, + "outputs": [], + "source": [ + "import typing" + ] + }, + { + "cell_type": "markdown", + "id": "990298fd-f154-45c7-b9a7-af63ae6e7c4e", + "metadata": { + "jp-MarkdownHeadingCollapsed": true, + "tags": [] + }, + "source": [ + "## Declaring a type" + ] + }, + { + "cell_type": "markdown", + "id": "de9d52c9-6df3-4427-b316-587b56347cd4", + "metadata": { + "tags": [] + }, + "source": [ + "### For a variable" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "0e87b508-7845-4678-8134-884643f52213", + "metadata": {}, + "outputs": [], + "source": [ + "num0: int\n", + "num0 = 1\n", + "num1: int = 1" + ] + }, + { + "cell_type": "markdown", + "id": "ccde8fee-3003-4842-bec8-8f118e95b17e", + "metadata": {}, + "source": [ + "### For a function" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0a72a4e0-0e01-498f-b770-e48116d5a97b", + "metadata": {}, + "outputs": [], + "source": [ + "def fn0(x: int) -> int:\n", + " return x\n", + "\n", + "\n", + "fn1: typing.Callable[[int], int]\n", + "def fn1(x):\n", + " return x" + ] + }, + { + "cell_type": "markdown", + "id": "36c05f51-5422-41c7-a461-7f766c04b088", + "metadata": {}, + "source": [ + "### Of a generic" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "267accf3-6ab5-421a-a349-04d21669e586", + "metadata": {}, + "outputs": [], + "source": [ + "list0: list[int] = [0, 1]\n", + "dict0: dict[str, int] = {'0': 0, '1': 1}" + ] + }, + { + "cell_type": "markdown", + "id": "a52cf44c-aa01-40c9-9adc-e838da385b28", + "metadata": {}, + "source": [ + "### Of a union" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "eef4b91f-9a86-4d40-a049-d54f6a6f230f", + "metadata": {}, + "outputs": [], + "source": [ + "optional0: int | None = 0\n", + "optional1: typing.Union[int, None] = None\n", + "optional2: typing.Optional[int] = None\n", + "optional2 = -1" + ] + }, + { + "cell_type": "markdown", + "id": "82158316-a752-4bcf-bd6f-c4cde017096b", + "metadata": {}, + "source": [ + "### example with match\n", + "though annotations don't usually influence anything during runtime, they make programming easier" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "de6fd23d-1a81-47cc-ba84-b2f97ebe2cb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('12', '12', None)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def join(a: list[int] | set[str] | None) -> str | None:\n", + " match a:\n", + " case list():\n", + " return ''.join(map(str, a))\n", + " case set():\n", + " return ''.join(a)\n", + " case _:\n", + " return None\n", + "\n", + "result: tuple[str | None, str | None, str | None] = join([1, 2]), join({\"1\", \"2\"}), join(None)\n", + "result" + ] + }, { "cell_type": "markdown", "id": "c726cf29-f81e-4e52-95f1-128057fe4d0a", @@ -165,7 +312,7 @@ }, "source": [ "# asyncio\n", - "If you do anything with IO, you should probably do it **asynchronously** \\\n", + "If you do anything with IO, you should probably do it **asynchronously** \n", "https://docs.python.org/3/library/asyncio.html" ] }, @@ -187,13 +334,13 @@ }, "source": [ "## Background\n", - "The most prominent use of async IO is undoubtably anything that has to do with JavaScript (the web programming language). \\\n", - "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", - "Ryan Dahl on Node.js: \\\n", - "https://youtu.be/ztspvPYybIY \\\n", + "The most prominent use of async IO is undoubtably anything that has to do with JavaScript (the web programming language). \n", + "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", + "Ryan Dahl on Node.js: \n", + "https://youtu.be/ztspvPYybIY \n", "https://youtu.be/jo_B4LTHi3I" ] }, @@ -205,8 +352,8 @@ }, "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", - "It may seems like these frameworks just make you put this magic words here and there. \\\n", + "If you've ever worked with `aiohttp` or `discord.py`/`disnake`, you've probably encountered `async`/`await` syntax. \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." ] }, @@ -218,7 +365,7 @@ }, "source": [ "### `async def` -- define an asynchronous function (coroutine)\n", - "`async` here signals to python that the function might use `await`s inside it. \\\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" ] }, @@ -230,11 +377,11 @@ }, "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", - "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`. \\" + "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", + "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`. " ] }, {