10000 Address minor issues in the "modules and packages" notebook (#266) · DagaEMPA/python-tutorial@a50c214 · GitHub
[go: up one dir, main page]

Skip to content

Commit a50c214

Browse files
authored
Address minor issues in the "modules and packages" notebook (empa-scientific-it#266)
1 parent 751bcb1 commit a50c214

File tree

1 file changed

+87
-48
lines changed

1 file changed

+87
-48
lines changed

modules_and_packages.ipynb

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"Move the `Point` class and the `distance` function to a new file called `point.py`:\n",
139139
"\n",
140140
"1. Create a new file called `point.py` in the same directory as this notebook.\n",
141-
"2. Copy the `Point` class and the `distance` function to the new file.\n",
141+
"2. Copy **only** the `Point` class and the `distance` function to the new file.\n",
142142
"3. Save the file."
143143
]
144144
},
@@ -152,7 +152,9 @@
152152
{
153153
"cell_type": "code",
154154
"execution_count": null,
155-
"metadata": {},
155+
"metadata": {
156+
"tags": []
157+
},
156158
"outputs": [],
157159
"source": [
158160
"from point import Point, distance\n",
@@ -188,10 +190,10 @@
188190
" import time\n",
189191
"\n",
190192
" print(\"I will now sleep\")\n",
191-
" time.sleep(10)\n",
193+
" time.sleep(5)\n",
192194
" print(\"I am now awake\")\n",
193195
"\n",
194-
" def something() -> int:\n",
196+
" def some_function() -> int:\n",
195197
" print(\"I don't do much\")\n",
196198
" ```\n",
197199
"\n",
@@ -202,18 +204,21 @@
202204
{
203205
"cell_type": "code",
204206
"execution_count": null,
205-
"metadata": {},
207+
"metadata": {
208+
"tags": []
209+
},
206210
"outputs": [],
207211
"source": [
208-
"from import_side_effect import something\n",
209-
"something()"
212+
"from import_side_effect import some_function\n",
213+
"some_function()"
210214
]
211215
},
212216
{
213217
"cell_type": "markdown",
214218
"metadata": {},
215219
"source": [
216-
"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",
220+
"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",
221+
"Only after this process finished you were able to execute `some_function`.\n",
217222
"\n",
218223
"To avoid these problems, you should put these statements in a function that the user can call when they need it."
219224
]
@@ -251,7 +256,9 @@
251256
{
252257
"cell_type": "code",
253258
"execution_count": null,
254-
"metadata": {},
259+
"metadata": {
260+
"tags": []
261+
},
255262
"outputs": [],
256263
"source": [
257264
"from mypackage.point import Point, distance\n",
@@ -289,7 +296,9 @@
289296
{
290297
"cell_type": "code",
291298
"execution_count": null,
292-
"metadata": {},
299+
"metadata": {
300+
"tags": []
301+
},
293302
"outputs": [],
294303
"source": [
295304
"from mypackage.point import Point\n",
@@ -318,13 +327,21 @@
318327
"Let's see some examples:"
319328
]
320329
},
330+
{
331+
"cell_type": "markdown",
332+
"metadata": {},
333+
"source": [
334+
"**Same as above:**"
335+
]
336+
},
321337
{
322338
"cell_type": "code",
323339
"execution_count": null,
324-
"metadata": {},
340+
"metadata": {
341+
"tags": []
342+
},
325343
"outputs": [],
326344
"source": [
327-
"# Same as above\n",
328345
"from mypackage.point import Point\n",
329346
"from mypackage.utils import distance\n",
330347
"\n",
@@ -333,13 +350,21 @@
333350
"print(f\"The distance between `p1` and `p2` is {distance(p1, p2)}\")"
334351
]
335352
},
353+
{
354+
"cell_type": "markdown",
355+
"metadata": {},
356+
"source": [
357+
"**Import only the `mypackage` package:**"
358+
]
359+
},
336360
{
337361
"cell_type": "code",
338362
"execution_count": null,
339-
"metadata": {},
363+
"metadata": {
364+
"tags": []
365+
},
340366
"outputs": [],
341367
"source": [
342-
"# Import only the `mypackage` package\n",
343368
"import mypackage\n",
344369
"\n",
345370
"p1 = mypackage.point.Point(0, 0)\n",
@@ -348,13 +373,23 @@
348373
"print(f\"The distance between `p1` and `p2` is {mypackage.utils.distance(p1, p2)}\")"
349374
]
350375
},
376+
{
377+
"cell_type": "markdown",
378+
"metadata": {
379+
"tags": []
380+
},
381+
"source": [
382+
"**Import `point` and `utils` modules from `mypackage`:**"
383+
]
384+
},
351385
{
352386
"cell_type": "code",
353387
"execution_count": null,
354-
"metadata": {},
388+
"metadata": {
389+
"tags": []
390+
},
355391
"outputs": [],
356392
"source": [
357-
"# Import `point` and `utils` modules from `mypackage`\n",
358393
"from mypackage import point, utils\n",
359394
"\n",
360395
"p1 = point.Point(0, 0)\n",
@@ -417,10 +452,22 @@
417452
"If everything went well, we should be able to execute the following code:"
418453
]
419454
},
455+
{
456+
"cell_type": "markdown",
457+
"metadata": {},
458+
"source": [
459+
"<div class=\"alert alert-block alert-warning\">\n",
460+
" <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",
461+
"The reason for this is that Python has imported the package before, and it is too lazy to import it again.\n",
462+
"</div>"
463+
]
464+
},
420465
{
421466
"cell_type": "code",
422467
"execution_count": null,
423-
"metadata": {},
468+
"metadata": {
469+
"tags": []
470+
},
424471
"outputs": [],
425472
"source": [
426473
"from mypackage import Point, distance\n",
@@ -430,17 +477,6 @@
430477
"print(f\"The distance between `p1` and `p2` is {distance(p1, p2)}\")"
431478
]
432479
},
433-
{
434-
"cell_type": "markdown",
435-
"metadata": {},
436-
"source": [
437-
"<div class=\"alert alert-block alert-warning\">\n",
438-
" <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",
439-
"Python has imported the package before, and it is too lazy to import it again.\n",
440-
"We need to restart Python.\n",
441-
"</div>"
442-
]
443-
},
444480
{
445481
"cell_type": "markdown",
446482
"metadata": {},
@@ -453,7 +489,9 @@
453489
{
454490
"cell_type": "code",
455491
"execution_count": null,
456-
"metadata": {},
492+
"metadata": {
493+
"tags": []
494+
},
457495
"outputs": [],
458496
"source": [
459497
"import mypackage\n",
@@ -474,7 +512,9 @@
474512
{
475513
"cell_type": "code",
476514
"execution_count": null,
477-
"metadata": {},
515+
"metadata": {
516+
"tags": []
517+
},
478518
"outputs": [],
479519
"source": [
480520
"import mypackage as mp\n",
@@ -522,7 +562,9 @@
522562
{
523563
"cell_type": "code",
524564
"execution_count": null,
525-
"metadata": {},
565+
"metadata": {
566+
"tags": []
567+
},
526568
"outputs": [],
527569
"source": [
528570
"class Line:\n",
@@ -562,7 +604,9 @@
562604
{
563605
"cell_type": "code",
564606
"execution_count": null,
565-
"metadata": {},
607+
"metadata": {
608+
"tags": []
609+
},
566610
"outputs": [],
567611
"source": [
568612
"import mypackage as mp\n",
@@ -609,7 +653,9 @@
609653
{
610654
"cell_type": "code",
611655
"execution_count": null,
612-
"metadata": {},
656+
"metadata": {
657+
"tags": []
658+
},
613659
"outputs": [],
614660
"source": [
615661
"import mypackage as mp\n",
@@ -659,7 +705,9 @@
659705
{
660706
"cell_type": "code",
661707
"execution_count": null,
662-
"metadata": {},
708+
"metadata": {
709+
"tags": []
710+
},
663711
"outputs": [],
664712
"source": [
665713
"import mypackage as mp\n",
@@ -779,17 +827,13 @@
779827
"## Exercise on installing the package\n",
780828
"\n",
781829
"1. Inside the `mypackage` directory, we need to create another directory called `mypackage`.\n",
782-
"2. Move the `geometry` directory together with `__init__.py` and `utils.py` files to the `mypackage` sub-directory.\n",
830+
"2. Move the `geometry` directory together with `__init__.py` and `utils.py` files to the freshly-created `mypackage` sub-directory.\n",
783831
"3. Create a new file called `pyproject.toml` in the top-level `mypackage` directory with the content from the cell above.\n",
784-
"4. Open the terminal (`File` -> `New Launcher`, then select `Terminal`), and navigate to the `mypackage` directory.\n",
832+
"4. Open the terminal (`File` -> `New Launcher`, then select `Terminal`), and run the following command to install `mypackage`:\n",
785833
"```bash\n",
786-
" $ cd mypackage\n",
834+
" $ pip install mypackage/\n",
787835
"```\n",
788-
"5. Run the following command to install the package:\n",
789-
"```bash\n",
790-
" $ pip install .\n",
791-
"```\n",
792-
"6. Return to the new notebook we created in the previous exercise and try to import the `mypackage` package again.\n",
836+
"5. Return to the new notebook we created in the previous exercise and try to import the `mypackage` package again.\n",
793837
"\n",
794838
"\n",
795839
"Congratulations, now your package is installed and you can import it from anywhere!"
@@ -999,12 +1043,7 @@
9991043
"name": "python",
10001044
"nbconvert_exporter": "python",
10011045
"pygments_lexer": "ipython3",
1002-
"version": "3.9.6"
1003-
},
1004-
"vscode": {
1005-
"interpreter": {
1006-
"hash": "2b2b08c237e7c74caf82ad6490346743fce9bd812859047e307c1031e538ab1c"
1007-
}
1046+
"version": "3.9.12"
10081047
}
10091048
},
10101049
"nbformat": 4,

0 commit comments

Comments
 (0)
0