Skip to content

Commit

Permalink
Address minor issues in the "modules and packages" notebook (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakutovicha authored Nov 27, 2024
1 parent 751bcb1 commit a50c214
Showing 1 changed file with 87 additions and 48 deletions.
135 changes: 87 additions & 48 deletions modules_and_packages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"Move the `Point` class and the `distance` function to a new file called `point.py`:\n",
"\n",
"1. Create a new file called `point.py` in the same directory as this notebook.\n",
"2. Copy the `Point` class and the `distance` function to the new file.\n",
"2. Copy **only** the `Point` class and the `distance` function to the new file.\n",
"3. Save the file."
]
},
Expand All @@ -152,7 +152,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from point import Point, distance\n",
Expand Down Expand Up @@ -188,10 +190,10 @@
" import time\n",
"\n",
" print(\"I will now sleep\")\n",
" time.sleep(10)\n",
" time.sleep(5)\n",
" print(\"I am now awake\")\n",
"\n",
" def something() -> int:\n",
" def some_function() -> int:\n",
" print(\"I don't do much\")\n",
" ```\n",
"\n",
Expand All @@ -202,18 +204,21 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from import_side_effect import something\n",
"something()"
"from import_side_effect import some_function\n",
"some_function()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you see, importing the module printed a first message on the terminal, then waited some time and then finally printed a message on the terminal again. Only after this process finished you were able to execute `something`.\n",
"As you see, importing the module printed a first message on the terminal, then waited some time and then finally printed a message on the terminal again.\n",
"Only after this process finished you were able to execute `some_function`.\n",
"\n",
"To avoid these problems, you should put these statements in a function that the user can call when they need it."
]
Expand Down Expand Up @@ -251,7 +256,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from mypackage.point import Point, distance\n",
Expand Down Expand Up @@ -289,7 +296,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from mypackage.point import Point\n",
Expand Down Expand Up @@ -318,13 +327,21 @@
"Let's see some examples:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Same as above:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Same as above\n",
"from mypackage.point import Point\n",
"from mypackage.utils import distance\n",
"\n",
Expand All @@ -333,13 +350,21 @@
"print(f\"The distance between `p1` and `p2` is {distance(p1, p2)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Import only the `mypackage` package:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Import only the `mypackage` package\n",
"import mypackage\n",
"\n",
"p1 = mypackage.point.Point(0, 0)\n",
Expand All @@ -348,13 +373,23 @@
"print(f\"The distance between `p1` and `p2` is {mypackage.utils.distance(p1, p2)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"**Import `point` and `utils` modules from `mypackage`:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Import `point` and `utils` modules from `mypackage`\n",
"from mypackage import point, utils\n",
"\n",
"p1 = point.Point(0, 0)\n",
Expand Down Expand Up @@ -417,10 +452,22 @@
"If everything went well, we should be able to execute the following code:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
" <b> Warning: </b> Sometimes for the code below to work you need to restart the Python kernel. Find the <b>Kernel</b> menu at the top and then choose <b>Restart Kernel</b>.\n",
"The reason for this is that Python has imported the package before, and it is too lazy to import it again.\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from mypackage import Point, distance\n",
Expand All @@ -430,17 +477,6 @@
"print(f\"The distance between `p1` and `p2` is {distance(p1, p2)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
" <b> Warning: </b> If you did everything correctly but nothing works, consider restarting the Python kernel. Find the <b>Kernel</b> menu and then choose <b>Restart Kernel</b>.\n",
"Python has imported the package before, and it is too lazy to import it again.\n",
"We need to restart Python.\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -453,7 +489,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import mypackage\n",
Expand All @@ -474,7 +512,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import mypackage as mp\n",
Expand Down Expand Up @@ -522,7 +562,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"class Line:\n",
Expand Down Expand Up @@ -562,7 +604,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import mypackage as mp\n",
Expand Down Expand Up @@ -609,7 +653,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import mypackage as mp\n",
Expand Down Expand Up @@ -659,7 +705,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import mypackage as mp\n",
Expand Down Expand Up @@ -779,17 +827,13 @@
"## Exercise on installing the package\n",
"\n",
"1. Inside the `mypackage` directory, we need to create another directory called `mypackage`.\n",
"2. Move the `geometry` directory together with `__init__.py` and `utils.py` files to the `mypackage` sub-directory.\n",
"2. Move the `geometry` directory together with `__init__.py` and `utils.py` files to the freshly-created `mypackage` sub-directory.\n",
"3. Create a new file called `pyproject.toml` in the top-level `mypackage` directory with the content from the cell above.\n",
"4. Open the terminal (`File` -> `New Launcher`, then select `Terminal`), and navigate to the `mypackage` directory.\n",
"4. Open the terminal (`File` -> `New Launcher`, then select `Terminal`), and run the following command to install `mypackage`:\n",
"```bash\n",
" $ cd mypackage\n",
" $ pip install mypackage/\n",
"```\n",
"5. Run the following command to install the package:\n",
"```bash\n",
" $ pip install .\n",
"```\n",
"6. Return to the new notebook we created in the previous exercise and try to import the `mypackage` package again.\n",
"5. Return to the new notebook we created in the previous exercise and try to import the `mypackage` package again.\n",
"\n",
"\n",
"Congratulations, now your package is installed and you can import it from anywhere!"
Expand Down Expand Up @@ -999,12 +1043,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"vscode": {
"interpreter": {
"hash": "2b2b08c237e7c74caf82ad6490346743fce9bd812859047e307c1031e538ab1c"
}
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down

0 comments on commit a50c214

Please sign in to comment.