git
for updating numpy
beginners' course material: https://itgit.bs.ptb.de/python-course/python-beginners-course-material
__init__
(constructor)super
1 class Vehicle:
2 def __init__(self):
3 self._capacity = None
4 self._num_passengers = None
5
6 def is_full(self):
7 if self._num_passengers == self._capacity:
8 return True
9 else:
10 return False
11
12 class Car(Vehicle):
13 def __init__(self):
14 super(Car, self).__init__() # cf. in a second ...
15 self._capacity = 5
list
e.g. [1, 2, 4]
dict
e.g. {"name": "Katrin", "age": 35}
tuple
e.g. (1, 4) # point in 2d plane (x, y)-coords
set
e.g. {1, 15, 42}
namedtuple
(next slide)tuples
: namedtuple
1 from collections import namedtuple
2 Person = namedtuple('Person', ["name", "age"])
3 florian = Person(name="Florian", age=40) # equally: florian = Person("Florian", 40)
4 florian.name
5 >>> Florian
6 florian[0]
7 >>> Florian
8 florian.age
9 >>> 40
Doc: https://numpy.org/doc/stable/
1 import numpy as np
2 >>> a = np.arange(10)**3
3 >>> a
4 array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
5 >>> a[2]
6 8
7 >>> a[2:5]
8 array([ 8, 27, 64])
9 # equivalent to a[0:6:2] = 1000;
10 # from start to position 6, exclusive, set every 2nd element to 1000
11 >>> a[:6:2] = 1000
12 >>> a
13 array([1000, 1, 1000, 27, 1000, 125, 216, 343, 512, 729])
14 >>> a[::-1] # reversed a
15 array([ 729, 512, 343, 216, 125, 1000, 27, 1000, 1, 1000])
Goals:
conda
, pip
, etc.my_module.py
class MyClass
def my_add(x, y)
README
that explains the packageA very basic sample package 'my-package' could look like this
```
<Package my-package>
├── mypackage
│ └── __init__.py
├── docs
├── LICENSE
├── README.rst
├── setup.py ( later ... )
└── tests
└── __init__.py
```
Optional: requirements.txt
for development dependencies (or in setup.py
)
pip
)Tasks: Create a package for "bitmap-filter"
git commit
your created package
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system
-- A. Hunt & D.Thomas in The pragmatic Programmer
Why?
Further in a Team: - Gives rise to unmotivated local adaptions of repeated code -> clutter
Thus:
- Already the first <Ctrl>-<v>
usually is the bad one!
- Early (but not premature!) abstractions
1 def check_area(b):
2 if (1 / 2 * 3.14 * b * b) < 0.2:
3 return True
4 elif (1 / 2 * 3.14 * b * b) < 0.8:
5 return False
6 elif (1 / 2 * 3.14 * b * b) < 2.0:
7 return True
8 else:
9 return False
This code is not DRY.
1 class CsvValidation:
2
3 def validate_product(self, product : dict):
4 if 'color' not in product:
5 raise RuntimeError(
6 'Import fail: the product attribute color is missing')
7
8 if 'size' not in product:
9 raise RuntimeError(
10 'Import fail: the product attribute size is missing')
11
12 if 'type' not in product:
13 raise RuntimeError(
14 'Import fail: the product attribute type is missing')
This code is DRY. But there is some code duplication.
1 class CsvValidation:
2
3 def validate_product(self, product : dict):
4 for property in ['color', 'size', 'type']:
5 if property not in product:
6 raise RuntimeError(
7 'Import fail: the product attribute {} is missing'.format(property))
This code is DRY. No duplications - but more difficult to understand! There is a tradeoff.
1 class ProductInterface:
2
3 def display_price(self):
4 raise NotImplementedError(
5 "You forgot to implement the displayPrice method")
6
7 class PlasticDuck(ProductInterface):
8
9 def __init__(self, price):
10 self._price = price
11
12 def display_price(self):
13 print("The price of this plastic"
14 "duck is {} euros!".format(self._price))
15
16 plast_duck = PlasticDuck(2)
17 plast_duck.displayPrice()
Think about the fact that the word 'price' appears 9! times
This code is DRY.
Tasks: Read a bitmap image
width
, height
and byte offset
of pixel array from the input bitmapPillow
packagemain.py
with the test code In object-oriented programming, an interface or protocol type is a data type describing a set of method signatures, the implementations of which may be provided by multiple classes that are otherwise not necessarily related to each other. A class which provides the methods listed in a protocol is said to adopt the protocol, or to implement the interface.
-- Wikipedia article: "Interface (object-oriented programming)"
A simple approach:
1 class Interface(object):
2
3 def method1(self):
4 raise NotImplementedError("this is not implemented") # -> provoke runtime error
5
6 class Concrete(Interface):
7
8 def method1(self):
9 print("method 1 is implemented here")
10 # ...
abc
abc
== "Abstract base class"
A better approach assuring implementation before any execution starts:
1 import abc
2
3 class Interface(object, metaclass=abc.ABCMeta):
4
5 @abc.abstractmethod
6 def method1(self):
7 pass
8
9 class Concrete(Interface):
10
11 def method1(self):
12 print("method 1 is implemented here")
13 # ...
day-1/PTBcolor.bmp
Tasks:
addressable2d.py
Decorators in general:
my_decorator
and use syntax sugar @my_decorator
to call 1 def smart_divide(func):
2 def inner(a, b):
3 print("I am going to divide", a, "and", b)
4 if b == 0:
5 raise ZeroDivisionError("Whoops! cannot divide")
6
7 return func(a, b)
8 return inner
9
10 @smart_divide
11 def divide(a, b):
12 print(a/b)
@staticmethod
get_num_wheels
of a class Car
may be staticget_num_broken_wheels
may not be static 1 class Person:
2 def __init__(self, age):
3 self.age = age
4
5 @staticmethod
6 def age_is_adult(age_in):
7 return age_in > 18
8
9 def is_adult(self):
10 return self.age_is_adult(self.age)
11
12
13 res = Person.age_is_adult(12)
14 print('Is person adult: ', res)
15
16 me = Person(35)
17 print('I am adult: ', me.is_adult())
@classmethod
@classmethod
1 import date
2 class Person():
3 species='homo_sapiens' # This is class variable
4 def __init__(self, age):
5 self.age = age
6
7 @classmethod
8 def create_with_birth_year(cls, birth_year):
9 return cls(date.today().year - birth_year)
10
11 @classmethod
12 def print_species(cls):
13 print('species: {}'.format(cls.species))
14
15 class Tutor(Person):
16 pass
17
18 me = Tutor.create_with_birth_year(1982)
19 print(type(me))
Tasks:
@classmethod
constructor to your Bitmap
class that creates an empty bitmap from input width and height.
The pixel array of correct dimension shall be created and pixels shall be initialized to color "white" @classmethod
constructor to your Bitmap
class that creates a copy from another input instance
of class Bitmap
Thanks!
Table of Contents | t |
---|---|
Exposé | ESC |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide next slide | c |
Notes | 2 |
Help | h |