From 09d563b9364fe6c89f5af92db7702217570d0c1f Mon Sep 17 00:00:00 2001 From: Brian H Miller <41061953+SmokingPuffin@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:23:14 -0800 Subject: [PATCH] clear outputs in notebooks --- content/00_about_the_course.ipynb | 6 +- content/01_python_basic_overview.ipynb | 673 +-------- content/02_memory_management_in_python.ipynb | 1008 ++----------- ...03_function_parameters_and_arguments.ipynb | 347 +---- .../04_namespaces_scopes_and_closures.ipynb | 579 +------- content/05_other_functions_concepts.ipynb | 1323 ++--------------- ...06_sequence_iterators_and_generators.ipynb | 837 ++--------- content/07_context_managers.ipynb | 635 +------- content/08_argparse.ipynb | 317 +--- content/09_unittest.ipynb | 519 +------ content/10_concurrency.ipynb | 943 +----------- content/14_python_builtin_cli.ipynb | 28 +- 12 files changed, 802 insertions(+), 6413 deletions(-) diff --git a/content/00_about_the_course.ipynb b/content/00_about_the_course.ipynb index eae3641..f2a6970 100644 --- a/content/00_about_the_course.ipynb +++ b/content/00_about_the_course.ipynb @@ -21,7 +21,7 @@ "\n", "## ✨ Try it in your browser ✨\n", "\n", - "➡️ **https://debakarr.github.io/intermediate-python**\n", + "➡️ **https://smokingpuffin.github.io/intermediate-python**\n", "\n", "![](./static/use_jupyterlite.gif)\n", "\n", @@ -49,9 +49,9 @@ "metadata": {}, "source": [ "---\n", - "This course is targeted for developers who have already beginner experience with Python and want to level up to intermediate to advance level.\n", + "This course is targeted for developers who have some experience with Python and want to increase their understanding.\n", "\n", - "Course is delivered by **Roy, Debakar**." + "Course is delivered by **Brian Miller** based on course materials from **Debakar Roy**." ] }, { diff --git a/content/01_python_basic_overview.ipynb b/content/01_python_basic_overview.ipynb index 767a779..4732581 100644 --- a/content/01_python_basic_overview.ipynb +++ b/content/01_python_basic_overview.ipynb @@ -30,54 +30,22 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "cfae3108-2ca3-4253-9882-70aac2d5337b", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function sum in module builtins:\n", - "\n", - "sum(iterable, /, start=0)\n", - " Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n", - " \n", - " When the iterable is empty, return the start value.\n", - " This function is intended specifically for use with numeric values and may\n", - " reject non-numeric types.\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(sum)" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "37d9908d-e849-4000-9bc3-aaca3cf5e5aa", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function sum in module builtins:\n", - "\n", - "sum(iterable, /, start=0)\n", - " Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n", - " \n", - " When the iterable is empty, return the start value.\n", - " This function is intended specifically for use with numeric values and may\n", - " reject non-numeric types.\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "# This cell does not work on WebAssembly. Try it out in your local terminal.\n", "\n", @@ -262,23 +230,10 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "02f62766-1e00-4225-8269-a91d621183dc", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "support can be used as identifier? True\n", - "$upport can be used as identifier? False\n", - "1name can be used as identifier? False\n", - "name1 can be used as identifier? True\n", - "_private_var can be used as identifier? True\n", - "from can be used as identifier? True\n" - ] - } - ], + "outputs": [], "source": [ "possible_identifiers = [\"support\", \"$upport\", \"1name\", \"name1\", \"_private_var\", \"from\"]\n", "\n", @@ -306,85 +261,20 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "b819877e-f0f0-4f7d-bfe5-dc8482d6424f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Here is a list of the Python keywords. Enter any keyword to get more help.\n", - "\n", - "False class from or\n", - "None continue global pass\n", - "True def if raise\n", - "and del import return\n", - "as elif in try\n", - "assert else is while\n", - "async except lambda with\n", - "await finally nonlocal yield\n", - "break for not \n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(\"keywords\")" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "95afd92b-9fe7-42ed-99e9-f4a9a1d7a6c6", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['False',\n", - " 'None',\n", - " 'True',\n", - " 'and',\n", - " 'as',\n", - " 'assert',\n", - " 'async',\n", - " 'await',\n", - " 'break',\n", - " 'class',\n", - " 'continue',\n", - " 'def',\n", - " 'del',\n", - " 'elif',\n", - " 'else',\n", - " 'except',\n", - " 'finally',\n", - " 'for',\n", - " 'from',\n", - " 'global',\n", - " 'if',\n", - " 'import',\n", - " 'in',\n", - " 'is',\n", - " 'lambda',\n", - " 'nonlocal',\n", - " 'not',\n", - " 'or',\n", - " 'pass',\n", - " 'raise',\n", - " 'return',\n", - " 'try',\n", - " 'while',\n", - " 'with',\n", - " 'yield']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import keyword\n", "\n", @@ -394,23 +284,14 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "45159ce4-1757-4379-9e66-baf4082a62b7", "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (307922934.py, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[22], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m with = 1\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "with = 1" ] @@ -471,27 +352,7 @@ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "p1 = <__main__.Point object at 0x000001FA22B13D10>\n", - "p2 = <__main__.Point object at 0x000001FA22B13E50>\n" - ] - }, - { - "ename": "TypeError", - "evalue": "unsupported operand type(s) for +: 'Point' and 'Point'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\01_python_basic_overview.ipynb Cell 23\u001b[0m line \u001b[0;36m1\n\u001b[0;32m 10\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mp1\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 11\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mp2\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 12\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mp1\u001b[39m \u001b[39;49m\u001b[39m+\u001b[39;49m\u001b[39m \u001b[39;49mp2\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Point' and 'Point'" - ] - } - ], + "outputs": [], "source": [ "class Point:\n", " def __init__(self, x, y):\n", @@ -551,15 +412,7 @@ "execution_count": null, "id": "9d2f3b80-8379-4738-95bf-47540b12a958", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting example/identifier_example.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/identifier_example.py\n", "_private_variable = 1\n", @@ -599,15 +452,7 @@ "execution_count": null, "id": "5b15b84e-1453-44ef-99a6-188f3ae119ed", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting example/main.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/main.py\n", "from identifier_example import *\n", @@ -624,17 +469,7 @@ "execution_count": null, "id": "09874de0-3195-4182-9d5a-1546bdb2a05d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "dir() = ['Example', 'PublicClass', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__nonzero__', '__package__', '__spec__', 'public_func', 'public_variable']\n", - "\n", - "dir(e) = ['_Example__mangled_method', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_private_method', 'public_method']\n" - ] - } - ], + "outputs": [], "source": [ "# Similar to running \"python example/main.py\"\n", "%run example/main.py" @@ -745,20 +580,7 @@ "execution_count": null, "id": "c1dda07f-6a26-4569-8b9a-ebcc4bfc1f70", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "type(10) = \n", - "(10).__class__ = \n", - "type('Hello') = \n", - "\n", - "isinstance(10, int) = True\n", - "isinstance('Hello', int) = False\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{type(10) = }\")\n", "print(f\"{type(10) = }\")\n", @@ -774,18 +596,7 @@ "execution_count": null, "id": "64bca0dd-b349-49bb-bb77-295c5421f4cb", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(int, int, int)" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(10), (10).__class__, int" ] @@ -803,15 +614,7 @@ "execution_count": null, "id": "aff6c1e0-d72e-468c-b521-97f2b22b0245", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "isinstance(square, FunctionType) = True\n" - ] - } - ], + "outputs": [], "source": [ "def square(num):\n", " return num**2\n", @@ -843,16 +646,7 @@ "execution_count": null, "id": "9709b820-98d4-4d7b-9bb7-326d73b6b12c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "~~Meow~~\n", - "barbarbar\n" - ] - } - ], + "outputs": [], "source": [ "Cat = type(\"Cat\", (), {\"meow\": lambda self: print(\"~~Meow~~\")})\n", "c = Cat()\n", @@ -1227,7 +1021,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "id": "cdd4be4d", "metadata": { "slideshow": { @@ -1256,7 +1050,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "efd3cac4", "metadata": { "slideshow": { @@ -1264,18 +1058,7 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hey Brian, check the memory address 0x1922cf593 for hint!'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Hey %s, check the memory address 0x%x for hint!\" % (name, memory_address)" ] @@ -1313,7 +1096,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "0a2155f5", "metadata": { "slideshow": { @@ -1321,25 +1104,14 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hey Brian, check the memory address 0x1922cf593 for hint!'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Hey {}, check the memory address 0x{:x} for hint!\".format(name, memory_address)" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "3781256f", "metadata": { "slideshow": { @@ -1347,18 +1119,7 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hey Brian, check the memory address 0x6747387283 for hint!'" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Hey {name}, check the memory address 0x{memory_address} for hint!\".format(\n", " name=name, memory_address=memory_address\n", @@ -1382,7 +1143,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "id": "33036211", "metadata": { "slideshow": { @@ -1390,25 +1151,14 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hey Brian, check the memory address 0x1922cf593 for hint!'" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f\"Hey {name}, check the memory address 0x{memory_address:x} for hint!\"" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "id": "5a584b31", "metadata": { "slideshow": { @@ -1416,25 +1166,14 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "\"name = 'Brian', memory_address = 6747387283\"" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f\"{name = }, {memory_address = }\"" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "id": "62ee237b", "metadata": { "slideshow": { @@ -1442,25 +1181,14 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'2 + 2 = 4'" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f\"{2 + 2 = }\"" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "id": "81bdcc8d", "metadata": { "slideshow": { @@ -1468,18 +1196,7 @@ }, "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "\"dir() = ['Cat', 'Example', 'Foo', 'FunctionType', 'In', 'Out', 'Point', 'PublicClass', '_', '_16', '_21', '_23', '_24', '_26', '_27', '_28', '_3', '_9', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__nonzero__', '__package__', '__spec__', '__vsc_ipynb_file__', '_dh', '_exit_code', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i22', '_i23', '_i24', '_i25', '_i26', '_i27', '_i28', '_i29', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'builtins', 'c', 'debugpy', 'e', 'exit', 'file', 'get_ipython', 'ipykernel', 'keyword', 'memory_address', 'name', 'open', 'p1', 'p2', 'possible_identifier', 'possible_identifiers', 'public_func', 'public_variable', 'quit', 'square']\"" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f\"{dir() = }\"" ] @@ -1518,21 +1235,10 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "id": "d6bf4437-150b-4097-8406-35d32d52b4ca", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3.3000000000000003" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "1.1 + 2.2" ] @@ -1692,18 +1398,10 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "9843aace-0147-408a-ab0d-8643be6a0f0f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "You are an adult.\n" - ] - } - ], + "outputs": [], "source": [ "import sys\n", "\n", @@ -1753,21 +1451,10 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "id": "c2a3a36e-008f-4c6a-9053-9938556dab3b", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Index 1: apple\n", - "Index 2: banana\n", - "Index 3: orange\n", - "Index 4: grape\n" - ] - } - ], + "outputs": [], "source": [ "fruits = [\"apple\", \"banana\", \"orange\", \"grape\"]\n", "\n", @@ -1777,19 +1464,10 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "id": "4b6cb93d-837f-49ec-aea6-418938fbe8c4", "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (2525040281.py, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[34], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m break if num == 5\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "for num in range(1, 11):\n", " if num == 5:\n", @@ -1801,23 +1479,10 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "id": "8eb46ecc-145d-41e4-a344-15cfce42f3a2", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "3\n", - "5\n", - "7\n", - "9\n", - "Loop completed successfully\n" - ] - } - ], + "outputs": [], "source": [ "for num in range(1, 11):\n", " if num % 2 == 0:\n", @@ -1924,7 +1589,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "id": "043282dd-e5e9-4acd-9824-f3ad0edf556f", "metadata": {}, "outputs": [], @@ -1961,21 +1626,10 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "id": "c1b2ff74-316f-49a9-893c-23150588712a", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "deque([2, 3, 4], maxlen=3)" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Bounded deque\n", "# Say you want to keep track of last two request that are made\n", @@ -2007,22 +1661,10 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "id": "52bc011a-dc11-4483-ab11-5ba9d500a7a7", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Collecting tabulate\n", - " Downloading tabulate-0.9.0-py3-none-any.whl (35 kB)\n", - "Installing collected packages: tabulate\n", - "Successfully installed tabulate-0.9.0\n", - "Note: you may need to restart the kernel to use updated packages.\n" - ] - } - ], + "outputs": [], "source": [ "# in case you are behind a company proxy use --proxy flag\n", "%pip install tabulate" @@ -2030,7 +1672,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "id": "11d45c7e-d068-48f7-9b7d-61f6ee000b2f", "metadata": { "tags": [] @@ -2080,42 +1722,10 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "id": "fabae644-8db3-460e-882f-074b91fc910c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "List Operations:\n", - "+-------------------+----------------+----------------+----------------+--------+\n", - "| Code | N=10000 (µs) | N=20000 (µs) | N=30000 (µs) | Time |\n", - "+===================+================+================+================+========+\n", - "| list.pop() | 28.10 µs | 1.80 µs | 3.40 µs | O(1) |\n", - "+-------------------+----------------+----------------+----------------+--------+\n", - "| list.pop(0) | 32.70 µs | 42.30 µs | 66.20 µs | O(N) |\n", - "+-------------------+----------------+----------------+----------------+--------+\n", - "| list.append(1) | 24.60 µs | 111.40 µs | 159.30 µs | O(1) |\n", - "+-------------------+----------------+----------------+----------------+--------+\n", - "| list.insert(0, 1) | 21.60 µs | 24.50 µs | 39.70 µs | O(N) |\n", - "+-------------------+----------------+----------------+----------------+--------+\n", - "\n", - "Deque Operations:\n", - "+---------------------+----------------+----------------+----------------+--------+\n", - "| Code | N=10000 (µs) | N=20000 (µs) | N=30000 (µs) | Time |\n", - "+=====================+================+================+================+========+\n", - "| deque.pop() | 28.10 µs | 4.30 µs | 2.30 µs | O(1) |\n", - "+---------------------+----------------+----------------+----------------+--------+\n", - "| deque.popleft() | 24.80 µs | 1.20 µs | 1.00 µs | O(1) |\n", - "+---------------------+----------------+----------------+----------------+--------+\n", - "| deque.append(1) | 11.40 µs | 0.80 µs | 0.80 µs | O(1) |\n", - "+---------------------+----------------+----------------+----------------+--------+\n", - "| deque.appendleft(1) | 16.60 µs | 0.90 µs | 1.10 µs | O(1) |\n", - "+---------------------+----------------+----------------+----------------+--------+\n" - ] - } - ], + "outputs": [], "source": [ "# This cell does not work on WebAssembly. Try it out in your local terminal.\n", "# You can copy the above two cell and this cell in the Python interpreter\n", @@ -2654,7 +2264,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "id": "08f2cddb-e72c-4cf5-8b43-ad19d248b803", "metadata": {}, "outputs": [], @@ -2673,21 +2283,10 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "id": "35855069-4a48-44b0-a5b6-64a785d8bda0", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 1, 'b': 3, 'c': 4}" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "dict3 = {**dict1, **dict2}\n", "dict3" @@ -2703,21 +2302,10 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "id": "ea01912c-c31d-4b67-89cf-b629b20aa8b0", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 1, 'b': 3, 'c': 4}" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "dict3 = dict1 | dict2\n", "dict3" @@ -2733,18 +2321,10 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "id": "c7a036ad-1208-4e4b-a673-98f41e2c4574", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ChainMap({'a': 1, 'b': 2}, {'b': 3, 'c': 4})\n" - ] - } - ], + "outputs": [], "source": [ "from collections import ChainMap\n", "\n", @@ -2755,19 +2335,10 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "id": "6b8c48a0-5cb1-42e2-afb6-9f447d8b6553", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ChainMap({'d': 5}, {'a': 1, 'b': 2}, {'b': 3, 'c': 4})\n", - "5\n" - ] - } - ], + "outputs": [], "source": [ "# Using new_child() to add a new dictionary\n", "dict3 = {\"d\": 5}\n", @@ -2786,18 +2357,10 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "id": "387ef796-5aff-4401-9d7e-7c3357a679e8", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "b'hello'\n" - ] - } - ], + "outputs": [], "source": [ "# Creating bytes from a string\n", "my_bytes = b\"hello\"\n", @@ -2806,18 +2369,10 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "id": "18cc975a-0f4c-49c0-ac59-e6e12bf4e7e9", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "104\n" - ] - } - ], + "outputs": [], "source": [ "# Accessing bytes elements\n", "print(my_bytes[0])" @@ -2825,18 +2380,10 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "id": "c0e0dd4a-245a-497a-ab3f-d60604c0f706", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello\n" - ] - } - ], + "outputs": [], "source": [ "# Converting bytes to string\n", "my_string = my_bytes.decode(\"utf-8\")\n", @@ -2853,18 +2400,10 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "id": "a4c11798-2ee8-4e58-93c6-07855da8694a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "bytearray(b'ABC')\n" - ] - } - ], + "outputs": [], "source": [ "# Creating a bytearray from a list of integers\n", "my_bytearray = bytearray([65, 66, 67]) # ASCII values of 'A', 'B', 'C'\n", @@ -2873,18 +2412,10 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "id": "7c1f83a6-d661-46fe-9b10-ea1e6976ffa0", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "66\n" - ] - } - ], + "outputs": [], "source": [ "# Accessing bytearray elements\n", "print(my_bytearray[1])" @@ -2892,18 +2423,10 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "id": "00efbc3c-ba4e-4d06-8f5e-bd3770076577", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "bytearray(b'ADC')\n" - ] - } - ], + "outputs": [], "source": [ "# Modifying a bytearray element\n", "my_bytearray[1] = 68 # ASCII value of 'D'\n", @@ -2912,18 +2435,10 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "id": "7b031b19-eb53-41b0-a745-fb44a32a9140", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "b'ADC'\n" - ] - } - ], + "outputs": [], "source": [ "# Converting bytearray to bytes\n", "my_bytes = bytes(my_bytearray)\n", @@ -3000,18 +2515,10 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "id": "e8fb38df-b501-42d0-b244-b286a9ceebad", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[4, 16, 36, 64, 100]\n" - ] - } - ], + "outputs": [], "source": [ "squares = [x**2 for x in range(1, 11) if x % 2 == 0]\n", "print(squares)" @@ -3027,18 +2534,10 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "id": "c830e3c1-512e-43ab-ba71-aa9004210e52", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}\n" - ] - } - ], + "outputs": [], "source": [ "square_set = {x**2 for x in range(1, 11)}\n", "print(square_set)" @@ -3054,18 +2553,10 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "id": "e627833f-316f-4464-91c0-96816a9b8fe6", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n" - ] - } - ], + "outputs": [], "source": [ "square_dict = {x: x**2 for x in range(1, 6)}\n", "print(square_dict)" diff --git a/content/02_memory_management_in_python.ipynb b/content/02_memory_management_in_python.ipynb index 2b7f957..21a9be5 100644 --- a/content/02_memory_management_in_python.ipynb +++ b/content/02_memory_management_in_python.ipynb @@ -55,23 +55,10 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "id": "02eee63d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x7ffafc83e448\n", - "0x2ab799c5e70\n", - "0x2ab7aee9e80\n", - "0x2ab7aee6b80\n", - "0x2ab7ae75120\n", - "0x2ab79d13710\n" - ] - } - ], + "outputs": [], "source": [ "a_int = 10\n", "a_str = \"hello\"\n", @@ -97,23 +84,10 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "id": "0a1f09f8", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "print(isinstance(a_int, object))\n", "print(isinstance(a_str, object))\n", @@ -147,19 +121,10 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "id": "241a5072", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "140715955056520\n", - "0x7ffafc83ef88\n" - ] - } - ], + "outputs": [], "source": [ "a = 100\n", "\n", @@ -183,19 +148,10 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "id": "be092255", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "140715955056520\n", - "0x7ffafc83ef88\n" - ] - } - ], + "outputs": [], "source": [ "b = a\n", "\n", @@ -216,19 +172,10 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "id": "2f99f365-2e78-4e96-b821-a172d60daada", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2935502954096\n", - "0x2ab799c5e70\n" - ] - } - ], + "outputs": [], "source": [ "c = \"hello\"\n", "\n", @@ -239,19 +186,10 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "id": "57a6dc1a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "100\n", - "hello\n" - ] - } - ], + "outputs": [], "source": [ "# We can use ctypes.cast to create a new Python object which reference to same address\n", "import ctypes\n", @@ -275,7 +213,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "id": "6dc5ea41-cbf2-4046-81ec-9c5298b27b54", "metadata": {}, "outputs": [], @@ -295,18 +233,10 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "id": "f704f670-fb26-4bac-be9a-2c7cd7791afc", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "after_reference_by_other = 3\n" - ] - } - ], + "outputs": [], "source": [ "import sys\n", "\n", @@ -322,21 +252,10 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "id": "8b285bcd", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'0x2ab7ae4dc30'" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "hex(id(my_var))" ] @@ -351,19 +270,10 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "id": "098982fb", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Starting reference: 3\n", - "After another variable reference: 4\n" - ] - } - ], + "outputs": [], "source": [ "# The counter starts at 1 when an object is created.\n", "before = sys.getrefcount(my_var)\n", @@ -386,29 +296,10 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "id": "bb15bfb1", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2935524482096\n", - "2935524482096\n" - ] - }, - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "print(id(my_var))\n", "print(id(my_var2))\n", @@ -425,21 +316,10 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "id": "a7c05015", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# We can actually use ctypes to get the actual reference counter\n", "address = id(my_var)\n", @@ -448,23 +328,12 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "id": "8be4fae5", "metadata": { "scrolled": true }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# reference counter decrements when a reference is deleted.\n", "del my_var2\n", @@ -473,44 +342,22 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "id": "6411f010", "metadata": { "scrolled": true }, - "outputs": [ - { - "data": { - "text/plain": [ - "12324" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "ctypes.cast(address, ctypes.py_object).value" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "id": "aa535187", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# The counter reaching zero indicates no more references to the object.\n", "del my_var\n", @@ -522,7 +369,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "id": "4d84831e", "metadata": {}, "outputs": [], @@ -564,105 +411,50 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "id": "8d108156-0f27-4d12-8e38-5e7552fbd962", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "28" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof(1)" ] }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "id": "2114037b-1d2a-4605-8639-caf1ffde6cbe", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "55" - ] - }, - "execution_count": 60, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof(\"Python\")" ] }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "id": "1958a63f-0434-438e-a787-4fa5ab430d0b", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "88" - ] - }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([1, 2, 3, 4])" ] }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "id": "de17b5bd-ffa4-4382-a869-f8aaefec2996", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "72" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof((1, 2, 3, 4))" ] }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "id": "d1ff3e60-ca95-45b9-94b3-d4bc313abb55", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "216" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof({1, 2, 3, 4})" ] @@ -685,21 +477,10 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "id": "ceab6ccc-4280-4bda-b947-b7e9726d1893", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "49" - ] - }, - "execution_count": 64, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# It should be 49, but jupyter notebook it shows 51 bytes\n", "# https://stackoverflow.com/questions/53899931/why-does-an-empty-string-in-python-sometimes-take-up-49-bytes-and-sometimes-51\n", @@ -709,63 +490,30 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "id": "338a92c7-f3e3-47cc-9c83-5a8c997de266", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "50" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof(\"P\")" ] }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "id": "f1ff1f62-463a-44aa-a217-f14a31f0d922", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "51" - ] - }, - "execution_count": 66, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof(\"Py\")" ] }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "id": "def19e8d-77bc-49d9-89a0-c8b02518ae21", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "52" - ] - }, - "execution_count": 67, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof(\"Pyt\")" ] @@ -788,63 +536,30 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "id": "b28bf891-a51d-4b03-aac9-4a335984b02a", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "56" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([])" ] }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "id": "ce5ead31-b656-4798-86be-35c8fe526570", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "64" - ] - }, - "execution_count": 69, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([1])" ] }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "id": "ba53a27a-0d49-4221-98d2-61912d078213", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "72" - ] - }, - "execution_count": 70, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([1, 2])" ] @@ -859,63 +574,30 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": null, "id": "314c69f8-c3fa-42bb-9790-8126e5a3c4ee", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "72" - ] - }, - "execution_count": 71, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([1, 2])" ] }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "id": "a764e163-f80c-447a-a3bc-af6cafc3117b", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "88" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([1, 2, 3, 4])" ] }, { "cell_type": "code", - "execution_count": 73, + "execution_count": null, "id": "d0db866c-dd63-46ea-997f-47ed44a86709", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "80" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sys.getsizeof([1, 2, [1, 2, 3, 4]])" ] @@ -943,40 +625,20 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": null, "id": "d297efa7-c852-40c8-9267-cb1cdec09113", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: pympler in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (1.0.1)Note: you may need to restart the kernel to use updated packages.\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "%pip install pympler" ] }, { "cell_type": "code", - "execution_count": 75, + "execution_count": null, "id": "bb5ccdd4-9a9c-4618-983a-d04668367947", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "296" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from pympler import asizeof\n", "\n", @@ -1008,7 +670,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "id": "48c38563-5fcb-4532-be10-13bcef06b128", "metadata": {}, "outputs": [], @@ -1023,7 +685,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "id": "ea7b2b3d-f744-41a5-8d08-1a53ad04cc23", "metadata": {}, "outputs": [], @@ -1033,18 +695,10 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": null, "id": "55f2cdce-4c39-4d14-931a-f9d60247011d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Deleting object: <__main__.C object at 0x000002AB7616E090>\n" - ] - } - ], + "outputs": [], "source": [ "c = None" ] @@ -1059,18 +713,10 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": null, "id": "a2eb6027-220b-4b3a-a3af-ff30171c5190", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, [...]]\n" - ] - } - ], + "outputs": [], "source": [ "a = [1, 2]\n", "a.append(a)\n", @@ -1080,28 +726,17 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "id": "9c6cb1a7-5321-49ac-ba63-eebef177aaaa", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 80, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "a[2][2][2][2][1]" ] }, { "cell_type": "code", - "execution_count": 81, + "execution_count": null, "id": "a446b679-b0e6-4830-bd1d-ae2302f860e0", "metadata": {}, "outputs": [], @@ -1112,28 +747,17 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": null, "id": "50cc4f04-85f6-4efd-9938-202b5d23f8ba", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 82, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "c.cr.cr.cr.cr.x" ] }, { "cell_type": "code", - "execution_count": 83, + "execution_count": null, "id": "15e81af1-4ef9-4eaa-ae4a-374d72ae0059", "metadata": {}, "outputs": [], @@ -1143,28 +767,10 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": null, "id": "02ed7d31-60ba-4616-a554-b3759a319f97", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Deleting object: <__main__.C object at 0x000002AB7AE6B890>\n" - ] - }, - { - "data": { - "text/plain": [ - "1123" - ] - }, - "execution_count": 84, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import gc\n", "\n", @@ -1181,7 +787,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": null, "id": "d1eb97c9-f7c3-47e5-ba60-820b3d47a909", "metadata": {}, "outputs": [], @@ -1191,7 +797,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": null, "id": "67489a46-79ff-4eaf-8d11-edcb0040b702", "metadata": {}, "outputs": [], @@ -1202,19 +808,10 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": null, "id": "91529eb2-7f53-4629-8a5a-97e04d1f851e", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "<__main__.C object at 0x000002AB7AE9DFD0>\n", - "<__main__.C object at 0x000002AB7AE9D4D0>\n" - ] - } - ], + "outputs": [], "source": [ "print(c1)\n", "print(c2)" @@ -1222,7 +819,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": null, "id": "3b59d2d5-768c-4e18-ae0b-197bbc993fe1", "metadata": {}, "outputs": [], @@ -1233,7 +830,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": null, "id": "b45aadd9-9870-4f3a-aa10-2a497d40b525", "metadata": {}, "outputs": [], @@ -1244,49 +841,27 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": null, "id": "e70c50a1-1e7f-415e-9a9a-c82c6e312de9", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{<__main__.C at 0x2ab7ae9d4d0>, <__main__.C at 0x2ab7ae9dfd0>}" - ] - }, - "execution_count": 90, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "cache" ] }, { "cell_type": "code", - "execution_count": 91, + "execution_count": null, "id": "08835efd-2bd3-47d8-9dbc-0aaeeeb12e00", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 91, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "gc.collect()" ] }, { "cell_type": "code", - "execution_count": 92, + "execution_count": null, "id": "a46f2c5b-36f2-4ff6-8691-d6fba1739515", "metadata": {}, "outputs": [], @@ -1297,7 +872,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": null, "id": "840d9f01-21dc-4660-8652-c525a1fcf7ed", "metadata": {}, "outputs": [], @@ -1311,19 +886,10 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": null, "id": "3d2ee1e7-e0e3-41fa-8444-d5180bad2772", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Deleting object: <__main__.C object at 0x000002AB7AEEA490>\n", - "Deleting object: <__main__.C object at 0x000002AB7AEE9A10>\n" - ] - } - ], + "outputs": [], "source": [ "c1 = None\n", "c2 = None" @@ -1331,42 +897,20 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": null, "id": "79230af3-3b86-4c9b-880d-e9ddf0eabe4f", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{, }" - ] - }, - "execution_count": 95, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "cache" ] }, { "cell_type": "code", - "execution_count": 96, + "execution_count": null, "id": "ae911079-afdf-4561-a0e4-95250e91d4aa", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 96, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "cache.pop()() is None" ] @@ -1446,7 +990,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": null, "id": "52aceaab-dd01-4b34-8d97-ade74f473a38", "metadata": {}, "outputs": [], @@ -1469,25 +1013,10 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": null, "id": "54001c1f-a7bd-401e-a3f0-ed0088337678", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "doing heavy work...\n", - "8000\n", - "doing heavy work...\n", - "8000\n", - "doing heavy work...\n", - "8000\n", - "doing heavy work...\n", - "8000\n" - ] - } - ], + "outputs": [], "source": [ "c = Calculator(20)\n", "print(c.calculate(3))\n", @@ -1498,25 +1027,17 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": null, "id": "5a0267d1-3757-4b2e-99f4-6360a697d57d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Deleting object: <__main__.Calculator object at 0x000002AB7AE9D950>\n" - ] - } - ], + "outputs": [], "source": [ "c = None" ] }, { "cell_type": "code", - "execution_count": 101, + "execution_count": null, "id": "74dcec3d-b52e-467a-88ba-01b802b3220b", "metadata": {}, "outputs": [], @@ -1541,22 +1062,10 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": null, "id": "f9f810fa-6fd9-4ca3-807b-74c67a29d648", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "doing heavy work...\n", - "8000\n", - "8000\n", - "8000\n", - "8000\n" - ] - } - ], + "outputs": [], "source": [ "c = Calculator(20)\n", "print(c.calculate(3))\n", @@ -1577,42 +1086,20 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": null, "id": "9621ec15-96c1-486a-b795-19dd6ff2b0b9", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "CacheInfo(hits=3, misses=1, maxsize=20, currsize=1)" - ] - }, - "execution_count": 103, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "Calculator.calculate.cache_info()" ] }, { "cell_type": "code", - "execution_count": 104, + "execution_count": null, "id": "a7762f9d-bf7f-48d5-b039-ab448c59e21d", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "492" - ] - }, - "execution_count": 104, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import gc\n", "\n", @@ -1621,7 +1108,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": null, "id": "1c66e884-f223-4228-9375-62dcb54a8710", "metadata": {}, "outputs": [], @@ -1645,19 +1132,10 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": null, "id": "a955d5e3", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x7ffafc83e448\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "my_var = 10\n", "print(hex(id(my_var)))\n", @@ -1666,19 +1144,10 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": null, "id": "c85ede28", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x2ab7ae677f0\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "my_var = \"Test string\"\n", "print(hex(id(my_var)))\n", @@ -1707,19 +1176,10 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": null, "id": "6fe4969c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x2ab7ae65c00\n", - "0x2ab7ae65c00\n" - ] - } - ], + "outputs": [], "source": [ "# Mutable objects provide a way to mutate them (change the internal state)\n", "a_list = [10, 20, 30]\n", @@ -1733,19 +1193,10 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": null, "id": "2db93ff0", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x2ab7aee9e80\n", - "0x2ab7ae675c0\n" - ] - } - ], + "outputs": [], "source": [ "# Note that this create a new object in memory\n", "a_list2 = [10, 20, 30]\n", @@ -1761,18 +1212,10 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": null, "id": "b9a4112b", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "([10, 20, 30, 40], 50, 60)\n" - ] - } - ], + "outputs": [], "source": [ "a_tuple = ([10, 20, 30], 50, 60)\n", "\n", @@ -1807,19 +1250,10 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": null, "id": "4a03471f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "a_int = 10\n", "b_int = 10\n", @@ -1830,19 +1264,10 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": null, "id": "13bf186e", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], + "outputs": [], "source": [ "a_int = 999\n", "b_int = 999\n", @@ -1853,19 +1278,10 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": null, "id": "ed1396d2", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "a_str = \"python\"\n", "b_str = \"python\"\n", @@ -1876,19 +1292,10 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": null, "id": "e6a89365", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], + "outputs": [], "source": [ "a_str = \"python course\"\n", "b_str = \"python course\"\n", @@ -1907,19 +1314,10 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": null, "id": "d98fb616", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], + "outputs": [], "source": [ "a_list = [1, 2, 3, 4]\n", "b_list = [1, 2, 3, 4]\n", @@ -1930,19 +1328,10 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": null, "id": "bf205007", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], + "outputs": [], "source": [ "a_int = 10\n", "a_float = 10.0\n", @@ -1953,21 +1342,10 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": null, "id": "a580ee5a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n", - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "a_obj = None\n", "b_obj = None\n", @@ -2020,19 +1398,10 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": null, "id": "32a77a82", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "a_string = \"python\"\n", "b_string = \"python\"\n", @@ -2043,19 +1412,10 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": null, "id": "9f7db4ef", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], + "outputs": [], "source": [ "a_string = \"python!\"\n", "b_string = \"python!\"\n", @@ -2066,19 +1426,10 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": null, "id": "912e50d8", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "a_string = \"_this_string_will_be_intern_since_it_is_valid_identifier\"\n", "b_string = \"_this_string_will_be_intern_since_it_is_valid_identifier\"\n", @@ -2089,21 +1440,10 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": null, "id": "d6db6b15", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "a_string = \"Generally this is not interned, but we can intern it using sys.intern\"\n", "b_string = \"Generally this is not interned, but we can intern it using sys.intern\"\n", @@ -2136,19 +1476,10 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": null, "id": "c03a5b04", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Deleting object: <__main__.Calculator object at 0x000002AB7AE53F10>\n", - "(1440,)\n" - ] - } - ], + "outputs": [], "source": [ "c = compile(\"24 * 60\", \"\", \"eval\")\n", "print(c.co_consts)" @@ -2156,18 +1487,10 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": null, "id": "e34bfdb7", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "((1, 2, 1, 2, 1, 2, 1, 2, 1, 2),)\n" - ] - } - ], + "outputs": [], "source": [ "c = compile(\"(1, 2) * 5\", \"\", \"eval\")\n", "print(c.co_consts)" @@ -2175,18 +1498,10 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": null, "id": "0dfadd57", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('xyzxyzxyzxyz',)\n" - ] - } - ], + "outputs": [], "source": [ "c = compile('\"xyz\" * 4', \"\", \"eval\")\n", "print(c.co_consts)" @@ -2194,21 +1509,10 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": null, "id": "5299ffe6", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(None, 1440, (1, 2, 1, 2, 1, 2, 1, 2, 1, 2), 'xyzxyzxyzxyz')" - ] - }, - "execution_count": 125, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def a_func():\n", " a_int = 24 * 60\n", @@ -2351,18 +1655,10 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": null, "id": "edf4ffc0-481d-476f-adf7-69f48c8a9290", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[1, 2, 3], [1, 2, 3], [1, 2, 3]]\n" - ] - } - ], + "outputs": [], "source": [ "a_list = [[]] * 3\n", "a_list[0].append(1)\n", @@ -2405,7 +1701,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/content/03_function_parameters_and_arguments.ipynb b/content/03_function_parameters_and_arguments.ipynb index 797478a..33c6b9d 100644 --- a/content/03_function_parameters_and_arguments.ipynb +++ b/content/03_function_parameters_and_arguments.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "3179aec9", "metadata": {}, "outputs": [], @@ -53,18 +53,10 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "d7a74866", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "15\n" - ] - } - ], + "outputs": [], "source": [ "x = 10\n", "y = 5\n", @@ -98,19 +90,10 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "cb565d68", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x7ffafc83e448\n", - "0x7ffafc83e448\n" - ] - } - ], + "outputs": [], "source": [ "def get_ref_address(param1):\n", " return hex(id(param1))\n", @@ -143,20 +126,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "f49337b5", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10\n", - "11\n", - "10\n" - ] - } - ], + "outputs": [], "source": [ "# Immutable Function Arguments (Safe Side Effects)\n", "def increment_number(number):\n", @@ -180,19 +153,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "c5e5e729", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3]\n", - "[1, 2, 3, 'New Element']\n" - ] - } - ], + "outputs": [], "source": [ "# Mutable Function Arguments (Unintended Side Effects)\n", "def append_element(my_list):\n", @@ -215,18 +179,10 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "43458149", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 2, [3, 4])\n" - ] - } - ], + "outputs": [], "source": [ "# Immutable Function Arguments (Unintended Side Effects)\n", "def modify_tuple(t):\n", @@ -559,18 +515,10 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "6f9337d0", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1, y = 2, z = 3\n" - ] - } - ], + "outputs": [], "source": [ "my_set = {1, 2, 3}\n", "\n", @@ -602,18 +550,10 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "05c38d6f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1, y = [2, 3, 4], z = 5\n" - ] - } - ], + "outputs": [], "source": [ "my_list = [1, 2, 3, 4, 5]\n", "\n", @@ -624,18 +564,10 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "4393abcd", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1, y = [2, 3, 4], z = 5\n" - ] - } - ], + "outputs": [], "source": [ "x, *y, z = my_list\n", "\n", @@ -675,18 +607,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "cff02955", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6]\n" - ] - } - ], + "outputs": [], "source": [ "my_list1 = [1, 2, 3]\n", "my_list2 = [4, 5, 6]\n", @@ -697,18 +621,10 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "637b38bc", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6]\n" - ] - } - ], + "outputs": [], "source": [ "merged_list = [*my_list1, *my_list2]\n", "print(merged_list)" @@ -734,7 +650,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "5053b190", "metadata": {}, "outputs": [], @@ -746,18 +662,10 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "12d46707", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'a': 3, 'b': 2, 'c': 6}\n" - ] - } - ], + "outputs": [], "source": [ "merged_dict = {**my_dict1, **my_dict2, **my_dict3}\n", "print(merged_dict)" @@ -773,18 +681,10 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "b0841d3f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'c': 3, 'a': 1, 'b': 2}\n" - ] - } - ], + "outputs": [], "source": [ "merged_dict = {**my_dict3, **my_dict2, **my_dict1}\n", "print(merged_dict)" @@ -802,26 +702,14 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "b4c41714", "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "ValueError", - "evalue": "not enough values to unpack (expected 4, got 3)", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\03_function_parameters_and_arguments.ipynb Cell 65\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m a, b, c, d \u001b[39m=\u001b[39m [\u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m, [\u001b[39m3\u001b[39m, \u001b[39m4\u001b[39m]]\n", - "\u001b[1;31mValueError\u001b[0m: not enough values to unpack (expected 4, got 3)" - ] - } - ], + "outputs": [], "source": [ "a, b, c, d = [1, 2, [3, 4]]" ] @@ -836,7 +724,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "b37032d2", "metadata": { "jupyter": { @@ -859,7 +747,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "f4ca2cd0", "metadata": { "jupyter": { @@ -867,15 +755,7 @@ }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "a = 1, b = 2, c = 3, d = 4\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{a = }, {b = }, {c = }, {d = }\")" ] @@ -979,7 +859,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "d54b3a73", "metadata": {}, "outputs": [], @@ -990,44 +870,24 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "id": "1d16f315", "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "func() missing 1 required keyword-only argument: 'z'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\03_function_parameters_and_arguments.ipynb Cell 80\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m func(\u001b[39m1\u001b[39;49m, \u001b[39m2\u001b[39;49m, \u001b[39m3\u001b[39;49m, \u001b[39m4\u001b[39;49m, \u001b[39m5\u001b[39;49m, \u001b[39m6\u001b[39;49m)\n", - "\u001b[1;31mTypeError\u001b[0m: func() missing 1 required keyword-only argument: 'z'" - ] - } - ], + "outputs": [], "source": [ "func(1, 2, 3, 4, 5, 6)" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "id": "8ba8b147", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "w = 1, x = 2, y = (3, 4, 5), z = 6\n" - ] - } - ], + "outputs": [], "source": [ "func(1, 2, 3, 4, 5, z=6)" ] @@ -1042,25 +902,17 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "id": "c7bf4d24", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "w = 1, x = 2, y = (), z = 6\n" - ] - } - ], + "outputs": [], "source": [ "func(1, 2, z=6)" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "id": "50c749dd", "metadata": {}, "outputs": [], @@ -1071,18 +923,10 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "id": "e93a66c0", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1, y = 2, z = 1\n" - ] - } - ], + "outputs": [], "source": [ "func(1, 2, z=1)" ] @@ -1120,7 +964,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "id": "6a597d88", "metadata": {}, "outputs": [], @@ -1135,20 +979,10 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "e7d4e334", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "w = 1, x = 2, args = (3, 4, 5, 6), y = 2, z = 3\n", - "w = 1, x = 2, args = (3, 4, 5, 6), y = 2, z = 2\n", - "w = 1, x = 1, args = (), y = 2, z = 2\n" - ] - } - ], + "outputs": [], "source": [ "catch_all_args(1, 2, 3, 4, 5, 6, y=2, z=3)\n", "catch_all_args(1, 2, 3, 4, 5, 6, y=2)\n", @@ -1157,46 +991,24 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "id": "bc80779f", "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "no_positional_argument() takes from 1 to 2 positional arguments but 6 positional arguments (and 2 keyword-only arguments) were given", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\03_function_parameters_and_arguments.ipynb Cell 91\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m no_positional_argument(\u001b[39m1\u001b[39;49m, \u001b[39m2\u001b[39;49m, \u001b[39m3\u001b[39;49m, \u001b[39m4\u001b[39;49m, \u001b[39m5\u001b[39;49m, \u001b[39m6\u001b[39;49m, y\u001b[39m=\u001b[39;49m\u001b[39m2\u001b[39;49m, z\u001b[39m=\u001b[39;49m\u001b[39m3\u001b[39;49m)\n", - "\u001b[1;31mTypeError\u001b[0m: no_positional_argument() takes from 1 to 2 positional arguments but 6 positional arguments (and 2 keyword-only arguments) were given" - ] - } - ], + "outputs": [], "source": [ "no_positional_argument(1, 2, 3, 4, 5, 6, y=2, z=3)" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "id": "4ebf0134", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "w = 1, x = 2, y = 2, z = 3\n", - "w = 1, x = 2, y = 2, z = 2\n", - "w = 1, x = 1, y = 2, z = 2\n" - ] - } - ], + "outputs": [], "source": [ "no_positional_argument(1, 2, y=2, z=3)\n", "no_positional_argument(1, 2, y=2)\n", @@ -1298,18 +1110,10 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "id": "c4e622ed", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello Brian!\n" - ] - } - ], + "outputs": [], "source": [ "def greet(name):\n", " print(f\"Hello {name}!\")\n", @@ -1320,51 +1124,20 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "id": "9c46f74d", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "ret is None" ] }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "id": "bf08c2fc", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 1 0 RESUME 0\n", - "\n", - " 2 2 LOAD_GLOBAL 1 (NULL + print)\n", - " 14 LOAD_CONST 1 ('Hello ')\n", - " 16 LOAD_FAST 0 (name)\n", - " 18 FORMAT_VALUE 0\n", - " 20 LOAD_CONST 2 ('!')\n", - " 22 BUILD_STRING 3\n", - " 24 PRECALL 1\n", - " 28 CALL 1\n", - " 38 POP_TOP\n", - " 40 LOAD_CONST 0 (None)\n", - " 42 RETURN_VALUE\n" - ] - } - ], + "outputs": [], "source": [ "import dis\n", "\n", @@ -1382,18 +1155,10 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "id": "aae61ebf-c159-4fc1-b513-3326faf6c5e4", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "100\n" - ] - } - ], + "outputs": [], "source": [ "val = 100\n", "\n", diff --git a/content/04_namespaces_scopes_and_closures.ipynb b/content/04_namespaces_scopes_and_closures.ipynb index adf1e41..061db03 100644 --- a/content/04_namespaces_scopes_and_closures.ipynb +++ b/content/04_namespaces_scopes_and_closures.ipynb @@ -34,180 +34,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['ArithmeticError',\n", - " 'AssertionError',\n", - " 'AttributeError',\n", - " 'BaseException',\n", - " 'BaseExceptionGroup',\n", - " 'BlockingIOError',\n", - " 'BrokenPipeError',\n", - " 'BufferError',\n", - " 'BytesWarning',\n", - " 'ChildProcessError',\n", - " 'ConnectionAbortedError',\n", - " 'ConnectionError',\n", - " 'ConnectionRefusedError',\n", - " 'ConnectionResetError',\n", - " 'DeprecationWarning',\n", - " 'EOFError',\n", - " 'Ellipsis',\n", - " 'EncodingWarning',\n", - " 'EnvironmentError',\n", - " 'Exception',\n", - " 'ExceptionGroup',\n", - " 'False',\n", - " 'FileExistsError',\n", - " 'FileNotFoundError',\n", - " 'FloatingPointError',\n", - " 'FutureWarning',\n", - " 'GeneratorExit',\n", - " 'IOError',\n", - " 'ImportError',\n", - " 'ImportWarning',\n", - " 'IndentationError',\n", - " 'IndexError',\n", - " 'InterruptedError',\n", - " 'IsADirectoryError',\n", - " 'KeyError',\n", - " 'KeyboardInterrupt',\n", - " 'LookupError',\n", - " 'MemoryError',\n", - " 'ModuleNotFoundError',\n", - " 'NameError',\n", - " 'None',\n", - " 'NotADirectoryError',\n", - " 'NotImplemented',\n", - " 'NotImplementedError',\n", - " 'OSError',\n", - " 'OverflowError',\n", - " 'PendingDeprecationWarning',\n", - " 'PermissionError',\n", - " 'ProcessLookupError',\n", - " 'RecursionError',\n", - " 'ReferenceError',\n", - " 'ResourceWarning',\n", - " 'RuntimeError',\n", - " 'RuntimeWarning',\n", - " 'StopAsyncIteration',\n", - " 'StopIteration',\n", - " 'SyntaxError',\n", - " 'SyntaxWarning',\n", - " 'SystemError',\n", - " 'SystemExit',\n", - " 'TabError',\n", - " 'TimeoutError',\n", - " 'True',\n", - " 'TypeError',\n", - " 'UnboundLocalError',\n", - " 'UnicodeDecodeError',\n", - " 'UnicodeEncodeError',\n", - " 'UnicodeError',\n", - " 'UnicodeTranslateError',\n", - " 'UnicodeWarning',\n", - " 'UserWarning',\n", - " 'ValueError',\n", - " 'Warning',\n", - " 'WindowsError',\n", - " 'ZeroDivisionError',\n", - " '__IPYTHON__',\n", - " '__build_class__',\n", - " '__debug__',\n", - " '__doc__',\n", - " '__import__',\n", - " '__loader__',\n", - " '__name__',\n", - " '__package__',\n", - " '__spec__',\n", - " 'abs',\n", - " 'aiter',\n", - " 'all',\n", - " 'anext',\n", - " 'any',\n", - " 'ascii',\n", - " 'bin',\n", - " 'bool',\n", - " 'breakpoint',\n", - " 'bytearray',\n", - " 'bytes',\n", - " 'callable',\n", - " 'chr',\n", - " 'classmethod',\n", - " 'compile',\n", - " 'complex',\n", - " 'copyright',\n", - " 'credits',\n", - " 'delattr',\n", - " 'dict',\n", - " 'dir',\n", - " 'display',\n", - " 'divmod',\n", - " 'enumerate',\n", - " 'eval',\n", - " 'exec',\n", - " 'execfile',\n", - " 'filter',\n", - " 'float',\n", - " 'format',\n", - " 'frozenset',\n", - " 'get_ipython',\n", - " 'getattr',\n", - " 'globals',\n", - " 'hasattr',\n", - " 'hash',\n", - " 'help',\n", - " 'hex',\n", - " 'id',\n", - " 'input',\n", - " 'int',\n", - " 'isinstance',\n", - " 'issubclass',\n", - " 'iter',\n", - " 'len',\n", - " 'license',\n", - " 'list',\n", - " 'locals',\n", - " 'map',\n", - " 'max',\n", - " 'memoryview',\n", - " 'min',\n", - " 'next',\n", - " 'object',\n", - " 'oct',\n", - " 'open',\n", - " 'ord',\n", - " 'pow',\n", - " 'print',\n", - " 'property',\n", - " 'range',\n", - " 'repr',\n", - " 'reversed',\n", - " 'round',\n", - " 'runfile',\n", - " 'set',\n", - " 'setattr',\n", - " 'slice',\n", - " 'sorted',\n", - " 'staticmethod',\n", - " 'str',\n", - " 'sum',\n", - " 'super',\n", - " 'tuple',\n", - " 'type',\n", - " 'vars',\n", - " 'zip']" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "dir(__builtins__)" ] @@ -221,22 +50,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Outside: Value of global val = 50\n", - "\n", - "Inside func: Value of local val = 100\n", - "\n", - "Outside after func call: Value of global val = 50\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "val = 50 # Global Scope\n", "\n", @@ -262,34 +78,13 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Outside: Value of global val = 50\n", - "\n" - ] - }, - { - "ename": "UnboundLocalError", - "evalue": "cannot access local variable 'val' where it is not associated with a value", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\04_namespaces_scopes_and_closures.ipynb Cell 9\u001b[0m line \u001b[0;36m1\n\u001b[0;32m 7\u001b[0m val \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m50\u001b[39m \u001b[39m# This line is changed\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mInside func: Value of local \u001b[39m\u001b[39m{\u001b[39;00mval\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 11\u001b[0m func()\n\u001b[0;32m 13\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mOutside after func call: Value of global \u001b[39m\u001b[39m{\u001b[39;00mval\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\04_namespaces_scopes_and_closures.ipynb Cell 9\u001b[0m line \u001b[0;36m7\n\u001b[0;32m 6\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mfunc\u001b[39m():\n\u001b[1;32m----> 7\u001b[0m val \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m \u001b[39m50\u001b[39m \u001b[39m# This line is changed\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mInside func: Value of local \u001b[39m\u001b[39m{\u001b[39;00mval\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;31mUnboundLocalError\u001b[0m: cannot access local variable 'val' where it is not associated with a value" - ] - } - ], + "outputs": [], "source": [ "val = 50\n", "\n", @@ -308,24 +103,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Outside: Value of global val = 50\n", - "\n", - "Inside func: Value of local val = 100\n", - "\n", - "Outside after func call: Value of global val = 100\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "val = 50\n", "\n", @@ -352,34 +134,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Outside: Value of global val = [1, 2, 3]\n", - "\n" - ] - }, - { - "ename": "UnboundLocalError", - "evalue": "cannot access local variable 'val' where it is not associated with a value", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\04_namespaces_scopes_and_closures.ipynb Cell 12\u001b[0m line \u001b[0;36m1\n\u001b[0;32m 7\u001b[0m val \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m [\u001b[39m4\u001b[39m]\n\u001b[0;32m 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mInside func: Value of local \u001b[39m\u001b[39m{\u001b[39;00mval\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 11\u001b[0m func()\n\u001b[0;32m 13\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mOutside after func call: Value of global \u001b[39m\u001b[39m{\u001b[39;00mval\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\04_namespaces_scopes_and_closures.ipynb Cell 12\u001b[0m line \u001b[0;36m7\n\u001b[0;32m 6\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mfunc\u001b[39m():\n\u001b[1;32m----> 7\u001b[0m val \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m [\u001b[39m4\u001b[39m]\n\u001b[0;32m 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mInside func: Value of local \u001b[39m\u001b[39m{\u001b[39;00mval\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;31mUnboundLocalError\u001b[0m: cannot access local variable 'val' where it is not associated with a value" - ] - } - ], + "outputs": [], "source": [ "val = [1, 2, 3]\n", "\n", @@ -405,22 +166,9 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Outside: Value of global val = [1, 2, 3]\n", - "\n", - "Inside func: Value of local val = [1, 2, 3, 4]\n", - "\n", - "Outside after func call: Value of global val = [1, 2, 3, 4]\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "val = [1, 2, 3]\n", "\n", @@ -448,22 +196,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Outside: Value of global val = {'a': 'A', 'b': 'B'}\n", - "\n", - "Inside func: Value of local val = {'a': 'A', 'b': 'β'}\n", - "\n", - "Outside after func call: Value of global val = {'a': 'A', 'b': 'β'}\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "val = {\"a\": \"A\", \"b\": \"B\"}\n", "\n", @@ -585,32 +320,13 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Inside func2: Value of val2 = 'Defined only inside func2'\n" - ] - }, - { - "ename": "NameError", - "evalue": "name 'val2' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\04_namespaces_scopes_and_closures.ipynb Cell 22\u001b[0m line \u001b[0;36m8\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mInside func2: Value of \u001b[39m\u001b[39m{\u001b[39;00mval2\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 6\u001b[0m func2()\n\u001b[1;32m----> 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mOutside after func2 call: Value of \u001b[39m\u001b[39m{\u001b[39;00mval2\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;31mNameError\u001b[0m: name 'val2' is not defined" - ] - } - ], + "outputs": [], "source": [ "def func2():\n", " val2 = \"Defined only inside func2\"\n", @@ -624,18 +340,9 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Inside func2: Value of val2 = 'Defined only inside func2'\n", - "Outside after func2 call: Value of val2 = 'Defined only inside func2'\n" - ] - } - ], + "outputs": [], "source": [ "def func2():\n", " global val2\n", @@ -650,22 +357,9 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Value inside loop: val3 = 0\n", - "Value inside loop: val3 = 1\n", - "Value inside loop: val3 = 2\n", - "Value inside loop: val3 = 3\n", - "Value inside loop: val3 = 4\n", - "Value outside loop: val3 = 4\n" - ] - } - ], + "outputs": [], "source": [ "for val3 in range(5):\n", " print(f\"Value inside loop: {val3 = }\")\n", @@ -684,22 +378,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Value inside loop: val4 = 0\n", - "Value inside loop: val4 = 1\n", - "Value inside loop: val4 = 2\n", - "Value inside loop: val4 = 3\n", - "Value inside loop: val4 = 4\n", - "Value outside loop: val4 = 4\n" - ] - } - ], + "outputs": [], "source": [ "val4 = 100\n", "\n", @@ -718,17 +399,9 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Value outside list comp: val5 = 100\n" - ] - } - ], + "outputs": [], "source": [ "val5 = 100\n", "\n", @@ -748,18 +421,9 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "After inner call: val6 = 'enclosing scope for outer func'\n", - "local scope for inner func\n" - ] - } - ], + "outputs": [], "source": [ "def outer_func():\n", " val6 = \"enclosing scope for outer func\"\n", @@ -786,17 +450,9 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "After inner call: val7 = 'local scope for inner_func'\n" - ] - } - ], + "outputs": [], "source": [ "def outer_func():\n", " val7 = \"enclosing scope for outer func\"\n", @@ -821,17 +477,9 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Inside inner_func: val8 = 'global scope'\n" - ] - } - ], + "outputs": [], "source": [ "val8 = \"global scope\"\n", "\n", @@ -848,17 +496,9 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Inside inner_func: val8 = 'enclosing scope'\n" - ] - } - ], + "outputs": [], "source": [ "val8 = \"global scope\"\n", "\n", @@ -877,17 +517,9 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Inside inner_func: val8 = 'inner scope'\n" - ] - } - ], + "outputs": [], "source": [ "val8 = \"global scope\"\n", "\n", @@ -914,17 +546,9 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting example/scope.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/scope.py\n", "\n", @@ -958,22 +582,13 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "no binding for nonlocal 'val9' found (2039796489.py, line 6)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[23], line 6\u001b[1;36m\u001b[0m\n\u001b[1;33m nonlocal val9\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m no binding for nonlocal 'val9' found\n" - ] - } - ], + "outputs": [], "source": [ "val9 = \"global scope\"\n", "\n", @@ -1002,17 +617,9 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Brian\n" - ] - } - ], + "outputs": [], "source": [ "def outer_func():\n", " # Free variable\n", @@ -1029,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1045,17 +652,9 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Brian\n" - ] - } - ], + "outputs": [], "source": [ "func = outer_func() # inner() + extended scope `name`\n", "func()" @@ -1063,47 +662,25 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "('name',)" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "func.__code__.co_freevars" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(,)" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "func.__closure__" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1121,19 +698,9 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Address of name in outer_func: 0x1aeef4e6270\n", - "Address of name in inner_func: 0x1aeef4e6270\n", - "Brian\n" - ] - } - ], + "outputs": [], "source": [ "func = outer_func() # inner() + extended scope `name`\n", "func()" @@ -1150,7 +717,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1170,59 +737,27 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0x1aeef4e6270\n", - "0x1aef2e21bb0\n" - ] - } - ], + "outputs": [], "source": [ "func = outer_func(\"Hello\")" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "('greeting', 'name')" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "func.__code__.co_freevars" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(,\n", - " )" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "func.__closure__" ] diff --git a/content/05_other_functions_concepts.ipynb b/content/05_other_functions_concepts.ipynb index b5f6d92..2446838 100644 --- a/content/05_other_functions_concepts.ipynb +++ b/content/05_other_functions_concepts.ipynb @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -44,27 +44,16 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add(1, 2)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -76,27 +65,16 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "16" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "square(4)" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -108,39 +86,18 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Hello Brian!'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "greeting(\"Brian\")" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "type(add) = \n", - "type(square) = \n", - "type(greeting) = \n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{type(add) = }\\n{type(square) = }\\n{type(greeting) = }\")" ] @@ -161,22 +118,13 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (3143571765.py, line 4)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[8], line 4\u001b[1;36m\u001b[0m\n\u001b[1;33m add = lambda num1: int, num2: int: num1 + num2\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "# def add(num1: int, num2: int) -> int:\n", "# return num1 + num2\n", @@ -193,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -211,22 +159,13 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "cannot assign to lambda (2862269418.py, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[10], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m lambda num: num = 10\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to lambda\n" - ] - } - ], + "outputs": [], "source": [ "lambda num: num = 10" ] @@ -267,20 +206,9 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 4, 9, 16, 25]" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5]\n", "\n", @@ -295,20 +223,9 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[20, 90, 120, 20, 50]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "item_quantity = [2, 3, 6, 4, 5]\n", "item_cost = [10, 30, 20, 5, 10]\n", @@ -333,20 +250,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 4, 6, 8, 10]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "\n", @@ -368,20 +274,9 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 5, 4]" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "items = [1, 2, 0, 5, 4, False, None, [], '', 0, 0]\n", "\n", @@ -398,20 +293,9 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "a = [1, 2, 3, 4, 5]\n", "b = [6, 7, 8, 9, 10]\n", @@ -421,20 +305,9 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[(1, 6, 'a'), (2, 7, 'b'), (3, 8, 'c')]" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# length of the new iterable will be minimum of length of all the iterable\n", "a = [1, 2, 3, 4, 5]\n", @@ -453,20 +326,9 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144]" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "squares_below_150 = filter(lambda x: x < 150, \n", " map(lambda num: num**2, range(20))\n", @@ -483,20 +345,9 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144]" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "squares_below_150 = [x**2 for x in range(20) if x**2 < 150]\n", "list(squares_below_150)" @@ -504,20 +355,9 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144]" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "squares_below_150 = filter(lambda x: x < 150, [x**2 for x in range(20)])\n", "list(squares_below_150)" @@ -538,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -555,45 +395,18 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function greeting in module __main__:\n", - "\n", - "greeting(name)\n", - " Generate a greeting message for the given 'name'.\n", - " \n", - " :param str name: The name of the person to greet.\n", - " :return: A greeting message.\n", - " :rtype: str\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(greeting)" ] }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"\\n Generate a greeting message for the given 'name'.\\n\\n :param str name: The name of the person to greet.\\n :return: A greeting message.\\n :rtype: str\\n \"" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Docstring are stored in __doc__ attribute\n", "greeting.__doc__" @@ -612,7 +425,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -628,44 +441,18 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function greeting in module __main__:\n", - "\n", - "greeting(name: str) -> str\n", - " Generate a greeting message for the given 'name'.\n", - " \n", - " :param name: The name of the person to greet.\n", - " :return: A greeting message.\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(greeting)" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'name': str, 'return': str}" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "greeting.__annotations__" ] @@ -693,17 +480,9 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "add\n" - ] - } - ], + "outputs": [], "source": [ "def add(a, b):\n", " return a + b\n", @@ -721,17 +500,9 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Hello',)\n" - ] - } - ], + "outputs": [], "source": [ "def greet(name, greeting=\"Hello\"):\n", " return f\"{greeting}, {name}\"\n", @@ -749,17 +520,9 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "None\n" - ] - } - ], + "outputs": [], "source": [ "def greet(name, greeting=\"Hello\"):\n", " return f\"{greeting}, {name}\"\n", @@ -777,17 +540,9 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n" - ] - } - ], + "outputs": [], "source": [ "def add(a, b):\n", " return a + b\n", @@ -805,17 +560,9 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('a', 'b', 'result')\n" - ] - } - ], + "outputs": [], "source": [ "def multiply(a, b):\n", " result = a * b\n", @@ -834,17 +581,9 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(None, 3.14)\n" - ] - } - ], + "outputs": [], "source": [ "def multiply(a, b):\n", " pi = 3.14\n", @@ -864,17 +603,9 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('global_var',)\n" - ] - } - ], + "outputs": [], "source": [ "global_var = 42\n", "\n", @@ -895,17 +626,9 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n" - ] - } - ], + "outputs": [], "source": [ "def calculate(a, b):\n", " result = a + b\n", @@ -931,17 +654,9 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(name, greeting='Hello')\n" - ] - } - ], + "outputs": [], "source": [ "import inspect\n", "\n", @@ -963,19 +678,9 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "def square(x):\n", - " return x**2\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "import inspect\n", "\n", @@ -997,17 +702,9 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Multiply two numbers and return the result.\n" - ] - } - ], + "outputs": [], "source": [ "import inspect\n", "\n", @@ -1032,17 +729,9 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('__doc__', 'This module provides access to the mathematical functions\\ndefined by the C standard.'), ('__loader__', ), ('__name__', 'math'), ('__package__', ''), ('__spec__', ModuleSpec(name='math', loader=, origin='built-in')), ('acos', ), ('acosh', ), ('asin', ), ('asinh', ), ('atan', ), ('atan2', ), ('atanh', ), ('cbrt', ), ('ceil', ), ('comb', ), ('copysign', ), ('cos', ), ('cosh', ), ('degrees', ), ('dist', ), ('e', 2.718281828459045), ('erf', ), ('erfc', ), ('exp', ), ('exp2', ), ('expm1', ), ('fabs', ), ('factorial', ), ('floor', ), ('fmod', ), ('frexp', ), ('fsum', ), ('gamma', ), ('gcd', ), ('hypot', ), ('inf', inf), ('isclose', ), ('isfinite', ), ('isinf', ), ('isnan', ), ('isqrt', ), ('lcm', ), ('ldexp', ), ('lgamma', ), ('log', ), ('log10', ), ('log1p', ), ('log2', ), ('modf', ), ('nan', nan), ('nextafter', ), ('perm', ), ('pi', 3.141592653589793), ('pow', ), ('prod', ), ('radians', ), ('remainder', ), ('sin', ), ('sinh', ), ('sqrt', ), ('tan', ), ('tanh', ), ('tau', 6.283185307179586), ('trunc', ), ('ulp', )]\n" - ] - } - ], + "outputs": [], "source": [ "import math\n", "import inspect\n", @@ -1060,488 +749,9 @@ }, { "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "C:\\Users\\bhmiller\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\numpy\\__init__.py\n", - "\"\"\"\n", - "NumPy\n", - "=====\n", - "\n", - "Provides\n", - " 1. An array object of arbitrary homogeneous items\n", - " 2. Fast mathematical operations over arrays\n", - " 3. Linear Algebra, Fourier Transforms, Random Number Generation\n", - "\n", - "How to use the documentation\n", - "----------------------------\n", - "Documentation is available in two forms: docstrings provided\n", - "with the code, and a loose standing reference guide, available from\n", - "`the NumPy homepage `_.\n", - "\n", - "We recommend exploring the docstrings using\n", - "`IPython `_, an advanced Python shell with\n", - "TAB-completion and introspection capabilities. See below for further\n", - "instructions.\n", - "\n", - "The docstring examples assume that `numpy` has been imported as ``np``::\n", - "\n", - " >>> import numpy as np\n", - "\n", - "Code snippets are indicated by three greater-than signs::\n", - "\n", - " >>> x = 42\n", - " >>> x = x + 1\n", - "\n", - "Use the built-in ``help`` function to view a function's docstring::\n", - "\n", - " >>> help(np.sort)\n", - " ... # doctest: +SKIP\n", - "\n", - "For some objects, ``np.info(obj)`` may provide additional help. This is\n", - "particularly true if you see the line \"Help on ufunc object:\" at the top\n", - "of the help() page. Ufuncs are implemented in C, not Python, for speed.\n", - "The native Python help() does not know how to view their help, but our\n", - "np.info() function does.\n", - "\n", - "To search for documents containing a keyword, do::\n", - "\n", - " >>> np.lookfor('keyword')\n", - " ... # doctest: +SKIP\n", - "\n", - "General-purpose documents like a glossary and help on the basic concepts\n", - "of numpy are available under the ``doc`` sub-module::\n", - "\n", - " >>> from numpy import doc\n", - " >>> help(doc)\n", - " ... # doctest: +SKIP\n", - "\n", - "Available subpackages\n", - "---------------------\n", - "lib\n", - " Basic functions used by several sub-packages.\n", - "random\n", - " Core Random Tools\n", - "linalg\n", - " Core Linear Algebra Tools\n", - "fft\n", - " Core FFT routines\n", - "polynomial\n", - " Polynomial tools\n", - "testing\n", - " NumPy testing tools\n", - "distutils\n", - " Enhancements to distutils with support for\n", - " Fortran compilers support and more (for Python <= 3.11).\n", - "\n", - "Utilities\n", - "---------\n", - "test\n", - " Run numpy unittests\n", - "show_config\n", - " Show numpy build configuration\n", - "matlib\n", - " Make everything matrices.\n", - "__version__\n", - " NumPy version string\n", - "\n", - "Viewing documentation using IPython\n", - "-----------------------------------\n", - "\n", - "Start IPython and import `numpy` usually under the alias ``np``: `import\n", - "numpy as np`. Then, directly past or use the ``%cpaste`` magic to paste\n", - "examples into the shell. To see which functions are available in `numpy`,\n", - "type ``np.`` (where ```` refers to the TAB key), or use\n", - "``np.*cos*?`` (where ```` refers to the ENTER key) to narrow\n", - "down the list. To view the docstring for a function, use\n", - "``np.cos?`` (to view the docstring) and ``np.cos??`` (to view\n", - "the source code).\n", - "\n", - "Copies vs. in-place operation\n", - "-----------------------------\n", - "Most of the functions in `numpy` return a copy of the array argument\n", - "(e.g., `np.sort`). In-place versions of these functions are often\n", - "available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.\n", - "Exceptions to this rule are documented.\n", - "\n", - "\"\"\"\n", - "\n", - "\n", - "# start delvewheel patch\n", - "def _delvewheel_patch_1_5_1():\n", - " import os\n", - " libs_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'numpy.libs'))\n", - " if os.path.isdir(libs_dir):\n", - " os.add_dll_directory(libs_dir)\n", - "\n", - "\n", - "_delvewheel_patch_1_5_1()\n", - "del _delvewheel_patch_1_5_1\n", - "# end delvewheel patch\n", - "\n", - "import sys\n", - "import warnings\n", - "\n", - "from ._globals import _NoValue, _CopyMode\n", - "# These exceptions were moved in 1.25 and are hidden from __dir__()\n", - "from .exceptions import (\n", - " ComplexWarning, ModuleDeprecationWarning, VisibleDeprecationWarning,\n", - " TooHardError, AxisError)\n", - "\n", - "\n", - "# If a version with git hash was stored, use that instead\n", - "from . import version\n", - "from .version import __version__\n", - "\n", - "# We first need to detect if we're being called as part of the numpy setup\n", - "# procedure itself in a reliable manner.\n", - "try:\n", - " __NUMPY_SETUP__\n", - "except NameError:\n", - " __NUMPY_SETUP__ = False\n", - "\n", - "if __NUMPY_SETUP__:\n", - " sys.stderr.write('Running from numpy source directory.\\n')\n", - "else:\n", - " # Allow distributors to run custom init code before importing numpy.core\n", - " from . import _distributor_init\n", - "\n", - " try:\n", - " from numpy.__config__ import show as show_config\n", - " except ImportError as e:\n", - " msg = \"\"\"Error importing numpy: you should not try to import numpy from\n", - " its source directory; please exit the numpy source tree, and relaunch\n", - " your python interpreter from there.\"\"\"\n", - " raise ImportError(msg) from e\n", - "\n", - " __all__ = [\n", - " 'exceptions', 'ModuleDeprecationWarning', 'VisibleDeprecationWarning',\n", - " 'ComplexWarning', 'TooHardError', 'AxisError']\n", - "\n", - " # mapping of {name: (value, deprecation_msg)}\n", - " __deprecated_attrs__ = {}\n", - "\n", - " from . import core\n", - " from .core import *\n", - " from . import compat\n", - " from . import exceptions\n", - " from . import dtypes\n", - " from . import lib\n", - " # NOTE: to be revisited following future namespace cleanup.\n", - " # See gh-14454 and gh-15672 for discussion.\n", - " from .lib import *\n", - "\n", - " from . import linalg\n", - " from . import fft\n", - " from . import polynomial\n", - " from . import random\n", - " from . import ctypeslib\n", - " from . import ma\n", - " from . import matrixlib as _mat\n", - " from .matrixlib import *\n", - "\n", - " # Deprecations introduced in NumPy 1.20.0, 2020-06-06\n", - " import builtins as _builtins\n", - "\n", - " _msg = (\n", - " \"module 'numpy' has no attribute '{n}'.\\n\"\n", - " \"`np.{n}` was a deprecated alias for the builtin `{n}`. \"\n", - " \"To avoid this error in existing code, use `{n}` by itself. \"\n", - " \"Doing this will not modify any behavior and is safe. {extended_msg}\\n\"\n", - " \"The aliases was originally deprecated in NumPy 1.20; for more \"\n", - " \"details and guidance see the original release note at:\\n\"\n", - " \" https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\")\n", - "\n", - " _specific_msg = (\n", - " \"If you specifically wanted the numpy scalar type, use `np.{}` here.\")\n", - "\n", - " _int_extended_msg = (\n", - " \"When replacing `np.{}`, you may wish to use e.g. `np.int64` \"\n", - " \"or `np.int32` to specify the precision. If you wish to review \"\n", - " \"your current use, check the release note link for \"\n", - " \"additional information.\")\n", - "\n", - " _type_info = [\n", - " (\"object\", \"\"), # The NumPy scalar only exists by name.\n", - " (\"bool\", _specific_msg.format(\"bool_\")),\n", - " (\"float\", _specific_msg.format(\"float64\")),\n", - " (\"complex\", _specific_msg.format(\"complex128\")),\n", - " (\"str\", _specific_msg.format(\"str_\")),\n", - " (\"int\", _int_extended_msg.format(\"int\"))]\n", - "\n", - " __former_attrs__ = {\n", - " n: _msg.format(n=n, extended_msg=extended_msg)\n", - " for n, extended_msg in _type_info\n", - " }\n", - "\n", - " # Future warning introduced in NumPy 1.24.0, 2022-11-17\n", - " _msg = (\n", - " \"`np.{n}` is a deprecated alias for `{an}`. (Deprecated NumPy 1.24)\")\n", - "\n", - " # Some of these are awkward (since `np.str` may be preferable in the long\n", - " # term), but overall the names ending in 0 seem undesirable\n", - " _type_info = [\n", - " (\"bool8\", bool_, \"np.bool_\"),\n", - " (\"int0\", intp, \"np.intp\"),\n", - " (\"uint0\", uintp, \"np.uintp\"),\n", - " (\"str0\", str_, \"np.str_\"),\n", - " (\"bytes0\", bytes_, \"np.bytes_\"),\n", - " (\"void0\", void, \"np.void\"),\n", - " (\"object0\", object_,\n", - " \"`np.object0` is a deprecated alias for `np.object_`. \"\n", - " \"`object` can be used instead. (Deprecated NumPy 1.24)\")]\n", - "\n", - " # Some of these could be defined right away, but most were aliases to\n", - " # the Python objects and only removed in NumPy 1.24. Defining them should\n", - " # probably wait for NumPy 1.26 or 2.0.\n", - " # When defined, these should possibly not be added to `__all__` to avoid\n", - " # import with `from numpy import *`.\n", - " __future_scalars__ = {\"bool\", \"long\", \"ulong\", \"str\", \"bytes\", \"object\"}\n", - "\n", - " __deprecated_attrs__.update({\n", - " n: (alias, _msg.format(n=n, an=an)) for n, alias, an in _type_info})\n", - "\n", - " import math\n", - "\n", - " __deprecated_attrs__['math'] = (math,\n", - " \"`np.math` is a deprecated alias for the standard library `math` \"\n", - " \"module (Deprecated Numpy 1.25). Replace usages of `np.math` with \"\n", - " \"`math`\")\n", - "\n", - " del math, _msg, _type_info\n", - "\n", - " from .core import abs\n", - " # now that numpy modules are imported, can initialize limits\n", - " core.getlimits._register_known_types()\n", - "\n", - " __all__.extend(['__version__', 'show_config'])\n", - " __all__.extend(core.__all__)\n", - " __all__.extend(_mat.__all__)\n", - " __all__.extend(lib.__all__)\n", - " __all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])\n", - "\n", - " # Remove min and max from __all__ to avoid `from numpy import *` override\n", - " # the builtins min/max. Temporary fix for 1.25.x/1.26.x, see gh-24229.\n", - " __all__.remove('min')\n", - " __all__.remove('max')\n", - " __all__.remove('round')\n", - "\n", - " # Remove one of the two occurrences of `issubdtype`, which is exposed as\n", - " # both `numpy.core.issubdtype` and `numpy.lib.issubdtype`.\n", - " __all__.remove('issubdtype')\n", - "\n", - " # These are exported by np.core, but are replaced by the builtins below\n", - " # remove them to ensure that we don't end up with `np.long == np.int_`,\n", - " # which would be a breaking change.\n", - " del long, unicode\n", - " __all__.remove('long')\n", - " __all__.remove('unicode')\n", - "\n", - " # Remove things that are in the numpy.lib but not in the numpy namespace\n", - " # Note that there is a test (numpy/tests/test_public_api.py:test_numpy_namespace)\n", - " # that prevents adding more things to the main namespace by accident.\n", - " # The list below will grow until the `from .lib import *` fixme above is\n", - " # taken care of\n", - " __all__.remove('Arrayterator')\n", - " del Arrayterator\n", - "\n", - " # These names were removed in NumPy 1.20. For at least one release,\n", - " # attempts to access these names in the numpy namespace will trigger\n", - " # a warning, and calling the function will raise an exception.\n", - " _financial_names = ['fv', 'ipmt', 'irr', 'mirr', 'nper', 'npv', 'pmt',\n", - " 'ppmt', 'pv', 'rate']\n", - " __expired_functions__ = {\n", - " name: (f'In accordance with NEP 32, the function {name} was removed '\n", - " 'from NumPy version 1.20. A replacement for this function '\n", - " 'is available in the numpy_financial library: '\n", - " 'https://pypi.org/project/numpy-financial')\n", - " for name in _financial_names}\n", - "\n", - " # Filter out Cython harmless warnings\n", - " warnings.filterwarnings(\"ignore\", message=\"numpy.dtype size changed\")\n", - " warnings.filterwarnings(\"ignore\", message=\"numpy.ufunc size changed\")\n", - " warnings.filterwarnings(\"ignore\", message=\"numpy.ndarray size changed\")\n", - "\n", - " # oldnumeric and numarray were removed in 1.9. In case some packages import\n", - " # but do not use them, we define them here for backward compatibility.\n", - " oldnumeric = 'removed'\n", - " numarray = 'removed'\n", - "\n", - " def __getattr__(attr):\n", - " # Warn for expired attributes, and return a dummy function\n", - " # that always raises an exception.\n", - " import warnings\n", - " import math\n", - " try:\n", - " msg = __expired_functions__[attr]\n", - " except KeyError:\n", - " pass\n", - " else:\n", - " warnings.warn(msg, DeprecationWarning, stacklevel=2)\n", - "\n", - " def _expired(*args, **kwds):\n", - " raise RuntimeError(msg)\n", - "\n", - " return _expired\n", - "\n", - " # Emit warnings for deprecated attributes\n", - " try:\n", - " val, msg = __deprecated_attrs__[attr]\n", - " except KeyError:\n", - " pass\n", - " else:\n", - " warnings.warn(msg, DeprecationWarning, stacklevel=2)\n", - " return val\n", - "\n", - " if attr in __future_scalars__:\n", - " # And future warnings for those that will change, but also give\n", - " # the AttributeError\n", - " warnings.warn(\n", - " f\"In the future `np.{attr}` will be defined as the \"\n", - " \"corresponding NumPy scalar.\", FutureWarning, stacklevel=2)\n", - "\n", - " if attr in __former_attrs__:\n", - " raise AttributeError(__former_attrs__[attr])\n", - "\n", - " if attr == 'testing':\n", - " import numpy.testing as testing\n", - " return testing\n", - " elif attr == 'Tester':\n", - " \"Removed in NumPy 1.25.0\"\n", - " raise RuntimeError(\"Tester was removed in NumPy 1.25.\")\n", - "\n", - " raise AttributeError(\"module {!r} has no attribute \"\n", - " \"{!r}\".format(__name__, attr))\n", - "\n", - " def __dir__():\n", - " public_symbols = globals().keys() | {'testing'}\n", - " public_symbols -= {\n", - " \"core\", \"matrixlib\",\n", - " # These were moved in 1.25 and may be deprecated eventually:\n", - " \"ModuleDeprecationWarning\", \"VisibleDeprecationWarning\",\n", - " \"ComplexWarning\", \"TooHardError\", \"AxisError\"\n", - " }\n", - " return list(public_symbols)\n", - "\n", - " # Pytest testing\n", - " from numpy._pytesttester import PytestTester\n", - " test = PytestTester(__name__)\n", - " del PytestTester\n", - "\n", - " def _sanity_check():\n", - " \"\"\"\n", - " Quick sanity checks for common bugs caused by environment.\n", - " There are some cases e.g. with wrong BLAS ABI that cause wrong\n", - " results under specific runtime conditions that are not necessarily\n", - " achieved during test suite runs, and it is useful to catch those early.\n", - "\n", - " See https://github.com/numpy/numpy/issues/8577 and other\n", - " similar bug reports.\n", - "\n", - " \"\"\"\n", - " try:\n", - " x = ones(2, dtype=float32)\n", - " if not abs(x.dot(x) - float32(2.0)) < 1e-5:\n", - " raise AssertionError()\n", - " except AssertionError:\n", - " msg = (\"The current Numpy installation ({!r}) fails to \"\n", - " \"pass simple sanity checks. This can be caused for example \"\n", - " \"by incorrect BLAS library being linked in, or by mixing \"\n", - " \"package managers (pip, conda, apt, ...). Search closed \"\n", - " \"numpy issues for similar problems.\")\n", - " raise RuntimeError(msg.format(__file__)) from None\n", - "\n", - " _sanity_check()\n", - " del _sanity_check\n", - "\n", - " def _mac_os_check():\n", - " \"\"\"\n", - " Quick Sanity check for Mac OS look for accelerate build bugs.\n", - " Testing numpy polyfit calls init_dgelsd(LAPACK)\n", - " \"\"\"\n", - " try:\n", - " c = array([3., 2., 1.])\n", - " x = linspace(0, 2, 5)\n", - " y = polyval(c, x)\n", - " _ = polyfit(x, y, 2, cov=True)\n", - " except ValueError:\n", - " pass\n", - "\n", - " if sys.platform == \"darwin\":\n", - " with warnings.catch_warnings(record=True) as w:\n", - " _mac_os_check()\n", - " # Throw runtime error, if the test failed Check for warning and error_message\n", - " error_message = \"\"\n", - " if len(w) > 0:\n", - " error_message = \"{}: {}\".format(w[-1].category.__name__, str(w[-1].message))\n", - " msg = (\n", - " \"Polyfit sanity test emitted a warning, most likely due \"\n", - " \"to using a buggy Accelerate backend.\"\n", - " \"\\nIf you compiled yourself, more information is available at:\"\n", - " \"\\nhttps://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries\"\n", - " \"\\nOtherwise report this to the vendor \"\n", - " \"that provided NumPy.\\n{}\\n\".format(error_message))\n", - " raise RuntimeError(msg)\n", - " del _mac_os_check\n", - "\n", - " # We usually use madvise hugepages support, but on some old kernels it\n", - " # is slow and thus better avoided.\n", - " # Specifically kernel version 4.6 had a bug fix which probably fixed this:\n", - " # https://github.com/torvalds/linux/commit/7cf91a98e607c2f935dbcc177d70011e95b8faff\n", - " import os\n", - " use_hugepage = os.environ.get(\"NUMPY_MADVISE_HUGEPAGE\", None)\n", - " if sys.platform == \"linux\" and use_hugepage is None:\n", - " # If there is an issue with parsing the kernel version,\n", - " # set use_hugepages to 0. Usage of LooseVersion will handle\n", - " # the kernel version parsing better, but avoided since it\n", - " # will increase the import time. See: #16679 for related discussion.\n", - " try:\n", - " use_hugepage = 1\n", - " kernel_version = os.uname().release.split(\".\")[:2]\n", - " kernel_version = tuple(int(v) for v in kernel_version)\n", - " if kernel_version < (4, 6):\n", - " use_hugepage = 0\n", - " except ValueError:\n", - " use_hugepages = 0\n", - " elif use_hugepage is None:\n", - " # This is not Linux, so it should not matter, just enable anyway\n", - " use_hugepage = 1\n", - " else:\n", - " use_hugepage = int(use_hugepage)\n", - "\n", - " # Note that this will currently only make a difference on Linux\n", - " core.multiarray._set_madvise_hugepage(use_hugepage)\n", - " del use_hugepage\n", - "\n", - " # Give a warning if NumPy is reloaded or imported on a sub-interpreter\n", - " # We do this from python, since the C-module may not be reloaded and\n", - " # it is tidier organized.\n", - " core.multiarray._multiarray_umath._reload_guard()\n", - "\n", - " # default to \"weak\" promotion for \"NumPy 2\".\n", - " core._set_promotion_state(\n", - " os.environ.get(\"NPY_PROMOTION_STATE\",\n", - " \"weak\" if _using_numpy2_behavior() else \"legacy\"))\n", - "\n", - " # Tell PyInstaller where to find hook-numpy.py\n", - " def _pyinstaller_hooks_dir():\n", - " from pathlib import Path\n", - " return [str(Path(__file__).with_name(\"_pyinstaller\").resolve())]\n", - "\n", - " # Remove symbols imported for internal use\n", - " del os\n", - "\n", - "\n", - "# Remove symbols imported for internal use\n", - "del sys, warnings\n", - "\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import numpy as np\n", "import inspect\n", @@ -1560,17 +770,9 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "import math\n", "import inspect\n", @@ -1604,19 +806,9 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "callable(print) = True\n", - "callable(len) = True\n", - "callable(any) = True\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{callable(print) = }\")\n", "print(f\"{callable(len) = }\")\n", @@ -1632,18 +824,9 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "callable(str.upper) = True\n", - "callable(list.append) = True\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{callable(str.upper) = }\")\n", "print(f\"{callable(list.append) = }\")" @@ -1658,18 +841,9 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "callable(add) = True\n", - "callable(mul) = True\n" - ] - } - ], + "outputs": [], "source": [ "def add(num1, num2):\n", " return num1 + num2\n", @@ -1692,20 +866,9 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "callable(Counter) = True\n", - "callable(Counter.current) = True\n", - "callable(counter.current) = True\n", - "callable(counter) = True\n" - ] - } - ], + "outputs": [], "source": [ "class Counter:\n", " def __init__(self):\n", @@ -1736,20 +899,9 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def square():\n", " for i in range(10):\n", @@ -1772,7 +924,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1790,7 +942,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1805,7 +957,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1822,37 +974,18 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hex(id(add)) = '0x19624a1ea20'\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{hex(id(add)) = }\")" ] }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add(2, 3)" ] @@ -1866,7 +999,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1875,35 +1008,18 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hex(id(add)) = '0x1962477fc40'\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{hex(id(add)) = }\")" ] }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "add.__code__.co_freevars = ('func',)\n", - "add.__closure__ = (,)\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{add.__code__.co_freevars = }\")\n", "print(f\"{add.__closure__ = }\")" @@ -1918,34 +1034,16 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Caling add with args = (2, 3) and kwargs = {}\n" - ] - }, - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add(2, 3)" ] }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1956,7 +1054,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1965,18 +1063,9 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Caling gravitational_force with args = (5.972e+24, 7.3477e+22) and kwargs = {'distance': 384400000.0}\n", - "The gravitational force between the Earth and the Moon is 1.98e+20 Newtons.\n" - ] - } - ], + "outputs": [], "source": [ "mass1 = 5.972e24 # Mass of the Earth in kilograms\n", "mass2 = 7.3477e22 # Mass of the Moon in kilograms\n", @@ -1988,7 +1077,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2005,27 +1094,9 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Caling add with args = (2, 3) and kwargs = {}\n" - ] - }, - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add(2, 3)" ] @@ -2053,47 +1124,25 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'call'" - ] - }, - "execution_count": 69, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add.__name__ # name should have been 'add'" ] }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function call in module __main__:\n", - "\n", - "call(*args, **kwargs)\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(add) # docstring as well as original function signature is also lost" ] }, { "cell_type": "code", - "execution_count": 71, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2117,7 +1166,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2136,7 +1185,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2147,40 +1196,18 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'add'" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add.__name__" ] }, { "cell_type": "code", - "execution_count": 75, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function add in module __main__:\n", - "\n", - "add(num1, num2)\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(add)" ] @@ -2196,21 +1223,9 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n" - ] - } - ], + "outputs": [], "source": [ "def run_multiple_times(func, num_times):\n", " def wrapper(*args, **kwargs):\n", @@ -2238,25 +1253,13 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "run_multiple_times() missing 1 required positional argument: 'num_times'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\05_other_functions_concepts.ipynb Cell 116\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m \u001b[39m@run_multiple_times\u001b[39m(\u001b[39m5\u001b[39;49m)\n\u001b[0;32m 2\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrun_s3_cycle\u001b[39m():\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mRunning S3 cycle\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 6\u001b[0m run_s3_cycle()\n", - "\u001b[1;31mTypeError\u001b[0m: run_multiple_times() missing 1 required positional argument: 'num_times'" - ] - } - ], + "outputs": [], "source": [ "@run_multiple_times(5)\n", "def run_s3_cycle():\n", @@ -2268,21 +1271,9 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "run_multiple_times() missing 1 required positional argument: 'num_times'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\05_other_functions_concepts.ipynb Cell 117\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m decorator \u001b[39m=\u001b[39m run_multiple_times(\u001b[39m5\u001b[39;49m) \u001b[39m# This should return the run_multiple_times with parameter num_times parameter set to 5\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39m@decorator\u001b[39m\n\u001b[0;32m 4\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrun_s3_cycle\u001b[39m():\n\u001b[0;32m 5\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mRunning S3 cycle\u001b[39m\u001b[39m\"\u001b[39m)\n", - "\u001b[1;31mTypeError\u001b[0m: run_multiple_times() missing 1 required positional argument: 'num_times'" - ] - } - ], + "outputs": [], "source": [ "decorator = run_multiple_times(5) # This should return the run_multiple_times with parameter num_times parameter set to 5\n", "\n", @@ -2293,7 +1284,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2315,21 +1306,9 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n" - ] - } - ], + "outputs": [], "source": [ "def run_s3_cycle():\n", " print(\"Running S3 cycle\")\n", @@ -2348,21 +1327,9 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n", - "Running S3 cycle\n" - ] - } - ], + "outputs": [], "source": [ "@run_multiple_times(5)\n", "def run_s3_cycle():\n", @@ -2388,7 +1355,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2403,22 +1370,9 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Doing some computation for num1 = 3 and num2 =5\n", - "some_computational_task(3, 5) = 8\n", - "Doing some computation for num1 = 3 and num2 =5\n", - "some_computational_task(3, 5) = 8\n", - "Doing some computation for num1 = 3 and num2 =5\n", - "some_computational_task(3, 5) = 8\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{some_computational_task(3, 5) = }\")\n", "print(f\"{some_computational_task(3, 5) = }\")\n", @@ -2427,7 +1381,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2451,7 +1405,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -2464,20 +1418,9 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Doing some computation for num1 = 3 and num2 =5\n", - "some_computational_task(3, 5) = 8\n", - "some_computational_task(3, 5) = 8\n", - "some_computational_task(3, 5) = 8\n" - ] - } - ], + "outputs": [], "source": [ "print(f\"{some_computational_task(3, 5) = }\")\n", "print(f\"{some_computational_task(3, 5) = }\")\n", diff --git a/content/06_sequence_iterators_and_generators.ipynb b/content/06_sequence_iterators_and_generators.ipynb index d021498..97dfca3 100644 --- a/content/06_sequence_iterators_and_generators.ipynb +++ b/content/06_sequence_iterators_and_generators.ipynb @@ -35,18 +35,9 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 in numbers = True\n", - "10 not in numbers = True\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5]\n", "\n", @@ -63,17 +54,9 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers1 = [1, 2, 3, 4, 5]\n", "numbers2 = [6, 7, 8, 9, 10]\n", @@ -90,17 +73,9 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers1 = [1, 2, 3, 4, 5]\n", "\n", @@ -116,19 +91,9 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "len(numbers) = 5\n", - "min(numbers) = 1\n", - "max(numbers) = 5\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers1 = [1, 2, 3, 4, 5]\n", "\n", @@ -146,19 +111,9 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Index of first 1: numbers.index(1) = 0\n", - "Index of first 1, at or after index 1: numbers.index(1, 1) = 2\n", - "Index of first 3, at or after index 4 and before 12: numbers.index(3, 4, 12) = 8\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers = [1, 2, 1, 3, 5, 7, 2, 9, 3, 7, 3, 4, 5]\n", "\n", @@ -178,20 +133,9 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "numbers[2] = 1\n", - "numbers[2:10] = [1, 3, 5, 7, 2, 9, 3, 7]\n", - "numbers[2:10:3] = [1, 7, 3]\n", - "numbers[-1] = 5\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers = [1, 2, 1, 3, 5, 7, 2, 9, 3, 7, 3, 4, 5]\n", "\n", @@ -219,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -230,68 +174,31 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(type(number_iterator))" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(number_iterator)" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n" - ] - }, - { - "ename": "StopIteration", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\06_sequence_iterators_and_generators.ipynb Cell 20\u001b[0m line \u001b[0;36m4\n\u001b[0;32m 2\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mnext\u001b[39m(number_iterator))\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mnext\u001b[39m(number_iterator))\n\u001b[1;32m----> 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mnext\u001b[39;49m(number_iterator))\n", - "\u001b[1;31mStopIteration\u001b[0m: " - ] - } - ], + "outputs": [], "source": [ "print(next(number_iterator))\n", "print(next(number_iterator))\n", @@ -301,19 +208,9 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers = [1, 2, 3]\n", "\n", @@ -332,19 +229,9 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "number_iterator = iter(numbers)\n", "\n", @@ -374,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -399,21 +286,9 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "2\n", - "4\n", - "6\n", - "8\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "for item in MyRangeIterator(0, 10, 2):\n", " print(item)" @@ -430,7 +305,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -448,28 +323,9 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "4\n", - "9\n", - "16\n", - "25\n", - "36\n", - "49\n", - "64\n", - "81\n", - "100\n", - "121\n", - "144\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "square_iterator = SquareIterator()\n", "\n", @@ -504,7 +360,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -517,37 +373,18 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(type(square_generator()))" ] }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "4\n", - "9\n", - "16\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "sq = square_generator()\n", "print(next(sq))\n", @@ -558,28 +395,9 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "4\n", - "9\n", - "16\n", - "25\n", - "36\n", - "49\n", - "64\n", - "81\n", - "100\n", - "121\n", - "144\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "for number in square_generator():\n", " if number > 150:\n", @@ -609,31 +427,9 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Dataset 1 - Data point 1\n", - "Dataset 1 - Data point 2\n", - "Dataset 1 - Data point 3\n", - "Dataset 1 - Data point 4\n", - "Dataset 1 - Data point 5\n", - "Dataset 2 - Data point 6\n", - "Dataset 2 - Data point 7\n", - "Dataset 2 - Data point 8\n", - "Dataset 2 - Data point 9\n", - "Dataset 2 - Data point 10\n", - "Dataset 3 - Data point 11\n", - "Dataset 3 - Data point 12\n", - "Dataset 3 - Data point 13\n", - "Dataset 3 - Data point 14\n", - "Dataset 3 - Data point 15\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "def dataset1():\n", " for i in range(1, 6):\n", @@ -670,18 +466,9 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Memory usage for infinite square generator: 192 bytes\n", - "Memory usage for infinite square iterator: 56 bytes\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import sys\n", "\n", @@ -704,17 +491,9 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "67.6 ns ± 1.61 ns per loop (mean ± std. dev. of 5 runs, 100,000 loops each)\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "gen = square_generator()\n", "%timeit -n 100000 -r 5 next(gen)" @@ -722,17 +501,9 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "88.2 ns ± 1.41 ns per loop (mean ± std. dev. of 5 runs, 100,000 loops each)\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "it = SquareIterator()\n", "%timeit -n 100000 -r 5 next(it)" @@ -763,17 +534,9 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Total Sales: 650\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "daily_sales = [100, 150, 120, 80, 200]\n", "total_sales = sum(daily_sales)\n", @@ -790,17 +553,9 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Highest Temperature: 30\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "weekly_temperatures = [24, 27, 25, 28, 26, 22, 30]\n", "highest_temperature = max(weekly_temperatures)\n", @@ -817,17 +572,9 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Lowest Score: 68\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "quiz_scores = [85, 72, 90, 68, 78]\n", "lowest_score = min(quiz_scores)\n", @@ -844,17 +591,9 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Has Overtime: True\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "overtime_hours = [0, 0, 0, 3, 1, 0]\n", "has_overtime = any(overtime_hours)\n", @@ -871,17 +610,9 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "All Passed: False\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "exam_results = [True, True, False, True, True]\n", "all_passed = all(exam_results)\n", @@ -898,17 +629,9 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Number of Students: 5\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "students = [\"Alice\", \"Bob\", \"Charlie\", \"David\", \"Eva\"]\n", "num_students = len(students)\n", @@ -933,17 +656,9 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Year: 2023, Month: 07, Day: 15\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "date_str = \"2023-07-15\"\n", "year_slice = slice(0, 4)\n", @@ -966,25 +681,13 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'slice' object is not iterable", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\06_sequence_iterators_and_generators.ipynb Cell 61\u001b[0m line \u001b[0;36m4\n\u001b[0;32m 1\u001b[0m numbers \u001b[39m=\u001b[39m SquareIterator()\n\u001b[0;32m 2\u001b[0m first_5_square \u001b[39m=\u001b[39m \u001b[39mslice\u001b[39m(numbers, \u001b[39m5\u001b[39m)\n\u001b[1;32m----> 4\u001b[0m \u001b[39mfor\u001b[39;49;00m num \u001b[39min\u001b[39;49;00m first_5_square:\n\u001b[0;32m 5\u001b[0m \u001b[39mprint\u001b[39;49m(num)\n", - "\u001b[1;31mTypeError\u001b[0m: 'slice' object is not iterable" - ] - } - ], + "outputs": [], "source": [ "numbers = SquareIterator()\n", "first_5_square = slice(numbers, 5)\n", @@ -1003,27 +706,9 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "4\n", - "9\n", - "16\n", - "25\n", - "********************\n", - "36\n", - "49\n", - "64\n", - "81\n", - "100\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import islice\n", "\n", @@ -1060,21 +745,9 @@ }, { "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "4\n", - "6\n", - "8\n", - "10\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "\n", @@ -1097,19 +770,9 @@ }, { "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "banana\n", - "orange\n", - "watermelon\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import filterfalse\n", "\n", @@ -1129,19 +792,9 @@ }, { "cell_type": "code", - "execution_count": 41, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "90\n", - "92\n", - "95\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from functools import partial\n", "\n", @@ -1167,19 +820,9 @@ }, { "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import takewhile\n", "\n", @@ -1203,19 +846,9 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n", - "5\n", - "6\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import dropwhile\n", "\n", @@ -1239,19 +872,9 @@ }, { "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "3\n", - "5\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import compress\n", "\n", @@ -1280,20 +903,9 @@ }, { "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Item: apple, ID: 1\n", - "Item: banana, ID: 2\n", - "Item: orange, ID: 3\n", - "Item: grape, ID: 4\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import itertools\n", "\n", @@ -1321,24 +933,9 @@ }, { "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Task: Task A, Assigned to: John\n", - "Task: Task B, Assigned to: Alice\n", - "Task: Task C, Assigned to: Bob\n", - "Task: Task D, Assigned to: John\n", - "Task: Task E, Assigned to: Alice\n", - "Task: Task F, Assigned to: Bob\n", - "Task: Task G, Assigned to: John\n", - "Task: Task H, Assigned to: Alice\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import itertools\n", "\n", @@ -1367,19 +964,9 @@ }, { "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, World!\n", - "Hello, World!\n", - "Hello, World!\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import itertools\n", "\n", @@ -1407,7 +994,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1422,17 +1009,9 @@ }, { "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('Apple', 370), ('Banana', 240), ('Orange', 600), ('Grapes', 210)]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Using map() to calculate the total sales for each product\n", "total_sales_map = map(\n", @@ -1450,17 +1029,9 @@ }, { "cell_type": "code", - "execution_count": 56, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('Apple', 370), ('Banana', 240), ('Orange', 600), ('Grapes', 210)]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import starmap\n", "from functools import reduce\n", @@ -1480,17 +1051,9 @@ }, { "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1420\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from functools import reduce\n", "\n", @@ -1504,17 +1067,9 @@ }, { "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 3, 6, 10, 15]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import accumulate\n", "\n", @@ -1542,17 +1097,9 @@ }, { "cell_type": "code", - "execution_count": 58, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import chain\n", "\n", @@ -1574,17 +1121,9 @@ }, { "cell_type": "code", - "execution_count": 59, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import chain\n", "\n", @@ -1604,26 +1143,9 @@ }, { "cell_type": "code", - "execution_count": 60, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Iterator 1: 1\n", - "Iterator 1: 2\n", - "Iterator 1: 3\n", - "Iterator 1: 4\n", - "Iterator 1: 5\n", - "Iterator 2: 1\n", - "Iterator 2: 2\n", - "Iterator 2: 3\n", - "Iterator 2: 4\n", - "Iterator 2: 5\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import tee\n", "\n", @@ -1659,20 +1181,9 @@ }, { "cell_type": "code", - "execution_count": 61, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Students with grade A: ['Bob', 'Frank']\n", - "Students with grade B: ['Alice']\n", - "Students with grade C: ['Charlie', 'Eva']\n", - "Students with grade D: ['David']\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import groupby\n", "\n", @@ -1719,17 +1230,9 @@ }, { "cell_type": "code", - "execution_count": 62, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "defaultdict(, {'Lorem': 2, 'ipsum': 2, 'dolor': 1, 'sit': 1, 'amet': 1, 'consectetur': 1, 'adipiscing': 1, 'elit': 1, 'ipsum.': 1})\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from collections import defaultdict\n", "\n", @@ -1760,22 +1263,9 @@ }, { "cell_type": "code", - "execution_count": 63, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "abc\n", - "acb\n", - "bac\n", - "bca\n", - "cab\n", - "cba\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import permutations\n", "\n", @@ -1795,19 +1285,9 @@ }, { "cell_type": "code", - "execution_count": 64, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('red', 'green')\n", - "('red', 'blue')\n", - "('green', 'blue')\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import combinations\n", "\n", @@ -1827,20 +1307,9 @@ }, { "cell_type": "code", - "execution_count": 65, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 'A')\n", - "(1, 'B')\n", - "(2, 'A')\n", - "(2, 'B')\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from itertools import product\n", "\n", diff --git a/content/07_context_managers.ipynb b/content/07_context_managers.ipynb index cff8878..b5eac11 100644 --- a/content/07_context_managers.ipynb +++ b/content/07_context_managers.ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -46,103 +46,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
Hello there\n",
-       "
\n" - ], - "text/latex": [ - "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", - "Hello there\n", - "\\end{Verbatim}\n" - ], - "text/plain": [ - "Hello there" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from IPython.display import Code\n", "\n", @@ -151,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -162,103 +68,9 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
Hello there\n",
-       "
\n" - ], - "text/latex": [ - "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", - "Hello there\n", - "\\end{Verbatim}\n" - ], - "text/plain": [ - "Hello there" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "Code(filename=\"static/file1.txt\")" ] @@ -276,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -287,213 +99,25 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
Hello from file1\n",
-       "
\n" - ], - "text/latex": [ - "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", - "Hello from file1\n", - "\\end{Verbatim}\n" - ], - "text/plain": [ - "Hello from file1" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "Code(filename=\"static/file1.txt\")" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
Hello from file2\n",
-       "
\n" - ], - "text/latex": [ - "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", - "Hello from file2\n", - "\\end{Verbatim}\n" - ], - "text/plain": [ - "Hello from file2" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "Code(filename=\"static/file2.txt\")" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -512,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -531,20 +155,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "inside __init__\n", - "inside __enter__\n", - "x = 1, y = 2, z = 3\n", - "inside __exit__\n" - ] - } - ], + "outputs": [], "source": [ "with CtxMng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")" @@ -566,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -584,17 +197,9 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "

some text

" - ] - } - ], + "outputs": [], "source": [ "with HTMLTag(\"p\"):\n", " print(\"some text\", end=\"\")" @@ -602,17 +207,9 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "

some bold text

" - ] - } - ], + "outputs": [], "source": [ "with HTMLTag(\"p\"):\n", " print(\"some \", end=\"\")\n", @@ -637,7 +234,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -663,21 +260,9 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "inside __init__\n", - "inside __enter__\n", - "x = 1, y = 2, z = 3\n", - "inside __exit__\n", - "Not suppressing exc_value = None\n" - ] - } - ], + "outputs": [], "source": [ "with CtxMng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")" @@ -685,21 +270,9 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "inside __init__\n", - "inside __enter__\n", - "x = 1, y = 2, z = 3\n", - "inside __exit__\n", - "Supressing exc_value = TypeError()\n" - ] - } - ], + "outputs": [], "source": [ "with CtxMng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")\n", @@ -708,36 +281,13 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "inside __init__\n", - "inside __enter__\n", - "x = 1, y = 2, z = 3\n", - "inside __exit__\n", - "Not suppressing exc_value = ValueError()\n" - ] - }, - { - "ename": "ValueError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\07_context_managers.ipynb Cell 25\u001b[0m line \u001b[0;36m3\n\u001b[0;32m 1\u001b[0m \u001b[39mwith\u001b[39;00m CtxMng() \u001b[39mas\u001b[39;00m (x, y, z):\n\u001b[0;32m 2\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mx\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m, \u001b[39m\u001b[39m{\u001b[39;00my\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m, \u001b[39m\u001b[39m{\u001b[39;00mz\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m----> 3\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m()\n", - "\u001b[1;31mValueError\u001b[0m: " - ] - } - ], + "outputs": [], "source": [ "with CtxMng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")\n", @@ -809,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -835,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -861,19 +411,9 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "before yield\n", - "x = 1, y = 2, z = 3\n", - "cleanup\n" - ] - } - ], + "outputs": [], "source": [ "with ctx_mng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")" @@ -881,20 +421,9 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "before yield\n", - "x = 1, y = 2, z = 3\n", - "Supressing exc_value = TypeError()\n", - "cleanup\n" - ] - } - ], + "outputs": [], "source": [ "with ctx_mng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")\n", @@ -903,34 +432,13 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "before yield\n", - "x = 1, y = 2, z = 3\n", - "cleanup\n" - ] - }, - { - "ename": "ValueError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\07_context_managers.ipynb Cell 32\u001b[0m line \u001b[0;36m3\n\u001b[0;32m 1\u001b[0m \u001b[39mwith\u001b[39;00m ctx_mng() \u001b[39mas\u001b[39;00m (x, y, z):\n\u001b[0;32m 2\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mx\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m, \u001b[39m\u001b[39m{\u001b[39;00my\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m, \u001b[39m\u001b[39m{\u001b[39;00mz\u001b[39m \u001b[39m\u001b[39m= }\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m----> 3\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m()\n", - "\u001b[1;31mValueError\u001b[0m: " - ] - } - ], + "outputs": [], "source": [ "with ctx_mng() as (x, y, z):\n", " print(f\"{x = }, {y = }, {z = }\")\n", @@ -946,20 +454,9 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "inside __init__\n", - "inside __enter__\n", - "inside __exit__\n", - "Supressing exc_value = TypeError(\"'int' object does not support the context manager protocol\")\n" - ] - } - ], + "outputs": [], "source": [ "with CtxMng() as x, y, z:\n", " print(f\"{x = }, {y = }, {z = }\")" @@ -967,20 +464,9 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "inside __init__\n", - "inside __enter__\n", - "inside __exit__\n", - "Supressing exc_value = TypeError(\"'int' object does not support the context manager protocol\")\n" - ] - } - ], + "outputs": [], "source": [ "with CtxMng() as x:\n", " with y:\n", @@ -1004,22 +490,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Before entering example os.getcwd() = 'c:\\\\Users\\\\bhmiller\\\\OneDrive - Intel Corporation\\\\Documents\\\\GitHub\\\\intermediate-python\\\\content'\n", - "After entering example os.getcwd() = 'c:\\\\Users\\\\bhmiller\\\\OneDrive - Intel Corporation\\\\Documents\\\\GitHub\\\\intermediate-python\\\\content\\\\example'\n", - "Do something here\n", - "Outside context manager os.getcwd() = 'c:\\\\Users\\\\bhmiller\\\\OneDrive - Intel Corporation\\\\Documents\\\\GitHub\\\\intermediate-python\\\\content'\n" - ] - } - ], + "outputs": [], "source": [ "from contextlib import contextmanager\n", "import os\n", @@ -1060,7 +535,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1085,7 +560,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1106,17 +581,9 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "C:\\Users\\bhmiller\\AppData\\Local\\Temp\\tmpj_sp_5tn\n" - ] - } - ], + "outputs": [], "source": [ "import tempfile\n", "\n", @@ -1133,17 +600,9 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Execution time: 2.0007057189941406 seconds\n" - ] - } - ], + "outputs": [], "source": [ "import time\n", "from contextlib import contextmanager\n", diff --git a/content/08_argparse.ipynb b/content/08_argparse.ipynb index 6c873af..8780609 100644 --- a/content/08_argparse.ipynb +++ b/content/08_argparse.ipynb @@ -116,53 +116,18 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "\n", - "positional arguments:\n", - " vm_name Name of virtual machine to be created\n", - "\n", - "options:\n", - " -h, --help show this help message and exit\n", - " -c CPU, --cpu CPU Number of cpu to use. Default to 4 cores\n", - " -m MEMORY, --memory MEMORY\n", - " Amount of memory to use in GB. Default to 8 GB\n", - " -v, --verbose Amount of verbosity in log.\n", - " -f, --force Force creation without confirmation.\n", - " -t TAGS, --tags TAGS Tags for the VM (can be specified multiple times).\n", - " --vm-type {linux,windows}\n", - " Type of VM (linux or windows).\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py --help" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "vm_cli.py: error: the following arguments are required: --vm-type\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1" ] @@ -204,93 +169,36 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "\n", - "positional arguments:\n", - " vm_name Name of virtual machine to be created\n", - "\n", - "options:\n", - " -h, --help show this help message and exit\n", - " -c CPU, --cpu CPU Number of cpu to use. Default to 4 cores\n", - " -m MEMORY, --memory MEMORY\n", - " Amount of memory to use in GB. Default to 8 GB\n", - " -v, --verbose Amount of verbosity in log.\n", - " -f, --force Force creation without confirmation.\n", - " -t TAGS, --tags TAGS Tags for the VM (can be specified multiple times).\n", - " --vm-type {linux,windows}\n", - " Type of VM (linux or windows).\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py --help" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "vm_cli.py: error: the following arguments are required: --vm-type\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "vm_cli.py: error: the following arguments are required: --vm-type\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 -c 32 -m 256" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "vm_cli.py: error: argument -c/--cpu: invalid int value: 'x'\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 -c x -m 256" ] @@ -344,135 +252,54 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'cpu': 4,\n", - " 'force': False,\n", - " 'memory': 8,\n", - " 'tags': [],\n", - " 'verbose': 0,\n", - " 'vm_name': 'vm1',\n", - " 'vm_type': 'windows'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 --vm-type windows" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "usage: vm_cli.py [-h] [-c CPU] [-m MEMORY] [-v] [-f] [-t TAGS] --vm-type\n", - " {linux,windows}\n", - " vm_name\n", - "vm_cli.py: error: argument --vm-type: invalid choice: 'osx' (choose from 'linux', 'windows')\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 --vm-type osx" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'cpu': 4,\n", - " 'force': False,\n", - " 'memory': 8,\n", - " 'tags': [],\n", - " 'verbose': 1,\n", - " 'vm_name': 'vm1',\n", - " 'vm_type': 'windows'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 --vm-type windows -v" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'cpu': 4,\n", - " 'force': False,\n", - " 'memory': 8,\n", - " 'tags': [],\n", - " 'verbose': 3,\n", - " 'vm_name': 'vm1',\n", - " 'vm_type': 'windows'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 --vm-type windows -vvv" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'cpu': 4,\n", - " 'force': True,\n", - " 'memory': 8,\n", - " 'tags': [],\n", - " 'verbose': 0,\n", - " 'vm_name': 'vm1',\n", - " 'vm_type': 'windows'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 --vm-type windows -f" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'cpu': 4,\n", - " 'force': False,\n", - " 'memory': 8,\n", - " 'tags': ['automation', 'windows', 'gen13'],\n", - " 'verbose': 0,\n", - " 'vm_name': 'vm1',\n", - " 'vm_type': 'windows'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/vm_cli.py vm1 --vm-type windows -t automation -t windows -t gen13" ] @@ -515,126 +342,54 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "usage: git_cli.py [-h] {add,commit} ...\n", - "\n", - "positional arguments:\n", - " {add,commit}\n", - " add git-add - Add file contents to the index\n", - " commit git-commit - Record changes to the repository\n", - "\n", - "options:\n", - " -h, --help show this help message and exit\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/git_cli.py --help" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "usage: git_cli.py add [-h] path\n", - "\n", - "positional arguments:\n", - " path Files or directory to add to add content from\n", - "\n", - "options:\n", - " -h, --help show this help message and exit\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/git_cli.py add --help" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "usage: git_cli.py commit [-h] [-a] [-m MESSAGE]\n", - "\n", - "options:\n", - " -h, --help show this help message and exit\n", - " -a, --all Tell the command to automatically stage files that\n", - " have been modified and deleted, but new files you have\n", - " not told Git about are not affected.\n", - " -m MESSAGE, --message MESSAGE\n", - " Use the given as the commit message. If multiple\n", - " -m options are given, their values are concatenated as\n", - " separate paragraphs.\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/git_cli.py commit --help" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'cmd': 'add', 'path': '/path1'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/git_cli.py add /path1" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'all': False, 'cmd': 'commit', 'message': 'Some message'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/git_cli.py commit -m \"Some message\"" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'all': True, 'cmd': 'commit', 'message': 'Some message'}\n" - ] - } - ], + "outputs": [], "source": [ "!python 08_argparse/git_cli.py commit -a -m \"Some message\"" ] diff --git a/content/09_unittest.ipynb b/content/09_unittest.ipynb index c8cb6ce..a8e6d03 100644 --- a/content/09_unittest.ipynb +++ b/content/09_unittest.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "6836536d-2b8b-4c11-8676-f94db5baa5d8", "metadata": {}, "outputs": [], @@ -28,38 +28,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "862b2eff-0469-4a3b-ab34-3e6066e55b3f", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/html": [ - "
F\n",
-       "======================================================================\n",
-       "FAIL: test_something (tmp.ExampleTest.test_something)\n",
-       "----------------------------------------------------------------------\n",
-       "Traceback (most recent call last):\n",
-       "  File \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\tmp.py\", line 6, in test_something\n",
-       "    self.assertEqual(val, 5)\n",
-       "AssertionError: 1 != 5\n",
-       "\n",
-       "----------------------------------------------------------------------\n",
-       "Ran 1 test in 0.001s\n",
-       "\n",
-       "FAILED (failures=1)\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic unittest\n", "import unittest\n", @@ -72,75 +46,10 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "e32280ad-2854-44fc-b6ba-2ff008b79a7a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Collecting pytest\n", - " Downloading pytest-7.4.3-py3-none-any.whl.metadata (7.9 kB)\n", - "Collecting iniconfig (from pytest)\n", - " Downloading iniconfig-2.0.0-py3-none-any.whl (5.9 kB)\n", - "Requirement already satisfied: packaging in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (23.2)\n", - "Collecting pluggy<2.0,>=0.12 (from pytest)\n", - " Downloading pluggy-1.3.0-py3-none-any.whl.metadata (4.3 kB)\n", - "Requirement already satisfied: colorama in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (0.4.6)\n", - "Downloading pytest-7.4.3-py3-none-any.whl (325 kB)\n", - " ---------------------------------------- 0.0/325.1 kB ? eta -:--:--\n", - " --- ----------------------------------- 30.7/325.1 kB 660.6 kB/s eta 0:00:01\n", - " --------------- ------------------------ 122.9/325.1 kB 1.4 MB/s eta 0:00:01\n", - " ---------------------------------- ----- 276.5/325.1 kB 2.4 MB/s eta 0:00:01\n", - " ---------------------------------------- 325.1/325.1 kB 2.2 MB/s eta 0:00:00\n", - "Downloading pluggy-1.3.0-py3-none-any.whl (18 kB)\n", - "Installing collected packages: pluggy, iniconfig, pytest\n", - "Successfully installed iniconfig-2.0.0 pluggy-1.3.0 pytest-7.4.3\n", - "Collecting pytest\n", - " Downloading pytest-7.4.3-py3-none-any.whl.metadata (7.9 kB)\n", - "Collecting iniconfig (from pytest)\n", - " Downloading iniconfig-2.0.0-py3-none-any.whl (5.9 kB)\n", - "Requirement already satisfied: packaging in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (23.2)\n", - "Collecting pluggy<2.0,>=0.12 (from pytest)\n", - " Downloading pluggy-1.3.0-py3-none-any.whl.metadata (4.3 kB)\n", - "Requirement already satisfied: colorama in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (0.4.6)\n", - "Downloading pytest-7.4.3-py3-none-any.whl (325 kB)\n", - " ---------------------------------------- 0.0/325.1 kB ? eta -:--:--\n", - " - -------------------------------------- 10.2/325.1 kB ? eta -:--:--\n", - " -------- ------------------------------ 71.7/325.1 kB 787.7 kB/s eta 0:00:01\n", - " --------------------------- ------------ 225.3/325.1 kB 2.0 MB/s eta 0:00:01\n", - " ---------------------------------------- 325.1/325.1 kB 2.0 MB/s eta 0:00:00\n", - "Downloading pluggy-1.3.0-py3-none-any.whl (18 kB)\n", - "Installing collected packages: pluggy, iniconfig, pytest\n", - "Successfully installed iniconfig-2.0.0 pluggy-1.3.0 pytest-7.4.3\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [WinError 10051] A socket operation was attempted to an unreachable network')': /simple/pytest/\n", - "WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 11004] getaddrinfo failed')': /simple/pytest/\n", - "WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 11004] getaddrinfo failed')': /simple/pytest/\n", - "WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [WinError 10051] A socket operation was attempted to an unreachable network')': /simple/pytest/\n", - "WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 11004] getaddrinfo failed')': /simple/pytest/\n", - "WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 11004] getaddrinfo failed')': /simple/pytest/\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: pytest in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (7.4.3)\n", - "Requirement already satisfied: iniconfig in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (2.0.0)\n", - "Requirement already satisfied: packaging in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (23.2)\n", - "Requirement already satisfied: pluggy<2.0,>=0.12 in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (1.3.0)\n", - "Requirement already satisfied: colorama in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from pytest) (0.4.6)\n", - "Note: you may need to restart the kernel to use updated packages.\n" - ] - } - ], + "outputs": [], "source": [ "%pip install pytest" ] @@ -155,42 +64,10 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "38fd0a98-c5d9-448e-bd00-e7fded8d9aee", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collected 1 item\n",
-       "\n",
-       "tmp.py F                                                                 [100%]\n",
-       "\n",
-       "================================== FAILURES ===================================\n",
-       "_______________________________ test_something ________________________________\n",
-       "\n",
-       "    def test_something():\n",
-       "        val = 1\n",
-       ">       assert val == 5\n",
-       "E       assert 1 == 5\n",
-       "\n",
-       "tmp.py:3: AssertionError\n",
-       "=========================== short test summary info ===========================\n",
-       "FAILED tmp.py::test_something - assert 1 == 5\n",
-       "============================== 1 failed in 0.10s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest\n", "def test_something():\n", @@ -222,42 +99,10 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "1132e4b0-ddd6-486a-bbf2-eb3315046d2a", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collected 1 item\n",
-       "\n",
-       "tmp.py F                                                                 [100%]\n",
-       "\n",
-       "================================== FAILURES ===================================\n",
-       "__________________________________ test_add ___________________________________\n",
-       "\n",
-       "    def test_add():\n",
-       ">       assert add(1, 2) == 3\n",
-       "E       assert None == 3\n",
-       "E        +  where None = add(1, 2)\n",
-       "\n",
-       "tmp.py:6: AssertionError\n",
-       "=========================== short test summary info ===========================\n",
-       "FAILED tmp.py::test_add - assert None == 3\n",
-       "============================== 1 failed in 0.07s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest\n", "def add(num1, num2):\n", @@ -270,31 +115,10 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "6965ab16-7184-4672-bbdc-b15ee11be477", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collected 1 item\n",
-       "\n",
-       "tmp.py .                                                                 [100%]\n",
-       "\n",
-       "============================== 1 passed in 0.01s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest\n", "def add(num1, num2):\n", @@ -315,42 +139,10 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "498cb615-4747-415a-b2e2-9ba20e2d6151", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collected 2 items\n",
-       "\n",
-       "tmp.py .F                                                                [100%]\n",
-       "\n",
-       "================================== FAILURES ===================================\n",
-       "_______________________________ test_add_float ________________________________\n",
-       "\n",
-       "    def test_add_float():\n",
-       ">       assert add(0.1, 0.2) == 0.3\n",
-       "E       assert 0.30000000000000004 == 0.3\n",
-       "E        +  where 0.30000000000000004 = add(0.1, 0.2)\n",
-       "\n",
-       "tmp.py:9: AssertionError\n",
-       "=========================== short test summary info ===========================\n",
-       "FAILED tmp.py::test_add_float - assert 0.30000000000000004 == 0.3\n",
-       "========================= 1 failed, 1 passed in 0.14s =========================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest\n", "def add(num1, num2):\n", @@ -366,31 +158,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "e8957e45-7538-42d7-b1d9-30c2e1d08368", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collected 2 items\n",
-       "\n",
-       "tmp.py ..                                                                [100%]\n",
-       "\n",
-       "============================== 2 passed in 0.02s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest\n", "def add(num1, num2):\n", @@ -415,31 +186,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "5e3735bb-d685-40ab-b1a6-179097ac44e3", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collected 2 items\n",
-       "\n",
-       "tmp.py ..                                                                [100%]\n",
-       "\n",
-       "============================== 2 passed in 0.01s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest\n", "def add(num1, num2):\n", @@ -462,33 +212,10 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "a0d62060-a4ab-4d26-bb73-7a4ed238a917", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_add[input_arg0-3] PASSED                                    [ 50%]\n",
-       "tmp.py::test_add[input_arg1-0.3] PASSED                                  [100%]\n",
-       "\n",
-       "============================== 2 passed in 0.02s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -v\n", "def add(num1, num2):\n", @@ -519,37 +246,10 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "252781d2-a153-42da-9dc7-9345aa9f3b98", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_add[input_arg0-3] \n",
-       "input_arg = (1, 2), expected = 3\n",
-       "PASSED\n",
-       "tmp.py::test_add[input_arg1-0.3] \n",
-       "input_arg = (0.1, 0.2), expected = 0.3\n",
-       "PASSED\n",
-       "\n",
-       "============================== 2 passed in 0.02s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -vs\n", "def add(num1, num2):\n", @@ -581,32 +281,10 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "0f0f4466-24a3-4e88-8961-0977722efd9a", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 1 item\n",
-       "\n",
-       "tmp.py::test_raises PASSED                                               [100%]\n",
-       "\n",
-       "============================== 1 passed in 0.00s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -v\n", "import pytest\n", @@ -640,33 +318,10 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "de096fb9-fba3-4a2d-ad88-3db4605212fa", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_addition PASSED                                             [ 50%]\n",
-       "tmp.py::test_subtraction PASSED                                          [100%]\n",
-       "\n",
-       "============================== 2 passed in 0.01s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -v\n", "class Calculator:\n", @@ -689,33 +344,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "041a810b-0398-48de-8cac-0efc1e1ba7c9", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_addition PASSED                                             [ 50%]\n",
-       "tmp.py::test_subtraction PASSED                                          [100%]\n",
-       "\n",
-       "============================== 2 passed in 0.01s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -v\n", "class Calculator:\n", @@ -750,39 +382,10 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "9e0fd8fa-1653-4d62-a319-87307db52a22", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_add Setup - Before the test\n",
-       "Running test_add\n",
-       "PASSEDTeardown - After the test\n",
-       "\n",
-       "tmp.py::test_sub Setup - Before the test\n",
-       "Running test_sub\n",
-       "PASSEDTeardown - After the test\n",
-       "\n",
-       "\n",
-       "============================== 2 passed in 0.01s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -vs\n", "import pytest\n", @@ -813,39 +416,10 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "f41c08b3-83c6-4129-9195-d75cc237ac2f", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_add Setup - Before the test\n",
-       "Running test_add\n",
-       "PASSEDTeardown - After the test\n",
-       "\n",
-       "tmp.py::test_sub Setup - Before the test\n",
-       "Running test_sub\n",
-       "PASSEDTeardown - After the test\n",
-       "\n",
-       "\n",
-       "============================== 2 passed in 0.01s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -vs\n", "import pytest\n", @@ -875,37 +449,10 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "f7d77a4e-1866-4c4a-8296-ce14e3a00950", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
============================= test session starts =============================\n",
-       "platform win32 -- Python 3.11.6, pytest-7.4.3, pluggy-1.3.0 -- C:\\Users\\bhmiller\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe\n",
-       "cachedir: .pytest_cache\n",
-       "rootdir: c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\n",
-       "collecting ... collected 2 items\n",
-       "\n",
-       "tmp.py::test_add Setup - Before the test\n",
-       "Running test_add\n",
-       "PASSED\n",
-       "tmp.py::test_sub Running test_sub\n",
-       "PASSEDTeardown - After the test\n",
-       "\n",
-       "\n",
-       "============================== 2 passed in 0.02s ==============================\n",
-       "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "%%save_and_run_magic pytest -vs\n", "import pytest\n", diff --git a/content/10_concurrency.ipynb b/content/10_concurrency.ipynb index 555ee61..88b581f 100644 --- a/content/10_concurrency.ipynb +++ b/content/10_concurrency.ipynb @@ -37,48 +37,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "bd4ab344-0029-451f-8c26-fa2b5499f935", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: viztracer in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (0.16.1)\n", - "Requirement already satisfied: objprint>0.1.3 in c:\\users\\bhmiller\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from viztracer) (0.2.3)\n", - "Note: you may need to restart the kernel to use updated packages.\n", - "Collecting requests\n", - " Downloading requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)\n", - "Collecting charset-normalizer<4,>=2 (from requests)\n", - " Downloading charset_normalizer-3.3.2-cp311-cp311-win_amd64.whl.metadata (34 kB)\n", - "Collecting idna<4,>=2.5 (from requests)\n", - " Downloading idna-3.6-py3-none-any.whl.metadata (9.9 kB)\n", - "Collecting urllib3<3,>=1.21.1 (from requests)\n", - " Downloading urllib3-2.1.0-py3-none-any.whl.metadata (6.4 kB)\n", - "Collecting certifi>=2017.4.17 (from requests)\n", - " Downloading certifi-2023.11.17-py3-none-any.whl.metadata (2.2 kB)\n", - "Downloading requests-2.31.0-py3-none-any.whl (62 kB)\n", - " ---------------------------------------- 0.0/62.6 kB ? eta -:--:--\n", - " ---------------------------------------- 62.6/62.6 kB 1.7 MB/s eta 0:00:00\n", - "Downloading certifi-2023.11.17-py3-none-any.whl (162 kB)\n", - " ---------------------------------------- 0.0/162.5 kB ? eta -:--:--\n", - " ---------------------------------------- 162.5/162.5 kB 4.9 MB/s eta 0:00:00\n", - "Downloading charset_normalizer-3.3.2-cp311-cp311-win_amd64.whl (99 kB)\n", - " ---------------------------------------- 0.0/99.9 kB ? eta -:--:--\n", - " ---------------------------------------- 99.9/99.9 kB ? eta 0:00:00\n", - "Downloading idna-3.6-py3-none-any.whl (61 kB)\n", - " ---------------------------------------- 0.0/61.6 kB ? eta -:--:--\n", - " ---------------------------------------- 61.6/61.6 kB 3.2 MB/s eta 0:00:00\n", - "Downloading urllib3-2.1.0-py3-none-any.whl (104 kB)\n", - " ---------------------------------------- 0.0/104.6 kB ? eta -:--:--\n", - " ---------------------------------------- 104.6/104.6 kB ? eta 0:00:00\n", - "Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests\n", - "Successfully installed certifi-2023.11.17 charset-normalizer-3.3.2 idna-3.6 requests-2.31.0 urllib3-2.1.0\n", - "Note: you may need to restart the kernel to use updated packages.\n" - ] - } - ], + "outputs": [], "source": [ "%pip install viztracer\n", "%pip install requests" @@ -103,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "da7c7a00-ee14-4fcd-bc1e-80dea07dcf3f", "metadata": {}, "outputs": [], @@ -113,18 +75,10 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "663372ae-735e-4d91-b268-89f53146545f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting example/downloader.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/downloader.py\n", "import shutil\n", @@ -152,18 +106,10 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "faf8dc35-9da9-4e99-8803-afcd5e5273e9", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting example/single_core_single_process.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/single_core_single_process.py\n", "from downloader import download_file\n", @@ -193,45 +139,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "650c5547-7e34-49dc-9ecf-d6b181c2cda9", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Downloading itr1_english.pdf...\n", - "Download complete for itr1_english.pdf!\n", - "Downloading itr2_english.pdf...\n", - "Download complete for itr2_english.pdf!\n", - "Downloading itr3_english.pdf...\n", - "Download complete for itr3_english.pdf!\n", - "Downloading itr4_english.pdf...\n", - "Download complete for itr4_english.pdf!\n", - "Downloading itr5_english.pdf...\n", - "Download complete for itr5_english.pdf!\n", - "Downloading itr6_english.pdf...\n", - "Download complete for itr6_english.pdf!\n", - "Downloading itr7_english.pdf...\n", - "Download complete for itr7_english.pdf!\n", - "All downloads completed!\n", - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/1\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 153212\n", - " \n", - "Total Entries: 153212\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer example/single_core_single_process.py" ] @@ -249,18 +160,10 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "2bb3d508-450a-4508-9d3e-d7278097ebd2", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing example/single_core_multi_thread.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/single_core_multi_thread.py\n", "import threading\n", @@ -299,45 +202,10 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "40d1b34f-d586-432b-afbf-e7d5359aade9", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Downloading itr3_english.pdf...\n", - "Download complete for itr3_english.pdf!\n", - "Downloading itr6_english.pdf...\n", - "Downloading itr5_english.pdf...\n", - "Downloading itr4_english.pdf...\n", - "Download complete for itr6_english.pdf!\n", - "Downloading itr2_english.pdf...\n", - "Download complete for itr5_english.pdf!Downloading itr7_english.pdf...\n", - "Download complete for itr4_english.pdf!\n", - "Downloading itr1_english.pdf...\n", - "\n", - "Download complete for itr2_english.pdf!\n", - "Download complete for itr1_english.pdf!Download complete for itr7_english.pdf!\n", - "\n", - "All downloads completed!\n", - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/1\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 153730\n", - " \n", - "Total Entries: 153730\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer example/single_core_multi_thread.py" ] @@ -368,18 +236,10 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "912cb044-33e2-495a-8949-c7a03ba72e86", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing example/single_core_multi_thread_class_imp.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/single_core_multi_thread_class_imp.py\n", "import threading\n", @@ -444,59 +304,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "981d7dfc-6efb-466d-bd0f-ffda209f1586", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Starting Thread-1\n", - "Starting Thread-2\n", - "Starting Thread-3\n", - "Starting Thread-4\n", - "Starting Thread-5\n", - "Starting Thread-6\n", - "Starting Thread-7\n", - "Downloading itr3_english.pdf...\n", - "Download complete for itr3_english.pdf!\n", - "Completing Thread-3\n", - "Downloading itr2_english.pdf...\n", - "Download complete for itr2_english.pdf!\n", - "Completing Thread-2\n", - "Downloading itr5_english.pdf...Downloading itr4_english.pdf...\n", - "\n", - "Download complete for itr5_english.pdf!\n", - "Completing Thread-5\n", - "Download complete for itr4_english.pdf!\n", - "Completing Thread-4\n", - "Downloading itr6_english.pdf...\n", - "Download complete for itr6_english.pdf!\n", - "Completing Thread-6\n", - "Downloading itr7_english.pdf...Downloading itr1_english.pdf...\n", - "\n", - "Download complete for itr1_english.pdf!\n", - "Completing Thread-1Download complete for itr7_english.pdf!\n", - "\n", - "Completing Thread-7\n", - "All downloads completed!\n", - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/1\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 154785\n", - " \n", - "Total Entries: 154785\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer example/single_core_multi_thread_class_imp.py" ] @@ -511,18 +322,10 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "8a288c25-3c2d-4d68-a232-6878ee055614", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing example/single_core_multi_thread_cpu_bound.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/single_core_multi_thread_cpu_bound.py\n", "import threading\n", @@ -553,30 +356,10 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "d71b888e-34e3-45db-8ba8-ab1925bf402a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/1\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 29141\n", - " \n", - "Total Entries: 29141\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer example/single_core_multi_thread_cpu_bound.py" ] @@ -599,20 +382,10 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "74242817-0545-4c93-aac2-e3f25d32871d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "counter=10\n", - "counter=20\n", - "The final counter is 20\n" - ] - } - ], + "outputs": [], "source": [ "from threading import Thread\n", "from time import sleep\n", @@ -654,46 +427,10 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "037dba1e-c83d-49d9-a3ff-75710805eaf7", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 7 0 RESUME 0\n", - "\n", - " 11 2 LOAD_GLOBAL 0 (counter)\n", - " 14 STORE_FAST 1 (local_counter)\n", - "\n", - " 14 16 LOAD_FAST 1 (local_counter)\n", - " 18 LOAD_FAST 0 (by)\n", - " 20 BINARY_OP 13 (+=)\n", - " 24 STORE_FAST 1 (local_counter)\n", - "\n", - " 17 26 LOAD_GLOBAL 3 (NULL + sleep)\n", - " 38 LOAD_CONST 1 (0.1)\n", - " 40 PRECALL 1\n", - " 44 CALL 1\n", - " 54 POP_TOP\n", - "\n", - " 20 56 LOAD_FAST 1 (local_counter)\n", - " 58 STORE_GLOBAL 0 (counter)\n", - "\n", - " 21 60 LOAD_GLOBAL 5 (NULL + print)\n", - " 72 LOAD_CONST 2 ('counter=')\n", - " 74 LOAD_GLOBAL 0 (counter)\n", - " 86 FORMAT_VALUE 0\n", - " 88 BUILD_STRING 2\n", - " 90 PRECALL 1\n", - " 94 CALL 1\n", - " 104 POP_TOP\n", - " 106 LOAD_CONST 0 (None)\n", - " 108 RETURN_VALUE\n" - ] - } - ], + "outputs": [], "source": [ "import dis\n", "\n", @@ -732,20 +469,10 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "574aa7a5-89c0-4792-a0b0-05677bfa73e1", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "counter=10\n", - "counter=30\n", - "The final counter is 30\n" - ] - } - ], + "outputs": [], "source": [ "from threading import Thread, Lock\n", "from time import sleep\n", @@ -805,30 +532,10 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "bbc2441f-b5f3-48cf-ae8d-6040abba73c9", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Pinging www.google.com [142.250.69.196] with 32 bytes of data:\n", - "Reply from 142.250.69.196: bytes=32 time=17ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=14ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=15ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=15ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=15ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=15ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=15ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=18ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=13ms TTL=114\n", - "Reply from 142.250.69.196: bytes=32 time=14ms TTL=114\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "import subprocess\n", "import threading\n", @@ -880,26 +587,10 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "ddb2aed2-4a1c-4f53-b4de-92050ab8a8b7", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Produced: Item 1\n", - "Consumed: Item 1\n", - "Produced: Item 2Consumed: Item 2\n", - "\n", - "Produced: Item 3Consumed: Item 3\n", - "\n", - "Produced: Item 4Consumed: Item 4\n", - "\n", - "Producer and consumer have finished.\n" - ] - } - ], + "outputs": [], "source": [ "import threading\n", "import queue\n", @@ -965,320 +656,10 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "ab4b526a-f4ea-4cc3-b863-6749661c9e81", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Has been waiting for 1 second(s)...\n", - "Has been waiting for 2 second(s)...\n", - "Has been waiting for 3 second(s)...\n", - "Has been waiting for 4 second(s)...\n", - "Has been waiting for 5 second(s)...\n", - "Has been waiting for 6 second(s)...\n", - "Has been waiting for 7 second(s)...\n", - "Has been waiting for 8 second(s)...\n", - "Has been waiting for 9 second(s)...\n", - "Has been waiting for 10 second(s)...\n", - "Has been waiting for 11 second(s)...\n", - "Has been waiting for 12 second(s)...\n", - "Has been waiting for 13 second(s)...\n", - "Has been waiting for 14 second(s)...\n", - "Has been waiting for 15 second(s)...\n", - "Has been waiting for 16 second(s)...\n", - "Has been waiting for 17 second(s)...\n", - "Has been waiting for 18 second(s)...\n", - "Has been waiting for 19 second(s)...\n", - "Has been waiting for 20 second(s)...\n", - "Has been waiting for 21 second(s)...\n", - "Has been waiting for 22 second(s)...\n", - "Has been waiting for 23 second(s)...\n", - "Has been waiting for 24 second(s)...\n", - "Has been waiting for 25 second(s)...\n", - "Has been waiting for 26 second(s)...\n", - "Has been waiting for 27 second(s)...\n", - "Has been waiting for 28 second(s)...\n", - "Has been waiting for 29 second(s)...\n", - "Has been waiting for 30 second(s)...\n", - "Has been waiting for 31 second(s)...\n", - "Has been waiting for 32 second(s)...\n", - "Has been waiting for 33 second(s)...\n", - "Has been waiting for 34 second(s)...\n", - "Has been waiting for 35 second(s)...\n", - "Has been waiting for 36 second(s)...\n", - "Has been waiting for 37 second(s)...\n", - "Has been waiting for 38 second(s)...\n", - "Has been waiting for 39 second(s)...\n", - "Has been waiting for 40 second(s)...\n", - "Has been waiting for 41 second(s)...\n", - "Has been waiting for 42 second(s)...\n", - "Has been waiting for 43 second(s)...\n", - "Has been waiting for 44 second(s)...\n", - "Has been waiting for 45 second(s)...\n", - "Has been waiting for 46 second(s)...\n", - "Has been waiting for 47 second(s)...\n", - "Has been waiting for 48 second(s)...\n", - "Has been waiting for 49 second(s)...\n", - "Has been waiting for 50 second(s)...\n", - "Has been waiting for 51 second(s)...\n", - "Has been waiting for 52 second(s)...\n", - "Has been waiting for 53 second(s)...\n", - "Has been waiting for 54 second(s)...\n", - "Has been waiting for 55 second(s)...\n", - "Has been waiting for 56 second(s)...\n", - "Has been waiting for 57 second(s)...\n", - "Has been waiting for 58 second(s)...\n", - "Has been waiting for 59 second(s)...\n", - "Has been waiting for 60 second(s)...\n", - "Has been waiting for 61 second(s)...\n", - "Has been waiting for 62 second(s)...\n", - "Has been waiting for 63 second(s)...\n", - "Has been waiting for 64 second(s)...\n", - "Has been waiting for 65 second(s)...\n", - "Has been waiting for 66 second(s)...\n", - "Has been waiting for 67 second(s)...\n", - "Has been waiting for 68 second(s)...\n", - "Has been waiting for 69 second(s)...\n", - "Has been waiting for 70 second(s)...\n", - "Has been waiting for 71 second(s)...\n", - "Has been waiting for 72 second(s)...\n", - "Has been waiting for 73 second(s)...\n", - "Has been waiting for 74 second(s)...\n", - "Has been waiting for 75 second(s)...\n", - "Has been waiting for 76 second(s)...\n", - "Has been waiting for 77 second(s)...\n", - "Has been waiting for 78 second(s)...\n", - "Has been waiting for 79 second(s)...\n", - "Has been waiting for 80 second(s)...\n", - "Has been waiting for 81 second(s)...\n", - "Has been waiting for 82 second(s)...\n", - "Has been waiting for 83 second(s)...\n", - "Has been waiting for 84 second(s)...\n", - "Has been waiting for 85 second(s)...\n", - "Has been waiting for 86 second(s)...\n", - "Has been waiting for 87 second(s)...\n", - "Has been waiting for 88 second(s)...\n", - "Has been waiting for 89 second(s)...\n", - "Has been waiting for 90 second(s)...\n", - "Has been waiting for 91 second(s)...\n", - "Has been waiting for 92 second(s)...\n", - "Has been waiting for 93 second(s)...\n", - "Has been waiting for 94 second(s)...\n", - "Has been waiting for 95 second(s)...\n", - "Has been waiting for 96 second(s)...\n", - "Has been waiting for 97 second(s)...\n", - "Has been waiting for 98 second(s)...\n", - "Has been waiting for 99 second(s)...\n", - "Has been waiting for 100 second(s)...\n", - "Has been waiting for 101 second(s)...\n", - "Has been waiting for 102 second(s)...\n", - "Has been waiting for 103 second(s)...\n", - "Has been waiting for 104 second(s)...\n", - "Has been waiting for 105 second(s)...\n", - "Has been waiting for 106 second(s)...\n", - "Has been waiting for 107 second(s)...\n", - "Has been waiting for 108 second(s)...\n", - "Has been waiting for 109 second(s)...\n", - "Has been waiting for 110 second(s)...\n", - "Has been waiting for 111 second(s)...\n", - "Has been waiting for 112 second(s)...\n", - "Has been waiting for 113 second(s)...\n", - "Has been waiting for 114 second(s)...\n", - "Has been waiting for 115 second(s)...\n", - "Has been waiting for 116 second(s)...\n", - "Has been waiting for 117 second(s)...\n", - "Has been waiting for 118 second(s)...\n", - "Has been waiting for 119 second(s)...\n", - "Has been waiting for 120 second(s)...\n", - "Has been waiting for 121 second(s)...\n", - "Has been waiting for 122 second(s)...\n", - "Has been waiting for 123 second(s)...\n", - "Has been waiting for 124 second(s)...\n", - "Has been waiting for 125 second(s)...\n", - "Has been waiting for 126 second(s)...\n", - "Has been waiting for 127 second(s)...\n", - "Has been waiting for 128 second(s)...\n", - "Has been waiting for 129 second(s)...\n", - "Has been waiting for 130 second(s)...\n", - "Has been waiting for 131 second(s)...\n", - "Has been waiting for 132 second(s)...\n", - "Has been waiting for 133 second(s)...\n", - "Has been waiting for 134 second(s)...\n", - "Has been waiting for 135 second(s)...\n", - "Has been waiting for 136 second(s)...\n", - "Has been waiting for 137 second(s)...\n", - "Has been waiting for 138 second(s)...\n", - "Has been waiting for 139 second(s)...\n", - "Has been waiting for 140 second(s)...\n", - "Has been waiting for 141 second(s)...\n", - "Has been waiting for 142 second(s)...\n", - "Has been waiting for 143 second(s)...\n", - "Has been waiting for 144 second(s)...\n", - "Has been waiting for 145 second(s)...\n", - "Has been waiting for 146 second(s)...\n", - "Has been waiting for 147 second(s)...\n", - "Has been waiting for 148 second(s)...\n", - "Has been waiting for 149 second(s)...\n", - "Has been waiting for 150 second(s)...\n", - "Has been waiting for 151 second(s)...\n", - "Has been waiting for 152 second(s)...\n", - "Has been waiting for 153 second(s)...\n", - "Has been waiting for 154 second(s)...\n", - "Has been waiting for 155 second(s)...\n", - "Has been waiting for 156 second(s)...\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Has been waiting for 157 second(s)...\n", - "Has been waiting for 158 second(s)...\n", - "Has been waiting for 159 second(s)...\n", - "Has been waiting for 160 second(s)...\n", - "Has been waiting for 161 second(s)...\n", - "Has been waiting for 162 second(s)...\n", - "Has been waiting for 163 second(s)...\n", - "Has been waiting for 164 second(s)...\n", - "Has been waiting for 165 second(s)...\n", - "Has been waiting for 166 second(s)...\n", - "Has been waiting for 167 second(s)...\n", - "Has been waiting for 168 second(s)...\n", - "Has been waiting for 169 second(s)...\n", - "Has been waiting for 170 second(s)...\n", - "Has been waiting for 171 second(s)...\n", - "Has been waiting for 172 second(s)...\n", - "Has been waiting for 173 second(s)...\n", - "Has been waiting for 174 second(s)...\n", - "Has been waiting for 175 second(s)...\n", - "Has been waiting for 176 second(s)...\n", - "Has been waiting for 177 second(s)...\n", - "Has been waiting for 178 second(s)...\n", - "Has been waiting for 179 second(s)...\n", - "Has been waiting for 180 second(s)...\n", - "Has been waiting for 181 second(s)...\n", - "Has been waiting for 182 second(s)...\n", - "Has been waiting for 183 second(s)...\n", - "Has been waiting for 184 second(s)...\n", - "Has been waiting for 185 second(s)...\n", - "Has been waiting for 186 second(s)...\n", - "Has been waiting for 187 second(s)...\n", - "Has been waiting for 188 second(s)...\n", - "Has been waiting for 189 second(s)...\n", - "Has been waiting for 190 second(s)...\n", - "Has been waiting for 191 second(s)...\n", - "Has been waiting for 192 second(s)...\n", - "Has been waiting for 193 second(s)...\n", - "Has been waiting for 194 second(s)...\n", - "Has been waiting for 195 second(s)...\n", - "Has been waiting for 196 second(s)...\n", - "Has been waiting for 197 second(s)...\n", - "Has been waiting for 198 second(s)...\n", - "Has been waiting for 199 second(s)...\n", - "Has been waiting for 200 second(s)...\n", - "Has been waiting for 201 second(s)...\n", - "Has been waiting for 202 second(s)...\n", - "Has been waiting for 203 second(s)...\n", - "Has been waiting for 204 second(s)...\n", - "Has been waiting for 205 second(s)...\n", - "Has been waiting for 206 second(s)...\n", - "Has been waiting for 207 second(s)...\n", - "Has been waiting for 208 second(s)...\n", - "Has been waiting for 209 second(s)...\n", - "Has been waiting for 210 second(s)...\n", - "Has been waiting for 211 second(s)...\n", - "Has been waiting for 212 second(s)...\n", - "Has been waiting for 213 second(s)...\n", - "Has been waiting for 214 second(s)...\n", - "Has been waiting for 215 second(s)...\n", - "Has been waiting for 216 second(s)...\n", - "Has been waiting for 217 second(s)...\n", - "Has been waiting for 218 second(s)...\n", - "Has been waiting for 219 second(s)...\n", - "Has been waiting for 220 second(s)...\n", - "Has been waiting for 221 second(s)...\n", - "Has been waiting for 222 second(s)...\n", - "Has been waiting for 223 second(s)...\n", - "Has been waiting for 224 second(s)...\n", - "Has been waiting for 225 second(s)...\n", - "Has been waiting for 226 second(s)...\n", - "Has been waiting for 227 second(s)...\n", - "Has been waiting for 228 second(s)...\n", - "Has been waiting for 229 second(s)...\n", - "Has been waiting for 230 second(s)...\n", - "Has been waiting for 231 second(s)...\n", - "Has been waiting for 232 second(s)...\n", - "Has been waiting for 233 second(s)...\n", - "Has been waiting for 234 second(s)...\n", - "Has been waiting for 235 second(s)...\n", - "Has been waiting for 236 second(s)...\n", - "Has been waiting for 237 second(s)...\n", - "Has been waiting for 238 second(s)...\n", - "Has been waiting for 239 second(s)...\n", - "Has been waiting for 240 second(s)...\n", - "Has been waiting for 241 second(s)...\n", - "Has been waiting for 242 second(s)...\n", - "Has been waiting for 243 second(s)...\n", - "Has been waiting for 244 second(s)...\n", - "Has been waiting for 245 second(s)...\n", - "Has been waiting for 246 second(s)...\n", - "Has been waiting for 247 second(s)...\n", - "Has been waiting for 248 second(s)...\n", - "Has been waiting for 249 second(s)...\n", - "Has been waiting for 250 second(s)...\n", - "Has been waiting for 251 second(s)...\n", - "Has been waiting for 252 second(s)...\n", - "Has been waiting for 253 second(s)...\n", - "Has been waiting for 254 second(s)...\n", - "Has been waiting for 255 second(s)...\n", - "Has been waiting for 256 second(s)...\n", - "Has been waiting for 257 second(s)...\n", - "Has been waiting for 258 second(s)...\n", - "Has been waiting for 259 second(s)...\n", - "Has been waiting for 260 second(s)...\n", - "Has been waiting for 261 second(s)...\n", - "Has been waiting for 262 second(s)...\n", - "Has been waiting for 263 second(s)...\n", - "Has been waiting for 264 second(s)...\n", - "Has been waiting for 265 second(s)...\n", - "Has been waiting for 266 second(s)...\n", - "Has been waiting for 267 second(s)...\n", - "Has been waiting for 268 second(s)...\n", - "Has been waiting for 269 second(s)...\n", - "Has been waiting for 270 second(s)...\n", - "Has been waiting for 271 second(s)...\n", - "Has been waiting for 272 second(s)...\n", - "Has been waiting for 273 second(s)...\n", - "Has been waiting for 274 second(s)...\n", - "Has been waiting for 275 second(s)...\n", - "Has been waiting for 276 second(s)...\n", - "Has been waiting for 277 second(s)...\n", - "Has been waiting for 278 second(s)...\n", - "Has been waiting for 279 second(s)...\n", - "Has been waiting for 280 second(s)...\n", - "Has been waiting for 281 second(s)...\n", - "Has been waiting for 282 second(s)...\n", - "Has been waiting for 283 second(s)...\n", - "Has been waiting for 284 second(s)...\n", - "Has been waiting for 285 second(s)...\n", - "Has been waiting for 286 second(s)...\n", - "Has been waiting for 287 second(s)...\n", - "Has been waiting for 288 second(s)...\n", - "Has been waiting for 289 second(s)...\n", - "Has been waiting for 290 second(s)...\n", - "Has been waiting for 291 second(s)...\n", - "Has been waiting for 292 second(s)...\n", - "Has been waiting for 293 second(s)...\n", - "Has been waiting for 294 second(s)...\n", - "Has been waiting for 295 second(s)...\n", - "Has been waiting for 296 second(s)...\n", - "Has been waiting for 297 second(s)...\n" - ] - } - ], + "outputs": [], "source": [ "import threading\n", "import time\n", @@ -1304,127 +685,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "ad264d27-a6dd-4fe9-bc4e-78248cb7497c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Has been waiting for 1 second(s)...\n", - "Has been waiting for 2 second(s)...\n", - "Has been waiting for 3 second(s)...\n", - "Has been waiting for 4 second(s)...\n", - "Has been waiting for 5 second(s)...\n", - "Has been waiting for 6 second(s)...\n", - "Has been waiting for 7 second(s)...\n" - ] - }, - { - "ename": "AttributeError", - "evalue": "'Thread' object has no attribute 'stop'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32mc:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\10_concurrency.ipynb Cell 38\u001b[0m line \u001b[0;36m1\n\u001b[0;32m 17\u001b[0m \u001b[39m# Wait for user input to exit\u001b[39;00m\n\u001b[0;32m 18\u001b[0m answer \u001b[39m=\u001b[39m \u001b[39minput\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mDo you want to exit? (Type \u001b[39m\u001b[39m'\u001b[39m\u001b[39myes\u001b[39m\u001b[39m'\u001b[39m\u001b[39m to exit)\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 19\u001b[0m timer_thread\u001b[39m.\u001b[39;49mstop()\n", - "\u001b[1;31mAttributeError\u001b[0m: 'Thread' object has no attribute 'stop'" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Has been waiting for 8 second(s)...\n", - "Has been waiting for 9 second(s)...\n", - "Has been waiting for 10 second(s)...\n", - "Has been waiting for 11 second(s)...\n", - "Has been waiting for 12 second(s)...\n", - "Has been waiting for 13 second(s)...\n", - "Has been waiting for 14 second(s)...\n", - "Has been waiting for 15 second(s)...\n", - "Has been waiting for 16 second(s)...\n", - "Has been waiting for 17 second(s)...\n", - "Has been waiting for 18 second(s)...\n", - "Has been waiting for 19 second(s)...\n", - "Has been waiting for 20 second(s)...\n", - "Has been waiting for 21 second(s)...\n", - "Has been waiting for 22 second(s)...\n", - "Has been waiting for 23 second(s)...\n", - "Has been waiting for 24 second(s)...\n", - "Has been waiting for 25 second(s)...\n", - "Has been waiting for 26 second(s)...\n", - "Has been waiting for 27 second(s)...\n", - "Has been waiting for 28 second(s)...\n", - "Has been waiting for 29 second(s)...\n", - "Has been waiting for 30 second(s)...\n", - "Has been waiting for 31 second(s)...\n", - "Has been waiting for 32 second(s)...\n", - "Has been waiting for 33 second(s)...\n", - "Has been waiting for 34 second(s)...\n", - "Has been waiting for 35 second(s)...\n", - "Has been waiting for 36 second(s)...\n", - "Has been waiting for 37 second(s)...\n", - "Has been waiting for 38 second(s)...\n", - "Has been waiting for 39 second(s)...\n", - "Has been waiting for 40 second(s)...\n", - "Has been waiting for 41 second(s)...\n", - "Has been waiting for 42 second(s)...\n", - "Has been waiting for 43 second(s)...\n", - "Has been waiting for 44 second(s)...\n", - "Has been waiting for 45 second(s)...\n", - "Has been waiting for 46 second(s)...\n", - "Has been waiting for 47 second(s)...\n", - "Has been waiting for 48 second(s)...\n", - "Has been waiting for 49 second(s)...\n", - "Has been waiting for 50 second(s)...\n", - "Has been waiting for 51 second(s)...\n", - "Has been waiting for 52 second(s)...\n", - "Has been waiting for 53 second(s)...\n", - "Has been waiting for 54 second(s)...\n", - "Has been waiting for 55 second(s)...\n", - "Has been waiting for 56 second(s)...\n", - "Has been waiting for 57 second(s)...\n", - "Has been waiting for 58 second(s)...\n", - "Has been waiting for 59 second(s)...\n", - "Has been waiting for 60 second(s)...\n", - "Has been waiting for 61 second(s)...\n", - "Has been waiting for 62 second(s)...\n", - "Has been waiting for 63 second(s)...\n", - "Has been waiting for 64 second(s)...\n", - "Has been waiting for 65 second(s)...\n", - "Has been waiting for 66 second(s)...\n", - "Has been waiting for 67 second(s)...\n", - "Has been waiting for 68 second(s)...\n", - "Has been waiting for 69 second(s)...\n", - "Has been waiting for 70 second(s)...\n", - "Has been waiting for 71 second(s)...\n", - "Has been waiting for 72 second(s)...\n", - "Has been waiting for 73 second(s)...\n", - "Has been waiting for 74 second(s)...\n", - "Has been waiting for 75 second(s)...\n", - "Has been waiting for 76 second(s)...\n", - "Has been waiting for 77 second(s)...\n", - "Has been waiting for 78 second(s)...\n", - "Has been waiting for 79 second(s)...\n", - "Has been waiting for 80 second(s)...\n", - "Has been waiting for 81 second(s)...\n", - "Has been waiting for 82 second(s)...\n", - "Has been waiting for 83 second(s)...\n", - "Has been waiting for 84 second(s)...\n", - "Has been waiting for 85 second(s)...\n", - "Has been waiting for 86 second(s)...\n", - "Has been waiting for 87 second(s)...\n", - "Has been waiting for 88 second(s)...\n", - "Has been waiting for 89 second(s)...\n", - "Has been waiting for 90 second(s)...\n", - "Has been waiting for 91 second(s)...\n", - "Has been waiting for 92 second(s)...\n", - "Has been waiting for 93 second(s)...\n" - ] - } - ], + "outputs": [], "source": [ "import threading\n", "import time\n", @@ -1467,18 +731,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "723389fb-ed7b-4c11-b430-c543af43666a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing example/multiprocess_example.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/multiprocess_example.py\n", "import multiprocessing\n", @@ -1510,36 +766,10 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "3e46e6e4-75e8-4f35-9fea-14b299502203", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/4\n", - " \n", - "Loading trace data from processes 1/4\n", - " \n", - "Loading trace data from processes 2/4\n", - " \n", - "Loading trace data from processes 3/4\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 29555\n", - " \n", - "Total Entries: 29555\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer example/multiprocess_example.py" ] @@ -1562,18 +792,10 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "333ae24a-9b35-4b97-b224-bf5dcdc99cbe", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing example/single_core_multi_thread_concurrent.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/single_core_multi_thread_concurrent.py\n", "import concurrent.futures\n", @@ -1610,63 +832,20 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "699b56b1-5b88-4466-bbea-e58f757658d5", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Downloading itr3_english.pdf...\n", - "Download complete for itr3_english.pdf!\n", - "Downloading itr5_english.pdf...\n", - "Downloading itr2_english.pdf...\n", - "Download complete for itr5_english.pdf!\n", - "Download complete for itr2_english.pdf!\n", - "Downloading itr1_english.pdf...\n", - "Downloading itr6_english.pdf...\n", - "Downloading itr7_english.pdf...\n", - "Download complete for itr1_english.pdf!\n", - "Download complete for itr6_english.pdf!\n", - "Download complete for itr7_english.pdf!\n", - "Downloading itr4_english.pdf...\n", - "Download complete for itr4_english.pdf!\n", - "All downloads completed!\n", - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/1\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 154337\n", - " \n", - "Total Entries: 154337\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer example/single_core_multi_thread_concurrent.py" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "36a97443-2dfb-4fd7-85ae-d73d481f62b0", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing example/multiprocess_example_concurrent.py\n" - ] - } - ], + "outputs": [], "source": [ "%%writefile example/multiprocess_example_concurrent.py\n", "import concurrent.futures\n", @@ -1697,38 +876,10 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "819e02ef-9a4d-4d5b-8259-d535e8963456", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[93m--log_multiprocess and --log_subprocess are no longer needed to trace multi-process program\u001b[0m\n", - "All prime number calculations completed!\n", - "\n", - " \n", - "Saving trace data, this could take a while\n", - " \n", - "Loading trace data from processes 0/4\n", - " \n", - "Loading trace data from processes 1/4\n", - " \n", - "Loading trace data from processes 2/4\n", - " \n", - "Loading trace data from processes 3/4\n", - " \n", - "Combining trace data\n", - " \n", - "Dumping trace data, total entries: 33063\n", - " \n", - "Total Entries: 33063\n", - "Use the following command to open the report:\n", - "\u001b[92mvizviewer \"c:\\Users\\bhmiller\\OneDrive - Intel Corporation\\Documents\\GitHub\\intermediate-python\\content\\result.json\"\u001b[0m\n" - ] - } - ], + "outputs": [], "source": [ "!viztracer --log_multiprocess example/multiprocess_example_concurrent.py" ] diff --git a/content/14_python_builtin_cli.ipynb b/content/14_python_builtin_cli.ipynb index 555d8a7..e536ed9 100644 --- a/content/14_python_builtin_cli.ipynb +++ b/content/14_python_builtin_cli.ipynb @@ -26,32 +26,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "83f54fa9-d481-4c2b-944c-b6ced5592e59", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "sys.path = [\n", - " 'C:\\\\Source\\\\intermediate-python\\\\content',\n", - " 'C:\\\\Python311\\\\python311.zip',\n", - " 'C:\\\\Python311\\\\DLLs',\n", - " 'C:\\\\Python311\\\\Lib',\n", - " 'C:\\\\Python311',\n", - " 'C:\\\\Source\\\\intermediate-python\\\\.venv',\n", - " 'C:\\\\Source\\\\intermediate-python\\\\.venv\\\\Lib\\\\site-packages',\n", - " 'C:\\\\Source\\\\intermediate-python\\\\.venv\\\\Lib\\\\site-packages\\\\win32',\n", - " 'C:\\\\Source\\\\intermediate-python\\\\.venv\\\\Lib\\\\site-packages\\\\win32\\\\lib',\n", - " 'C:\\\\Source\\\\intermediate-python\\\\.venv\\\\Lib\\\\site-packages\\\\Pythonwin',\n", - "]\n", - "USER_BASE: 'C:\\\\Users\\\\debakarr\\\\AppData\\\\Roaming\\\\Python' (exists)\n", - "USER_SITE: 'C:\\\\Users\\\\debakarr\\\\AppData\\\\Roaming\\\\Python\\\\Python311\\\\site-packages' (doesn't exist)\n", - "ENABLE_USER_SITE: False\n" - ] - } - ], + "outputs": [], "source": [ "!python -m site" ] @@ -275,4 +253,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +}