Skip to content

Commit 67d56c3

Browse files
author
giumas
committed
improvements after Roxy comments
1 parent 44bb363 commit 67d56c3

10 files changed

+71
-37
lines changed

OOP_000_First_Steps_of_a_Class.ipynb

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"cell_type": "markdown",
2020
"metadata": {},
2121
"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."
2323
]
2424
},
2525
{
@@ -31,29 +31,63 @@
3131
"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."
3232
]
3333
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"![Class and Object](images/OOP_000_000_class_and_object.png)"
39+
]
40+
},
3441
{
3542
"cell_type": "markdown",
3643
"metadata": {},
3744
"source": [
3845
"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."
3946
]
4047
},
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+
},
4157
{
4258
"cell_type": "markdown",
4359
"metadata": {},
4460
"source": [
4561
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
4662
"\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."
4881
]
4982
},
5083
{
5184
"cell_type": "markdown",
5285
"metadata": {},
5386
"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",
5588
"\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."
5791
]
5892
},
5993
{
@@ -106,6 +140,7 @@
106140
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
107141
"\n",
108142
"Modify the empty `waterlevel.py` file located in the `mycode` folder to successfully execute the following code.\n",
143+
"<br><br>\n",
109144
"*The solution code imports the `WaterLevel` class from the `waterlevel_definition` module located in the `solutions` folder.*"
110145
]
111146
},
@@ -146,7 +181,7 @@
146181
"cell_type": "markdown",
147182
"metadata": {},
148183
"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",
150185
"\n",
151186
"* Two lists (named `epochs` and `water_levels`, respectively)\n",
152187
"* A `metadata` dictionary with the following pairs of key and value:\n",
@@ -166,6 +201,7 @@
166201
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
167202
"\n",
168203
"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",
169205
"*The solution code imports the `WaterLevel` class from the `waterlevel_attributes` module located in the `solutions` folder.*"
170206
]
171207
},
@@ -221,6 +257,7 @@
221257
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
222258
"\n",
223259
"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",
224261
"*The solution code imports the `WaterLevel` class from the `waterlevel_initialization` module located in the `solutions` folder.*"
225262
]
226263
},
@@ -287,6 +324,7 @@
287324
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
288325
"\n",
289326
"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",
290328
"*The solution code imports the `WaterLevel` class from the `waterlevel_initialization_check` module located in the `solutions` folder.*"
291329
]
292330
},
@@ -388,24 +426,17 @@
388426
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
389427
"\n",
390428
"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",
391430
"*The solution code imports the `WaterLevel` class from the `waterlevel_str` module located in the `solutions` folder.*"
392431
]
393432
},
394433
{
395434
"cell_type": "code",
396-
"execution_count": 29,
435+
"execution_count": null,
397436
"metadata": {
398437
"solution2": "hidden"
399438
},
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": [],
409440
"source": [
410441
"from solutions.waterlevel_str import WaterLevel\n",
411442
"import os\n",
@@ -435,7 +466,7 @@
435466
"source": [
436467
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
437468
"\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)."
439470
]
440471
},
441472
{

OOP_001_More_About_Classes.ipynb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@
3737
"source": [
3838
"* The presence of a [class inheritance mechanism](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)). (Multiple base classes are allowed.)\n",
3939
"* The property that a derived class can [override any methods](https://en.wikipedia.org/wiki/Method_overriding) of its base class. \n",
40-
"* The possibility for a method to call the method of a base class with the same name. "
40+
"* The possibility for a method to call the method of a base class with the same function name. "
4141
]
4242
},
4343
{
4444
"cell_type": "markdown",
4545
"metadata": {},
4646
"source": [
47-
"An in-depth explanation of the above features is outside of the scope of this course. However, after this notebook, you will know how to add methods to a `class` in Python. The acquired notions should provide you with a good base to continue the learning process about OOP on your own."
47+
"An in-depth explanation of the above features is outside of the scope of this course. However, after this notebook, you will know how to add methods to a `class` in Python. The acquired notions should provide you with a good base to allow you to continue the learning process about OOP on your own."
4848
]
4949
},
5050
{
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"But first, please execute the following code for the reasons explained in the previous notebook."
54+
"But first, please execute the following code, for the smae reasons explained at the beginning of the previous notebook."
5555
]
5656
},
5757
{
@@ -128,7 +128,7 @@
128128
"cell_type": "markdown",
129129
"metadata": {},
130130
"source": [
131-
"As first example of an interface method, we will add a `read(self)`. \n",
131+
"As a first example of an interface method, we will add a `read(self)`. \n",
132132
"\n",
133133
"This method will perform similar operations to what was described in the [Read and Write Text Files notebook](../python_basics/006_Read_and_Write_Text_Files), but using the internal class attributes."
134134
]
@@ -153,15 +153,15 @@
153153
"| 2 | Hours |\n",
154154
"| 3 | Minutes |\n",
155155
"| 4 | Seconds |\n",
156-
"| 5 | Epoch |\n",
156+
"| 5 | POSIX Time |\n",
157157
"| 6 | Water Level |"
158158
]
159159
},
160160
{
161161
"cell_type": "markdown",
162162
"metadata": {},
163163
"source": [
164-
"As such, each row in the data file represents a record with a specific time and date (epoch), and an associated water level observation in meters. The epoch for each record is represented two times: \n",
164+
"As such, each row in the data file represents a record with a specific time and date, and an associated water level observation in meters. The time for each record is represented in two ways: \n",
165165
"\n",
166166
"* By a combination of the year, ordinal day (aka, year-day), hour and minute (as integer values) and seconds (as float values). \n",
167167
"* The [POSIX time](https://en.wikipedia.org/wiki/Unix_time) in seconds (as float value)."
@@ -180,7 +180,7 @@
180180
"cell_type": "markdown",
181181
"metadata": {},
182182
"source": [
183-
"Since POSIX time is independent of the time zone in which the data is collected, an epoch is represented by a single number, thereby simplifying math involving time spans."
183+
"Since POSIX time is independent of the time zone in which the data is collected, a time is represented by a single number, thereby simplifying math involving time spans."
184184
]
185185
},
186186
{
@@ -226,6 +226,7 @@
226226
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
227227
"\n",
228228
"Extend the `WaterLevel` class in the `waterlevel.py` file with a `read(self)` that reads and stores two fields for each row in the data file: the POSIX time and the water level.\n",
229+
"<br><br>\n",
229230
"*The solution code imports the `WaterLevel` class from the `waterlevel_read` module located in the `solutions` folder.*"
230231
]
231232
},
@@ -291,6 +292,8 @@
291292
"* [The official Python 3.6 documentation](https://docs.python.org/3.6/index.html)\n",
292293
" * [Classes](https://docs.python.org/3.6/tutorial/classes.html)\n",
293294
"* [Object-oriented Programming](https://en.wikipedia.org/wiki/Object-oriented_programming)\n",
295+
"* [POSIX time](https://en.wikipedia.org/wiki/Unix_time)\n",
296+
"* [Time zone](https://en.wikipedia.org/wiki/Time_zone)\n",
294297
"* [Programming Basics with Python](https://github.com/hydroffice/python_basics)"
295298
]
296299
},
83.1 KB
Loading

images/resources/resources.pptx

196 KB
Binary file not shown.

index.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
"cell_type": "markdown",
1919
"metadata": {},
2020
"source": [
21-
"Ocean [data science](https://en.wikipedia.org/wiki/Data_science) is a broad and interdisciplinary subject that requires statistical/modeling knowledge and computer science skills, coupled with domain-specific expertise. "
21+
"Ocean [data science](https://en.wikipedia.org/wiki/Data_science) is a broad and interdisciplinary topic that requires statistical and modeling knowledge as well as computer science skills, coupled with domain-specific expertise. "
2222
]
2323
},
2424
{
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"As you can imagine, it would be a long and overwhelming goal to cover all the aspects involved in the subject, and there will be very likely many duplications with other courses. The aim of the [*Foundations of Ocean Data Science* notebooks](https://github.com/hydroffice/ocean_data_science) is instead to teach you a selected number of basic concepts in ocean data science and to introduce some popular Python libraries related to the subject, to prepare you to the assignments for those other courses as well as to conduct your own research. "
28+
"As you can imagine, it would be an overwhelming goal to cover all the aspects of ocean data science, and there will likely be many cross-overs with material covered in other courses. The [*Foundations of Ocean Data Science* notebooks](https://github.com/hydroffice/ocean_data_science) will only teach you a selected number of basic concepts in ocean data science and will introduce some popular Python libraries related to the subject. This will help you to prepare you for coming assignments (for this and other courses) and your own research. "
2929
]
3030
},
3131
{
3232
"cell_type": "markdown",
3333
"metadata": {},
3434
"source": [
35-
"The notebooks assume the level of Python knowledge described in [*Programming Basics for Python* notebooks](https://github.com/hydroffice/python_basics). If you are not familiar with these latter notebooks, you should start from [Programming Basics with Python - Quick Start](https://www.hydroffice.org/manuals/epom/python_basics_quickstart.html)."
35+
"The notebooks assume some level of Python knowledge. Mainly, you need the concepts introduced in the [*Programming Basics for Python* notebooks](https://github.com/hydroffice/python_basics). If you are not familiar with these earlier notebooks, you should start from [Programming Basics with Python - Quick Start](https://www.hydroffice.org/manuals/epom/python_basics_quickstart.html)."
3636
]
3737
},
3838
{

solutions/waterlevel_attributes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class WaterLevel:
33

44
def __init__(self): # new method
55

6-
self.epochs = list()
6+
self.times = list()
77
self.water_levels = list()
88
self.metadata = dict()
99
self.metadata["units"] = "m"
10-
self.metadata["count"] = 0
10+
self.metadata["count"] = 0

solutions/waterlevel_initialization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class WaterLevel:
22
"""A Class for Water Level Data"""
33

44
def __init__(self, data_path): # new code
5-
5+
66
self.data_path = data_path # new code
7-
self.epochs = list()
7+
self.times = list()
88
self.water_levels = list()
99
self.metadata = dict()
1010
self.metadata["units"] = "m"

solutions/waterlevel_initialization_check.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class WaterLevel:
77
def __init__(self, data_path):
88

99
if not os.path.exists(data_path): # new code
10-
raise RuntimeError('Unable to locate the data file: %s' % data_path) # new code
10+
raise RuntimeError('Unable to locate the data file: %s' % data_path) # new code
1111

1212
self.data_path = data_path
13-
self.epochs = list()
13+
self.times = list()
1414
self.water_levels = list()
1515
self.metadata = dict()
1616
self.metadata["units"] = "m"

solutions/waterlevel_read.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class WaterLevel:
88
def __init__(self, data_path):
99

1010
if not os.path.exists(data_path):
11-
raise RuntimeError('Unable to locate the data file: %s' % data_path)
11+
raise RuntimeError('Unable to locate the data file: %s' % data_path)
1212

1313
self.data_path = data_path
14-
self.epochs = list()
14+
self.times = list()
1515
self.water_levels = list()
1616
self.metadata = dict()
1717
self.metadata["uom"] = "m"
@@ -34,8 +34,8 @@ def read(self):
3434
count = 0 # initialize the counter for the number of rows read
3535
for wl_line in wl_lines:
3636
observations = wl_line.split() # Tokenize the string
37-
epoch = datetime.fromtimestamp(float(observations[5]), timezone.utc)
38-
self.epochs.append(epoch)
37+
time = datetime.fromtimestamp(float(observations[5]), timezone.utc)
38+
self.times.append(time)
3939
self.water_levels.append(float(observations[6]))
4040
count += 1
4141
self.metadata["count"] = count

solutions/waterlevel_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, data_path):
1010
raise RuntimeError('Unable to locate the data file: %s' % data_path)
1111

1212
self.data_path = data_path
13-
self.epochs = list()
13+
self.times = list()
1414
self.water_levels = list()
1515
self.metadata = dict()
1616
self.metadata["uom"] = "m"

0 commit comments

Comments
 (0)