-
Notifications
You must be signed in to change notification settings - Fork 1
/
05_oop.qmd
856 lines (603 loc) · 15.4 KB
/
05_oop.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
---
title: "Object oriented design in Python"
format:
html: default
revealjs:
output-file: 05_oop_slides.html
slide-number: true
footer: Python package development
logo: academy_logo.png
---
## Object oriented design
Benefits of object oriented design:
* Encapsulation
* Code reuse (composition, inheritance)
* Abstraction
## Encapsulation
```{.python code-line-numbers="1-5|7-9|10-12"}
class Location:
def __init__(self, name, longitude, latitude):
self.name = name.upper() # Names are always uppercase
self.longitude = longitude
self.latitude = latitude
>>> loc = Location("Antwerp", 4.42, 51.22)
>>> loc.name
'ANTWERP'
>>> loc.name = "Antwerpen"
>>> loc.name
"Antwerpen" 😟
```
## Encapsulation - Attributes
Variables prefixed with an underscore (`self._name`) is a convention to indicate that the instance variable is private.
```{.python code-line-numbers="|3,7-9|10-11"}
class Location:
def __init__(self, name, longitude, latitude):
self._name = name.upper() # Names are always uppercase
...
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value.upper()
>>> loc = Location("Antwerp", 4.42, 51.22)
>>> loc.name = "Antwerpen"
>>> loc.name
"ANTWERPEN" 😊
```
## Composition {.smaller}
::: {.columns}
::: {.column}
Composition in object oriented design is a way to combine objects or data types into more complex objects.
:::
::: {.column}
```{mermaid}
classDiagram
class Grid{
+ nx
+ dx
+ ny
+ dy
+ find_index()
}
class ItemInfo{
+ name
+ type
+ unit
}
class DataArray{
+ data
+ time
+ item
+ geometry
+ plot()
}
DataArray --* Grid
DataArray --* ItemInfo
```
:::
::::
## Composition - Example {.smaller}
```python
class Grid:
def __init__(self, nx, dx, ny, dy):
self.nx = nx
self.dx = dx
self.ny = ny
self.dy = dy
def find_index(self, x,y):
...
class DataArray:
def __init__(self, data, time, item, geometry):
self.data = data
self.time = time
self.item = item
self.geometry = geometry
def plot(self):
...
```
. . .
`DataArray` *has a* `geometry` (e.g. `Grid`) and an `item` (`ItemInfo`).
## Inheritance
::: {.incremental}
* Inheritance is a way to reuse code and specialize behavior.
* A child class inherits the attributes and methods from the parent class.
* A child class can override the methods of the parent class.
* A child class can add new methods.
:::
## Inheritance - Example
```{mermaid}
classDiagram
class _GeometryFM{
+ node_coordinates
+ element_table
}
class GeometryFM2D{
+ interp2d()
+ get_element_area()
+ plot()
}
class _GeometryFMLayered{
- _n_layers
- _n_sigma
+ to_2d_geometry()
}
class GeometryFM3D{
+ plot()
}
class GeometryFMVerticalProfile{
+ plot()
}
_GeometryFM <|-- GeometryFM2D
_GeometryFM <|-- _GeometryFMLayered
_GeometryFMLayered <|-- GeometryFM3D
_GeometryFMLayered <|-- GeometryFMVerticalProfile
```
. . .
`GeometryFM3D` inherits from `_GeometryFMLayered`, it *is a* `_GeometryFMLayered`.
## Inheritance - Example (2)
```python
class _GeometryFMLayered(_GeometryFM):
def __init__(self, nodes, elements, n_layers, n_sigma):
# call the parent class init method
super().__init__(
nodes=nodes,
elements=elements,
)
self._n_layers = n_layers
self._n_sigma = n_sigma
```
## Composition vs inheritance
::: {.incremental}
* Inheritance is often used to reuse code, but this is not the main purpose of inheritance.
* Inheritance is used to specialize behavior.
* In most cases, composition is a better choice than inheritance.
* Some recent programming languages (e.g. Go & Rust) do not support this style of inheritance.
* Use inheritance only when it makes sense.
:::
::: aside
Hillard, 2020, Ch. 8 "The rules (and exceptions) of inheritance"
:::
## Types
**C#**
```{.csharp code-line-numbers="1-4"}
int n = 2;
String s = "Hello";
public String RepeatedString(String s, int n) {
return Enumerable.Repeat(s, n).Aggregate((a, b) => a + b);
}
```
. . .
**Python**
```{.python code-line-numbers="1-4"}
n = 2
s = "Hello"
def repeated_string(s, n):
return s * n
```
## Types
::: {.incremental}
* Python is a dynamically typed language
* Types are not checked at compile time by the interpreter
* Types *can* be checked before runtime using a linter (e.g. `mypy`)
* Type hints can be used by VS Code to provide auto-completion
:::
. . .
```python
n: int = 2
s: str = "Hello"
def repeated_string(s:str, n:int) -> str:
return s * n
```
## Abstraction
:::: {.columns}
::: {.column}
**Version A**
```python
total = 0.0
for x in values:
total = total +x
```
:::
::: {.column}
**Version B**
```python
total = sum(values)
```
:::
::::
. . .
::: {.incremental}
* Using functions, e.g. `sum()` allows us to operate on a higher level of abstraction.
* Too little abstraction will force you to write many lines of boiler-plate code
* Too much abstraction limits the flexibility
* ✨Find the right level of abstraction!✨
:::
::: {.notes}
* Which version is easiest to understand?
* Which version is easiest to change?
:::
## Collections Abstract Base Classes
```{mermaid}
classDiagram
Container <|-- Collection
Sized <|-- Collection
Iterable <|-- Collection
class Container{
__contains__(self, x)
}
class Sized{
__len__(self)
}
class Iterable{
__iter__(self)
}
```
. . .
::: {.incremental}
* If a class implements `__len__` it is a `Sized` object.
* If a class implements `__contains__` it is a `Container` object.
* If a class implements `__iter__` it is a `Iterable` object.
:::
## Collections Abstract Base Classes {.smaller}
```{.python code-line-numbers="1|2-5|6-9|10-11|12-25"}
>>> a = [1, 2, 3]
>>> 1 in a
True
>>> a.__contains__(1)
True
>>> len(a)
3
>>> a.__len__()
3
>>> for x in a:
... v.append(x)
>>> it = a.__iter__()
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
```
## Collections Abstract Base Classes
```{mermaid}
classDiagram
Container <|-- Collection
Sized <|-- Collection
Iterable <|-- Collection
Collection <|-- Sequence
Collection <|-- Set
Sequence <|-- MutableSequence
Mapping <|-- MutableMapping
Collection <|-- Mapping
MutableSequence <|-- List
Sequence <|-- Tuple
MutableMapping <|-- Dict
```
## Pythonic
If you want your code to be Pythonic, you have to be familiar with these types and their methods.
Dundermethods:
* `__getitem__`
* `__setitem__`
* `__len__`
* `__contains__`
* ...
---
```python
class JavaLikeToolbox:
def __init__(self, tools: Collection[Tool]):
self.tools = tools
def getToolByName(self, name: str) -> Tool:
for tool in self.tools:
if tool.name == name:
return tool
def numberOfTools(self) -> int:
return len(self.tools)
>>> tb = JavaLikeToolbox([Hammer(), Screwdriver()])
>>> tb.getToolByName("hammer")
Hammer()
>>> tb.numberOfTools()
2
```
---
```python
class Toolbox:
def __init__(self, tools: Collection[Tool]):
self._tools = {tool.name: tool for tool in tools}
def __getitem__(self, name: str) -> Tool:
return self._tools[name]
def __len__(self) -> int:
return len(self.tools)
>>> tb = Toolbox([Hammer(), Screwdriver()])
>>> tb["hammer"]
Hammer()
>>> len(tb)
2
```
::: {.notes}
You want your code to be feel like the built-in types.
:::
---
```{.python code-line-numbers="|7-13"}
class SparseMatrix:
def __init__(self, shape, fill_value=0.0, data=None):
self.shape = shape
self._data = data if data is not None else {}
self.fill_value = fill_value
def __setitem__(self, key, value):
i,j = key
self._data[i,j] = float(value)
def __getitem__(self, key) -> float:
i,j = key
return self._data.get((i,j), self.fill_value)
def transpose(self) -> "SparseMatrix":
data = {(j,i) : v for (i,j),v in self._data.items()}
return SparseMatrix(data=data,
shape=self.shape,
fill_value=self.fill_value)
def __repr__(self):
matrix_str = ""
for j in range(self.shape[1]):
for i in range(self.shape[0]):
value = self[i, j]
matrix_str += f"{value:<4}"
matrix_str += "\n"
return matrix_str
```
---
```python
>>> m = SparseMatrix(shape=(2,2), fill_value=0.0)
>>> m
0.0 0.0
0.0 0.0
>>> m[0,1]
0.0
>>> m[0,1] = 1.0
>>> m[1,0] = 2.0
>>> m
0.0 2.0
1.0 0.0
>>> m.transpose()
0.0 1.0
2.0 0.0
```
## Duck typing
::: {.incremental}
* "*If it walks like a duck and quacks like a duck, it's a duck*"
* From the perspective of the caller, it doesn't matter if it is a rubber duck or a real duck.
* The type of the object is **not important**, as long as it has the right methods.
* Python is different than C# or Java, where you would have to create an interface `IToolbox` and implement it for `Toolbox`.
:::
## Duck typing - Example
An example is a Scikit learn transformers
* `fit`
* `transform`
* `fit_transform`
If you want to make a transformer compatible with sklearn, you have to implement these methods.
## Duck typing - Example
```python
class PositiveNumberTransformer:
def fit(self, X, y=None):
# no need to fit (still need to have the method!)
return self
def transform(self, X):
return np.abs(X)
def fit_transform(self, X, y=None):
return self.fit(X, y).transform(X)
```
## Duck typing - Mixins {.smaller}
We can inherit some behavior from `sklearn.base.TransformerMixin`
```{.python code-line-numbers="|1,3,18,19"}
from sklearn.base import TransformerMixin
class RemoveOutliersTransformer(TransformerMixin):
def __init__(self, lower_bound, upper_bound):
self.lower_bound = lower_bound
self.upper_bound = upper_bound
self.lower_ = None
self.upper_ = None
def fit(self, X, y=None):
self.lower_ = np.quantile(X, self.lower_bound)
self.upper_ = np.quantile(X, self.upper_bound)
def transform(self, X):
return np.clip(X, self.lower_, self.upper_)
# def fit_transform(self, X, y=None):
# we get this for free, from TransformerMixin
```
## Let's revisit the (date) Interval
The `Interval` class represent an interval in time.
```{.python code-line-numbers="|6-7|11-14"}
class Interval:
def __init__(self, start, end):
self.start = start
self.end = end
def __contains__(self, x):
return self.start < x < self.end
>>> dr = Interval(date(2020, 1, 1), date(2020, 1, 31))
>>> date(2020,1,15) in dr
True
>>> date(1970,1,1) in dr
False
```
. . .
What if we want to make another type of interval, e.g. a interval of numbers $[1.0, 2.0]$?
## A number interval
```{.python code-line-numbers="9-14"}
class Interval:
def __init__(self, start, end):
self.start = start
self.end = end
def __contains__(self, x):
return self.start < x < self.end
>>> interval = Interval(5, 10)
>>> 8 in interval
True
>>> 12 in interval
False
```
. . .
As long as the `start`, `end` and `x` are comparable, the `Interval` class is a generic class able to handle integers, floats, dates, datetimes, strings ...
## Postel's law
a.k.a. the Robustness principle of software design
1. Be liberal in what you accept
2. Be conservative in what you send
. . .
```python
def process(number: Union[int,str,float]) -> int:
# make sure number is an int from now on
number = int(number)
result = number * 2
return result
```
##
![](images/postel_meme.jpg)
. . .
The consumers of your package (future self), will be grateful if you are not overly restricitive in what types you accept as input.
## Example - Pydantic
```python
from pydantic import BaseModel
from datetime import date
class Sensor(BaseModel):
name: str
voltage: float
install_date: date
location: tuple[float, float]
s1 = Sensor(name="Sensor 1",
voltage=3.3,
install_date=date(2020, 1, 1),
location=(4.42, 51.22))
data = {
"name": "Sensor 1",
"voltage": "3.3",
"install_date": "2020-01-01",
"location": ("4.42", "51.22")
}
s2 = Sensor(**data)
```
## Refactoring
::: {.incremental}
* Refactoring is a way to improve the design of existing code
* Changing a software system in such a way that it **does not alter the external behavior of the code**, yet improves its internal structure
* Refactoring is a way to make code more readable and maintainable
* Housekeeping
:::
## Common refactoring techniques:
* Extract method
* Extract variable
* Rename method
* Rename variable
* Rename class
* Inline method
* Inline variable
* Inline class
## Rename variable
**Before**
```python
n = 0
for v in y:
if v < 0:
n = n + 1
```
. . .
**After**
```python
FREEZING_POINT = 0.0
n_freezing_days = 0
for temp in daily_max_temperatures:
if temp < FREEZING_POINT:
n_freezing_days = n_freezing_days + 1
```
## Extract variable
**Before**
```python
def predict(x):
return min(0.0, 0.5 + 2.0 * min(0,x) + (random.random() - 0.5) / 10.0)
```
. . .
**After**
```python
def predict(x):
scale = 10.0
error = (random.random() - 0.5) / scale)
a = 0.5
b = 2.0
draft = a + b * x + error
return min(0.0, draft)
```
## Extract method
```python
def error(scale):
return (random.random() - 0.5) / scale)
def linear_model(x, *, a=0.0, b=1.0):
return a + b * x
def clip(x, *, min_value=0.0):
return min(min_value, x)
def predict(x):
draft = linear_model(x, a=0.5, b=2.0) + error(scale=10.0)
return clip(draft, min_value=0.)
```
## Inline method
Opposite of extract mehtod.
```{.python code-line-numbers="3"}
def predict(x):
draft = linear_model(x, a=0.5, b=2.0) + error(scale=10.0)
return min(0.0, x)
```
## Composed method
Break up a long method into smaller methods.
---
```python
# get data
os.shutil.copyfile(thisfile, localfile)
df = read_csv(localfile)
# clean data
df.dropna()
df.drop_duplicates()
df[somevar<0.0] = 0.0
# transform data
df.date = pd.to_datetime(df.date) - 86400
# predict
predictions = df.height + df.weight * df.age
```
---
```python
def get_data(filename,...):
...
def clean_data(df):
...
def transform_data(df):
...
def predict(df):
...
def main():
df = get_data("raw_data.csv")
cleaned_data = clean_data(df)
final_data = transform_data(cleaned_data)
predictions = predict(final_data)
```
## Composed method
* Divide your program into methods that perform one identifiable task
* Keep all of the operations in a method at the same level of abstraction.
* This will naturally result in programs with many small methods, each a few lines long.
* When you use Extract method a bunch of times on a method the original method becomes a Composed method.
---
:::: {.columns}
::: {.column}
![](images/refactoring_book.png)
:::
::: {.column}
If you want to learn more about refactoring, I recommend the book "Refactoring: Improving the Design of Existing Code" by Martin Fowler.
:::
::::
## Summary
::: {.incremental}
* OOP is a way to organize your code
* Encapsulation, composition, inheritance, abstraction
* Duck Typing
* Postel's law
* Refactoring
:::