diff --git a/lesson-02/class_02.ipynb b/lesson-02/class_02.ipynb new file mode 100644 index 0000000..636638f --- /dev/null +++ b/lesson-02/class_02.ipynb @@ -0,0 +1,2107 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f0cafd2e-c64a-424a-892c-f9f5faca893a", + "metadata": {}, + "source": [ + "# try/except example" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "85bab651-3cea-4daa-b452-8d9fbfb73c01", + "metadata": {}, + "outputs": [], + "source": [ + "class ApiError(Exception):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "466de340-9f43-4d87-9b98-b6bd3dcc5b46", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "try - enter\n", + "Exception err=ApiError()\n", + "finally\n" + ] + }, + { + "ename": "ApiError", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mApiError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[83], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtry - enter\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ApiError()\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mException \u001b[39m\u001b[38;5;132;01m{\u001b[39;00merr\u001b[38;5;132;01m=}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mApiError\u001b[0m: " + ] + } + ], + "source": [ + "try:\n", + " print(\"try - enter\")\n", + " raise ApiError()\n", + "except Exception as err:\n", + " print(f\"Exception {err=}\")\n", + " raise\n", + "except ApiError as err:\n", + " print(f\"ApiError {err=}\")\n", + " raise\n", + "else:\n", + " print(\"else\")\n", + "finally:\n", + " print(\"finally\")" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "4bb67d82-f38a-4286-bebb-dd9be2484a8b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "try - enter\n", + "Exception err=ApiError()\n", + "finally\n" + ] + } + ], + "source": [ + "try:\n", + " print(\"try - enter\")\n", + " raise ApiError()\n", + "except Exception as err:\n", + " print(f\"Exception {err=}\")\n", + "except ApiError as err:\n", + " print(f\"ApiError {err=}\")\n", + "else:\n", + " print(\"else\")\n", + "finally:\n", + " print(\"finally\")" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "430067cd-3c69-4c43-ba36-48fc67a2f1ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "try - enter\n", + "ApiError err=ApiError()\n", + "finally\n" + ] + } + ], + "source": [ + "try:\n", + " print(\"try - enter\")\n", + " raise ApiError()\n", + "except ApiError as err:\n", + " print(f\"ApiError {err=}\")\n", + "except Exception as err:\n", + " print(f\"Exception {err=}\")\n", + "else:\n", + " print(\"else\")\n", + "finally:\n", + " print(\"finally\")" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "e63ef0fc-2dca-4288-99fc-873b2192d424", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "try - enter\n", + "Exception err=ValueError()\n", + "finally\n" + ] + } + ], + "source": [ + "try:\n", + " print(\"try - enter\")\n", + " raise ValueError()\n", + "except ApiError as err:\n", + " print(f\"ApiError {err=}\")\n", + "except Exception as err:\n", + " print(f\"Exception {err=}\")\n", + "else:\n", + " print(\"else\")\n", + "finally:\n", + " print(\"finally\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c263f985-cfee-4ed6-8f3e-fccb49b6f41d", + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " print(\"try - enter\")\n", + " raise ValueError()\n", + "except ApiError as err:\n", + " print(f\"ApiError {err=}\")\n", + "except Exception as err:\n", + " print(f\"Exception {err=}\")\n", + "else:\n", + " print(\"else\")\n", + "finally:\n", + " print(\"finally\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c07eefc2-ada1-4e47-b0f4-f5f0d668a94a", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "file = open()\n", + "for line in file:\n", + " raise Exception()\n", + " print()\n", + "\n", + "file.close()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7964e24e-3a6e-42b3-9bd2-d832717404e8", + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " file = open()\n", + " for line in file:\n", + " raise Exception()\n", + " print()\n", + "finally:\n", + " file.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "28ee1930-c49e-4a01-a8b0-a7d7d17ec441", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "try - enter\n", + "else\n", + "finally\n" + ] + } + ], + "source": [ + "try:\n", + " print(\"try - enter\")\n", + "except ApiError as err:\n", + " print(f\"ApiError {err=}\")\n", + "except Exception as err:\n", + " print(f\"Exception {err=}\")\n", + "else:\n", + " print(\"else\")\n", + "finally:\n", + " print(\"finally\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "32284423-7334-4454-9e4f-6fc1981d71bc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b89c773-0c4e-4db8-8c4e-9963ff3067f8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "24208b3d-de56-4092-a0c5-d896e9a5c6ac", + "metadata": {}, + "source": [ + "# Рекурсия" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "81c8e7aa-29fa-4f55-b31f-a7b0ec39ce22", + "metadata": {}, + "outputs": [], + "source": [ + "def calc_factorial(n):\n", + " if n == 0:\n", + " return 1\n", + " return n * calc_factorial(n - 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "abdad667-808a-47f2-8a4e-f37b5a185549", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3628800, 120)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calc_factorial(10), calc_factorial(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "27565db2-4ece-4249-8138-690b0d0fb8d5", + "metadata": {}, + "outputs": [], + "source": [ + "def counter(n):\n", + " if n == 0:\n", + " return 0\n", + " return n + counter(n - 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "4d401527-8614-4263-9290-f3c3cba0a732", + "metadata": {}, + "outputs": [ + { + "ename": "RecursionError", + "evalue": "maximum recursion depth exceeded", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mRecursionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mcounter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2990\u001b[39;49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[4], line 4\u001b[0m, in \u001b[0;36mcounter\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m n \u001b[38;5;241m+\u001b[39m \u001b[43mcounter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[4], line 4\u001b[0m, in \u001b[0;36mcounter\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m n \u001b[38;5;241m+\u001b[39m \u001b[43mcounter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping similar frames: counter at line 4 (2974 times)]\u001b[0m\n", + "Cell \u001b[0;32mIn[4], line 4\u001b[0m, in \u001b[0;36mcounter\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m n \u001b[38;5;241m+\u001b[39m \u001b[43mcounter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mRecursionError\u001b[0m: maximum recursion depth exceeded" + ] + } + ], + "source": [ + "counter(2990)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "642cd1d8-0304-4ca6-8467-ff1aecf4fc66", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3000" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import sys\n", + "sys.getrecursionlimit()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "76ddf4fd-61f7-41ee-9b1c-ec01f3f73cfc", + "metadata": {}, + "outputs": [], + "source": [ + "sys.setrecursionlimit(5000)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "539163be-a51d-4d89-b5af-3e72efa9bce2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4501500" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "counter(3000)" + ] + }, + { + "cell_type": "markdown", + "id": "b19b5cf0-df12-43c8-ac72-79cbdc52a2b1", + "metadata": {}, + "source": [ + "# Параметры и аргументы функций" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "e109c7b4-026b-468a-9549-e243d38bc2ad", + "metadata": {}, + "outputs": [], + "source": [ + "def fn1(x, y=100):\n", + " return x + y" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "5d7ac51b-07c0-4a4b-bae7-eaa05a00ba21", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(110, 30, 110, 30, 30)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fn1(10), fn1(10, 20), fn1(x=10), fn1(x=10, y=20), fn1(y=20, x=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "79f0208b-aca5-40dd-b45c-231472c0a73a", + "metadata": {}, + "outputs": [], + "source": [ + "def fn2(*args, **kwargs):\n", + " print(args, kwargs)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "c158a07a-ea7e-4b45-8ca7-1a60ebc9ddef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "() {}\n", + "(1, 2, 3) {'k': 10, 'args': 20}\n" + ] + }, + { + "data": { + "text/plain": [ + "(None, None)" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fn2(), fn2(1, 2, 3, k=10, args=20)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "f53f990c-3a88-4936-abf5-3197ef5ac2a2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mSignature:\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m/\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mDocstring:\u001b[0m Return the number of items in a container.\n", + "\u001b[0;31mType:\u001b[0m builtin_function_or_method" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "len?" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "71adadb0-8140-4024-aee6-e29542e4bc18", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len([])" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "1547c838-f5b2-4423-8c03-b2fdcf133519", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "len() takes no keyword arguments", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[27], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: len() takes no keyword arguments" + ] + } + ], + "source": [ + "len(obj=[])" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "9dc77bd6-2195-4511-9512-e2b95c7bad18", + "metadata": {}, + "outputs": [], + "source": [ + "def fn7(pos1, /, pos2, pos3=3, *, kw1=11, **kwargs):\n", + " print(f\"{pos1=}, {pos2=}, {pos3=}, {kw1=}, {kwargs=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "36fad21d-8614-4203-af48-17def2786430", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=3, kw1=11, kwargs={}\n" + ] + } + ], + "source": [ + "fn7(10, pos2=20)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "91bd03c1-1dcb-471f-a516-82a358585d21", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=30, kw1=11, kwargs={}\n" + ] + } + ], + "source": [ + "fn7(10, 20, 30)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "4fa3e30b-dcbf-462f-8c1c-3a04e7ff50d1", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "fn7() takes from 2 to 3 positional arguments but 4 were given", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mfn7\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m30\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m40\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: fn7() takes from 2 to 3 positional arguments but 4 were given" + ] + } + ], + "source": [ + "fn7(10, 20, 30, 40)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "fb0d6d92-eea3-4190-9602-f1d19b99812d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=30, kw1=40, kwargs={}\n" + ] + } + ], + "source": [ + "fn7(10, 20, 30, kw1=40)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "a444e466-7b9b-4cf7-aba6-8ea39b71c004", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=30, kw1=40, kwargs={'key': 50, 'val': 90}\n" + ] + } + ], + "source": [ + "fn7(10, 20, 30, kw1=40, key=50, val=90)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "fcbd7719-4ff6-40cb-bf76-cf1584d85051", + "metadata": {}, + "outputs": [], + "source": [ + "def fn8(pos1, /, pos2, pos3=3, *args, kw1=11, **kwargs):\n", + " print(f\"{pos1=}, {pos2=}, {pos3=}, {args=}, {kw1=}, {kwargs=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "316bf871-cfc3-4991-ae33-658b38056e2c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=30, args=(), kw1=40, kwargs={'key': 50, 'val': 90}\n" + ] + } + ], + "source": [ + "fn8(10, 20, 30, kw1=40, key=50, val=90)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "3039326a-0dfb-40f7-be4e-2b657213c2b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=30, args=(40,), kw1=11, kwargs={'key': 50, 'val': 90}\n" + ] + } + ], + "source": [ + "fn8(10, 20, 30, 40, key=50, val=90)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "423114c1-2e67-4b7f-bcbf-933e77b74f40", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pos1=10, pos2=20, pos3=30, args=(40, 41, 42, 43), kw1=44, kwargs={'key': 50, 'val': 90}\n" + ] + } + ], + "source": [ + "fn8(10, 20, 30, 40, 41, 42, 43, kw1=44, key=50, val=90)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93d4e99a-ca53-41e7-846b-99a855d3cec2", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "c4ea2696-b69d-4a60-986e-f5dd2295a23f", + "metadata": {}, + "outputs": [], + "source": [ + "def double_param(x, **kwargs):\n", + " print(f\"{x=}, {kwargs=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "d91c919d-bc45-4e92-8d03-bd13888fc5b0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x=10, kwargs={'y': 20}\n" + ] + } + ], + "source": [ + "double_param(10, y=20)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "2ac7609e-cac9-4339-a07d-7410ea3bbf6f", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "double_param() got multiple values for argument 'x'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[42], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdouble_param\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: double_param() got multiple values for argument 'x'" + ] + } + ], + "source": [ + "double_param(10, x=20)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "dcb5925f-421f-4e54-9972-fc9d2c5be0f6", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "double_param() got multiple values for argument 'x'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[43], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdouble_param\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mx\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: double_param() got multiple values for argument 'x'" + ] + } + ], + "source": [ + "double_param(10, **{\"x\": 20})" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "5667f1f3-8b62-4ac6-a584-2b3a54cc4f92", + "metadata": {}, + "outputs": [], + "source": [ + "def double_param_fixed(x, /, **kwargs):\n", + " print(f\"{x=}, {kwargs=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "a926d878-d068-44f2-97ff-e10aabd051e7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x=10, kwargs={'x': 20}\n" + ] + } + ], + "source": [ + "double_param_fixed(10, x=20)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "979a7589-cbd7-438c-bf17-23bf686a031a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "1443b29b-5ec9-4c12-bd85-92397debd3b3", + "metadata": {}, + "source": [ + "# параметры по умоолчанию" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "7fe2f46f-dbc9-480d-bd01-adfabbae4985", + "metadata": {}, + "outputs": [], + "source": [ + "def append_to_list(new_val, lst=[1, 2]):\n", + " lst.append(new_val)\n", + " return lst" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "917d2f5e-4b01-4be0-b38e-7335b2400339", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10]" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst = []\n", + "\n", + "append_to_list(10, lst)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "847af802-dbef-422c-853a-7adbeccb3722", + "metadata": {}, + "outputs": [], + "source": [ + "arr1 = append_to_list(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "5dccd221-2706-45bf-a08b-924805c1ac8a", + "metadata": {}, + "outputs": [], + "source": [ + "arr2 = append_to_list(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "1a3d8a2d-bd17-4361-b602-fc2d4659e8ee", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(True, True, [1, 2, 10, 20, 10, 20], [1, 2, 10, 20, 10, 20])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr1 == arr2, arr1 is arr2, arr1, arr2" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "d6bf22a3-2f47-474a-bc66-c84c8e4e2a5d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 10, 20, 10, 20, 10, 10, 50, 50, 99]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "append_to_list(99)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "e49f5526-da38-4c9a-a47b-fd1e6bc4b764", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 10, 20, 10, 20, 10, 10, 50, 50, 99, 99]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "append_to_list(99)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9b50a81a-6335-4b98-9603-da0059a26638", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0cc1928b-9aa7-4e97-a7f4-5c7efd172c0c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "b943fc01-763a-42d6-9a38-78136232f736", + "metadata": {}, + "outputs": [], + "source": [ + "def append_to_list_correct(new_val, lst=None):\n", + " if lst is None:\n", + " lst = [1, 2]\n", + "\n", + " lst.append(new_val)\n", + " \n", + " return lst" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "582d86e0-ecc3-45c4-8b62-9d3153483717", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 99]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "append_to_list_correct(99)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "2a374ddd-42e1-4d29-8807-079dd39f093e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 99]" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "append_to_list_correct(99)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "fe33685b-4b8b-4063-a6d5-92206fb295cf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "append_to_list_correct(100) is append_to_list_correct(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "cf0d72bc-fb12-4a34-b92e-e04c0f159777", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 20, 30, 40]" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "append_to_list_correct(40, [10, 20, 30])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c0c1ab7f-5065-464d-a43e-bf6eb36f6b6b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "208c6f03-6791-4f9a-a93f-eeefcfa13cf8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "831a0e00-967b-4ee8-9623-aba42153369a", + "metadata": {}, + "source": [ + "# передача аргументов" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "fa5be512-543b-4d38-9188-a0d6d406315e", + "metadata": {}, + "outputs": [], + "source": [ + "def fn(param):\n", + " param = 20\n", + " return param" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "c5b656a2-c2d5-4a27-ae69-ae44f4d58678", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "arg = 10\n", + "\n", + "fn(arg) # param = arg\n", + "\n", + "print(f\"{arg}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "b2ae9f4d-85a6-4d2a-8b0b-337da623261b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[99, 10]\n" + ] + } + ], + "source": [ + "def fn_mutable(param: list[int]):\n", + " param.append(10)\n", + " return param\n", + "\n", + "arg = [99]\n", + "\n", + "fn_mutable(arg) # param = arg\n", + "\n", + "print(f\"{arg}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d0f029f-43ff-46ee-8de8-40845f0d07ec", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "be7773e3-5219-4ab6-838f-1fe87c374e88", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[99]\n" + ] + } + ], + "source": [ + "def fn_mutable(param: list[int]):\n", + " param = []\n", + " return param\n", + "\n", + "arg = [99]\n", + "\n", + "fn_mutable(arg) # param = arg\n", + "\n", + "print(f\"{arg}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "a50931da-899b-440f-aedc-9cc7f7ebcde8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "def fn_mutable(param: list[int]):\n", + " global arg\n", + " arg = []\n", + " return param\n", + "\n", + "arg = [99]\n", + "\n", + "fn_mutable(arg) # param = arg\n", + "\n", + "print(f\"{arg}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec7598b8-1c0f-4ede-a278-4634551c0cef", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a033f177-a302-41b4-b657-75c18139181a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "12621107-fee6-42d2-af2b-78bbb25e20d7", + "metadata": {}, + "outputs": [], + "source": [ + "lst = list(range(-10, 10))" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "bf92348c-c65a-40ee-ab9d-8304710f7c90", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted(lst)" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "c60d4a08-8038-48e7-9895-9614bbfd96f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8, 8, -9, 9, -10]" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted(lst, key=lambda x: x ** 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "2ecfb571-f0f8-4c7e-ae48-7e192971b09e", + "metadata": {}, + "outputs": [], + "source": [ + "lst1 = list(range(-10, 10))\n", + "lst2 = list(range(-10, 10))" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "2e469eeb-39e5-4d7c-9775-8e632e84a269", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8, 8, -9, 9, -10],\n", + " [0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8, 8, -9, 9, -10])" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sort_key = lambda x: x ** 2 # NONONO!\n", + "\n", + "sorted(lst1, key=sort_key), sorted(lst2, key=sort_key)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "8204807d-9520-4790-8201-964e687cd56e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8, 8, -9, 9, -10],\n", + " [0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8, 8, -9, 9, -10])" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sort_key(x):\n", + " return x ** 2\n", + "\n", + "sorted(lst1, key=sort_key), sorted(lst2, key=sort_key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18b71ab6-2871-4366-9636-6ebdd562a888", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "da2e2f8b-ed2f-4c00-8b96-769ce7120846", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"abc\" in \"abc\"" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "4959a1bd-5b8c-4da7-aae3-e2f9b1f46fd6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True False\n" + ] + } + ], + "source": [ + "def search_substring(haystack: str, needle: str) -> bool:\n", + " return needle.lower() in haystack.lower()\n", + "\n", + "\n", + "print(search_substring(\"aBc\", \"Ab\"), search_substring(\"abc\", \"Ac\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "8c0cefd3-652c-46b8-89a1-5d00b048e39a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "test_search_bad_param (__main__.TestSearchSubstring.test_search_bad_param) ... ok\n", + "test_search_invalid (__main__.TestSearchSubstring.test_search_invalid) ... ok\n", + "test_search_valid (__main__.TestSearchSubstring.test_search_valid) ... ok\n", + "\n", + "----------------------------------------------------------------------\n", + "Ran 3 tests in 0.007s\n", + "\n", + "OK\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import unittest\n", + "\n", + "\n", + "class TestSearchSubstring(unittest.TestCase):\n", + " def test_search_valid(self):\n", + " self.assertTrue(search_substring(\"aBc\", \"Ab\"))\n", + " self.assertTrue(search_substring(\"aBc\", \"\"))\n", + " self.assertTrue(search_substring(\"\", \"\"))\n", + " \n", + " self.assertTrue(search_substring(\"a\", \"A\"))\n", + " self.assertTrue(search_substring(\"a\", \"a\"))\n", + " self.assertTrue(search_substring(\"A\", \"a\"))\n", + " \n", + " self.assertTrue(search_substring(\"abc\", \"a\"))\n", + " self.assertTrue(search_substring(\"cba\", \"a\"))\n", + "\n", + " self.assertTrue(search_substring(\"aabbaa\", \"aa\"))\n", + " self.assertTrue(search_substring(\"CCAABBaa\", \"AA\"))\n", + " self.assertTrue(search_substring(\"CCAABBaa\", \"CCAABBaa\"))\n", + " self.assertTrue(search_substring(\"CCAABBaa\", \"ccAAbbaa\"))\n", + "\n", + " def test_search_invalid(self):\n", + " self.assertFalse(search_substring(\"abc\", \"Ac\"))\n", + " self.assertFalse(search_substring(\"\", \"a\"))\n", + "\n", + " self.assertFalse(search_substring(\"n\", \"A\"))\n", + " self.assertFalse(search_substring(\"N\", \"a\"))\n", + " self.assertFalse(search_substring(\"NBdop\", \"a\"))\n", + " \n", + " self.assertFalse(search_substring(\"ab\", \"abc\"))\n", + "\n", + " self.assertFalse(search_substring(\"CCAABBan\", \"ccAAbbaa\"))\n", + " self.assertFalse(search_substring(\"tCAABBaa\", \"ccAAbbaa\"))\n", + " \n", + " def test_search_bad_param(self):\n", + " with self.assertRaises(AttributeError):\n", + " search_substring(\"abc\", None)\n", + "\n", + "\n", + "unittest.main(argv=[''], verbosity=2, exit=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84ca8269-b230-4918-bf91-b2977f8d132e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "af4a35ae-08d6-4f41-9055-40fc552fa2d4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cee7b065-0f65-4d61-86e5-077a17a7ee66", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "9d1729a8-c4e0-4837-a782-a7c9a6f78191", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "5\n", + "5\n", + "5\n", + "5\n" + ] + } + ], + "source": [ + "functions = [lambda x: a for a in range(1, 6)]\n", + "\n", + "obj = 0\n", + "\n", + "for fn in functions:\n", + " obj = fn(obj)\n", + " print(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "1b0d50b8-8fec-4b7c-9a66-e6451292a1f2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "5\n", + "5\n", + "5\n", + "5\n" + ] + } + ], + "source": [ + "a = 10\n", + "\n", + "for fn in functions:\n", + " obj = fn(obj)\n", + " print(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "43b87153-5fec-49ab-ae2b-8261943f74b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "5\n", + "5\n", + "5\n", + "5\n" + ] + } + ], + "source": [ + "functions = []\n", + "\n", + "for a in range(1, 6):\n", + " functions.append(lambda x: a)\n", + "\n", + "obj = 0\n", + "\n", + "for fn in functions:\n", + " obj = fn(obj)\n", + " print(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "3d128966-753e-449d-9214-b7a26fa6707e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "10\n", + "10\n", + "10\n", + "10\n" + ] + } + ], + "source": [ + "a = 10\n", + "\n", + "for fn in functions:\n", + " obj = fn(obj)\n", + " print(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "540d8b00-cd0a-4486-a2a7-0b7a1926dc97", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "str\n", + "str\n", + "str\n", + "str\n", + "str\n" + ] + } + ], + "source": [ + "a = \"str\"\n", + "\n", + "for fn in functions:\n", + " obj = fn(obj)\n", + " print(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "2d1c7697-1504-495a-8d4d-3b47a04b7035", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "functions = [lambda x=a: x for a in range(1, 6)]\n", + "\n", + "obj = 0\n", + "\n", + "for fn in functions:\n", + " obj = fn()\n", + " print(obj)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26b66058-6da9-45b3-94a7-e55146db3aab", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "027750e3-ccec-430b-9054-f5b0b7b8aaff", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 121, + "id": "df82b910-1648-4334-a29f-252eb7ff9ffe", + "metadata": {}, + "outputs": [], + "source": [ + "from time import time\n", + "\n", + "\n", + "def deco(fn):\n", + " def inner(*args, **kwargs):\n", + " t1 = time()\n", + " res = fn(*args, **kwargs)\n", + " t2 = time()\n", + " print(fn.__name__, t2 - t1)\n", + " return res\n", + " return inner\n", + "\n", + "\n", + "@deco\n", + "def add(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def sub(a, b):\n", + " return a - b\n", + "\n", + "sub = deco(sub)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "bd8cdc27-586f-4c99-a4f2-d529a753a056", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "add 9.5367431640625e-07\n" + ] + }, + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "d0c01d7e-a3ac-4ff9-b5f5-48fe9941dadb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sub 1.1920928955078125e-06\n" + ] + }, + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sub(2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "id": "cf394baf-f8a0-4aa4-b6f2-5cd8835c6c6d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(.inner(*args, **kwargs)>,\n", + " .inner(*args, **kwargs)>)" + ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add, sub" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "941b3ca5-17cd-4dc5-b112-b1514da606a6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'inner'" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add.__name__" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "f16b8767-00b5-4a2e-9e6c-78a6b0a80653", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add.__closure__[0].cell_contents" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "id": "c129d4a7-1b71-40b1-a568-e148d7f3fb76", + "metadata": {}, + "outputs": [], + "source": [ + "from time import time\n", + "from functools import wraps\n", + "\n", + "\n", + "def deco_wraps(fn):\n", + " @wraps(fn)\n", + " def inner(*args, **kwargs):\n", + " t1 = time()\n", + " res = fn(*args, **kwargs)\n", + " t2 = time()\n", + " print(fn.__name__, t2 - t1)\n", + " return res\n", + " return inner\n", + "\n", + "\n", + "@deco_wraps\n", + "def add(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def sub(a, b):\n", + " return a - b\n", + "\n", + "sub = deco_wraps(sub)" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "1bcd37dc-a255-4898-8e9d-ef6e6bb8d061", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "add 9.5367431640625e-07\n", + "sub 9.5367431640625e-07\n" + ] + }, + { + "data": { + "text/plain": [ + "(4, -5)" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(1, 3), sub(3, 8)" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "414c048a-c360-41bd-9d8d-6c5185924bcc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'add'" + ] + }, + "execution_count": 129, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add.__name__" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "id": "02df674d-6a8e-4c0a-8528-bd92adf3c261", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add.__wrapped__" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6e349612-d676-43cb-815f-c2e85c784bd7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac62121e-3c38-4b3e-ac48-18296503040a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9138a832-80ce-4445-b937-dee929132859", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 139, + "id": "72dbc281-b198-4c98-8880-94553fe8327f", + "metadata": {}, + "outputs": [], + "source": [ + "from time import time\n", + "from functools import wraps\n", + "\n", + "\n", + "def limited_deco(limit=10):\n", + " def deco_wraps(fn):\n", + " times = []\n", + "\n", + " @wraps(fn)\n", + " def inner(*args, **kwargs):\n", + " t1 = time()\n", + " res = fn(*args, **kwargs)\n", + " t2 = time()\n", + " times.append(t2 - t1)\n", + "\n", + " if len(times) > limit:\n", + " times.pop(0)\n", + "\n", + " print(fn.__name__, times)\n", + " return res\n", + "\n", + " return inner\n", + "\n", + " return deco_wraps\n", + "\n", + "\n", + "@limited_deco(3)\n", + "def add(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def sub(a, b):\n", + " return a - b\n", + "\n", + "sub = limited_deco(2)(sub)" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "id": "152a7ea8-f3e4-4196-a9e1-a6d1b5eb3f19", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "add [1.6689300537109375e-06]\n", + "add [1.6689300537109375e-06, 9.5367431640625e-07]\n", + "add [1.6689300537109375e-06, 9.5367431640625e-07, 0.0]\n", + "add [9.5367431640625e-07, 0.0, 0.0]\n" + ] + }, + { + "data": { + "text/plain": [ + "(3, 5, 7, 11)" + ] + }, + "execution_count": 140, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(1, 2), add(2, 3), add(3, 4), add(5, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 136, + "id": "de569536-aa0d-4d48-83c1-5fa58f321f5a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "add [9.5367431640625e-07, 2.1457672119140625e-06]\n" + ] + }, + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 136, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(1, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "id": "e10dc07b-26c0-4184-894e-316658ed23b8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sub [1.9073486328125e-06]\n", + "sub [1.9073486328125e-06, 9.5367431640625e-07]\n", + "sub [9.5367431640625e-07, 9.5367431640625e-07]\n", + "sub [9.5367431640625e-07, 9.5367431640625e-07]\n" + ] + }, + { + "data": { + "text/plain": [ + "(-1, -1, -1, -1)" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sub(1, 2), sub(2, 3), sub(3, 4), sub(5, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5af6b38-e70d-4eef-81c0-5bb8717b33b9", + "metadata": {}, + "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.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}