|
19 | 19 | "cell_type": "markdown",
|
20 | 20 | "metadata": {},
|
21 | 21 | "source": [
|
22 |
| - "[Object-Oriented Programming (OOP)](https://en.wikipedia.org/wiki/Object-oriented_programming) is a powerful approach on how to organize code for short programs (scripts), libraries, and applications." |
| 22 | + "[Object-Oriented Programming (OOP)](https://en.wikipedia.org/wiki/Object-oriented_programming) is a powerful and popular approach adopted to organize code for short programs (scripts), libraries, and applications." |
23 | 23 | ]
|
24 | 24 | },
|
25 | 25 | {
|
|
31 | 31 | "OOP is based on the concept of **objects**. An object is an instance of a **class**. A class is used to define all the required data structures as well as the functions (**methods**) that can be applied to the class data."
|
32 | 32 | ]
|
33 | 33 | },
|
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "" |
| 39 | + ] |
| 40 | + }, |
34 | 41 | {
|
35 | 42 | "cell_type": "markdown",
|
36 | 43 | "metadata": {},
|
37 | 44 | "source": [
|
38 | 45 | "In the [*A Class as a Data Container*](../python_basics/008_A_Class_as_a_Data_Container.ipynb) notebook, you have already learned how to create your own type using a **class** as a data container. This notebook will describe how to extend a data container class with methods."
|
39 | 46 | ]
|
40 | 47 | },
|
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n", |
| 53 | + "\n", |
| 54 | + "To review the concept of type in Python, read the [*Variable and Types*](../python_basics/001_Variables_and_Types.ipynb#Dynamic-Nature-of-a-Variable-Type) notebook." |
| 55 | + ] |
| 56 | + }, |
41 | 57 | {
|
42 | 58 | "cell_type": "markdown",
|
43 | 59 | "metadata": {},
|
44 | 60 | "source": [
|
45 | 61 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
46 | 62 | "\n",
|
47 |
| - "Differently from [*Programming Basics for Python* notebooks](https://github.com/hydroffice/python_basics), you will be asked to write your OOP code in separated `.py` files, each of them containing the definition of a single class. " |
| 63 | + "You will be asked to write your OOP code in separated `.py` files, each of them represents a Python module containing the class definition. \n", |
| 64 | + "**This is different from the approach adopted in [*Programming Basics for Python* notebooks](https://github.com/hydroffice/python_basics) where all the code is written in the notebook itself!**" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "The adopted approach imports the class code contained in modules, and then creates instances of such a class in the body of this notebook (and in the following ones)." |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n", |
| 79 | + "\n", |
| 80 | + "To review the concept of module in Python, read the [*Read and Write Text Files*](../python_basics/006_Read_and_Write_Text_Files.ipynb#Read-and-Write-Text-Files) notebook." |
48 | 81 | ]
|
49 | 82 | },
|
50 | 83 | {
|
51 | 84 | "cell_type": "markdown",
|
52 | 85 | "metadata": {},
|
53 | 86 | "source": [
|
54 |
| - "Then, in the body of this notebook (and in the following ones), you will just import such a code and create instances of such classes.\n", |
| 87 | + "The following code cell performs two preliminary **required** operations:\n", |
55 | 88 | "\n",
|
56 |
| - "To avoid to have to manually reload the classes when new changes are applied, we will execute the following code cell to use the `autoreload` extension that automatically reloads module before executing the code. The code cell then instructs Python to look in the `code` folder for local Python modules." |
| 89 | + "* Load the `autoreload` [extension](https://jupyter-notebook.readthedocs.io/en/stable/extending/) to avoid to have to manually reload the classes when changes are applied. \n", |
| 90 | + "* Instruct Python (using `sys.path.append()`) to look in the `code` folder for local Python modules." |
57 | 91 | ]
|
58 | 92 | },
|
59 | 93 | {
|
|
106 | 140 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
107 | 141 | "\n",
|
108 | 142 | "Modify the empty `waterlevel.py` file located in the `mycode` folder to successfully execute the following code.\n",
|
| 143 | + "<br><br>\n", |
109 | 144 | "*The solution code imports the `WaterLevel` class from the `waterlevel_definition` module located in the `solutions` folder.*"
|
110 | 145 | ]
|
111 | 146 | },
|
|
146 | 181 | "cell_type": "markdown",
|
147 | 182 | "metadata": {},
|
148 | 183 | "source": [
|
149 |
| - "We now add a few attributes to the previously defined `WaterLevel` class:\n", |
| 184 | + "We can now add a few attributes to the previously defined `WaterLevel` class:\n", |
150 | 185 | "\n",
|
151 | 186 | "* Two lists (named `epochs` and `water_levels`, respectively)\n",
|
152 | 187 | "* A `metadata` dictionary with the following pairs of key and value:\n",
|
|
166 | 201 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
167 | 202 | "\n",
|
168 | 203 | "Extend the `WaterLevel` class in the `waterlevel.py` file (located in the `mycode` folder) to successfully execute the following code.\n",
|
| 204 | + "<br><br>\n", |
169 | 205 | "*The solution code imports the `WaterLevel` class from the `waterlevel_attributes` module located in the `solutions` folder.*"
|
170 | 206 | ]
|
171 | 207 | },
|
|
221 | 257 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
222 | 258 | "\n",
|
223 | 259 | "Extend the `WaterLevel` class in the `waterlevel.py` file to be able to take a `data_path` parameter for the location of the data file.\n",
|
| 260 | + "<br><br>\n", |
224 | 261 | "*The solution code imports the `WaterLevel` class from the `waterlevel_initialization` module located in the `solutions` folder.*"
|
225 | 262 | ]
|
226 | 263 | },
|
|
287 | 324 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
288 | 325 | "\n",
|
289 | 326 | "Extend the `__init__` method in the `WaterLevel` class in the `waterlevel.py` file to throw a meaningful error when the passed `data_path` does not actually exist.\n",
|
| 327 | + "<br><br>\n", |
290 | 328 | "*The solution code imports the `WaterLevel` class from the `waterlevel_initialization_check` module located in the `solutions` folder.*"
|
291 | 329 | ]
|
292 | 330 | },
|
|
388 | 426 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
|
389 | 427 | "\n",
|
390 | 428 | "Extend the `WaterLevel` class in the `waterlevel.py` file with a `__str__(self)` method that returns a `str` with some meaningful information about the status of the object.\n",
|
| 429 | + "<br><br>\n", |
391 | 430 | "*The solution code imports the `WaterLevel` class from the `waterlevel_str` module located in the `solutions` folder.*"
|
392 | 431 | ]
|
393 | 432 | },
|
394 | 433 | {
|
395 | 434 | "cell_type": "code",
|
396 |
| - "execution_count": 29, |
| 435 | + "execution_count": null, |
397 | 436 | "metadata": {
|
398 | 437 | "solution2": "hidden"
|
399 | 438 | },
|
400 |
| - "outputs": [ |
401 |
| - { |
402 |
| - "name": "stdout", |
403 |
| - "output_type": "stream", |
404 |
| - "text": [ |
405 |
| - "WaterLevel[count: 0, path: C:\\code\\hyo2\\epom\\ocean_data_science\\data\\tide.txt]\n" |
406 |
| - ] |
407 |
| - } |
408 |
| - ], |
| 439 | + "outputs": [], |
409 | 440 | "source": [
|
410 | 441 | "from solutions.waterlevel_str import WaterLevel\n",
|
411 | 442 | "import os\n",
|
|
435 | 466 | "source": [
|
436 | 467 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
|
437 | 468 | "\n",
|
438 |
| - "There are not fixed rules of what to return as `str`. The only requirement is to be a **nicely printable representation** of an object." |
| 469 | + "There are not fixed rules of what to return as `str`. Based on the official Python documentation, the only requirement is that it should be an [*\"informal or nicely printable string representation of an object\"*](https://docs.python.org/3.6/library/stdtypes.html#str)." |
439 | 470 | ]
|
440 | 471 | },
|
441 | 472 | {
|
|
0 commit comments