Skip to content

Add factory pattern example #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ For information on the training, see the website
develop the slides and illustrate concepts.
1. [`environment.yml`](environment.yml): conda environment file intended to be
cross-platform.
1. [`python_software_engineering_linux64_conda_specs.txt`](python_software_engineering_linux64_conda_specs.txt):
conda environment specification file specific for 64-bit Linux to precisely
reproduce the environment on which the code was developed.
1. [`python_software_engineering_linux64_conda_specs.txt`](python_software_engineering_linux64_conda_specs.txt):
conda environment specification file specific for 64-bit Linux to precisely
reproduce the environment on which the code was developed.
1. [License](LICENSE): license information for the material in this repository.
1. [Contributing](CONTRIBUTING.md): information on how to contribute to this
repository.
Expand Down
1 change: 0 additions & 1 deletion source-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ was used to develop it.
programming in Python.
1. `operators-functools`: illustrates some applications of the `operator`
and `functools` modules in Python's standard library.
1. `pyparsing`: illustration of unit testing on real world code.
1. `typing`: illustrates how to use type annotation in Python, and
demonstrates `mypy`.
1. `testing`: illustrates writing unit tests with `unittest` and
Expand Down
13 changes: 6 additions & 7 deletions source-code/descriptors/typed_property.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python

import sys

class TypedProperty(object):

class TypedProperty():

def __init__(self, name, type, default=None):
self._name = '-' + name
Expand All @@ -13,8 +15,7 @@ def __get__(self, instance, cls):

def __set__(self, instance, value):
if not isinstance(value, self._type):
raise TypeError('value {0} is not of {1}'.format(value,
self._type))
raise TypeError(f'value {value} is not of type {self._type}')
setattr(instance, self._name, value)

def __delete__(self, instance, cls):
Expand All @@ -36,12 +37,10 @@ def __init__(self, title=None, author=None, year=None):
self.year = year

def __str__(self):
return '{0}\n {1}, {2}'.format(self.title, self.author, self.year)
return f'{self.title}\n {self.author}, {self.year}'


if __name__ == '__main__':
import sys

book1 = Book()
print('showing defaults:')
print(str(book1) + '\n')
Expand All @@ -54,6 +53,6 @@ def __str__(self):
try:
book3 = Book(1984, 'George Orwell', 1948)
except TypeError as error:
sys.stderr.write('### error: {0}\n'.format(error))
print(f'### error: {error}', file=sys.stderr)
sys.exit(1)
sys.exit(0)
2 changes: 2 additions & 0 deletions source-code/design-patters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ Code to illustrate some design patterns in Python.

1. `decorator_design_pattern.ipynb`: notebook that illustrate how decorators
can be used to change the behaviour of objects.
1. `factory_design_pattern.ipynb`: notebook illustrating how a factory class
can be used to conveniently construct many objects with the same properties.
344 changes: 344 additions & 0 deletions source-code/design-patters/factory_design_pattern.ipynb

Large diffs are not rendered by default.

59 changes: 26 additions & 33 deletions source-code/object-orientation/avoiding_class_hierarchies.ipynb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Avoiding class hierarchies"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -23,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -34,7 +27,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Duck typing"
"# Duck typing"
]
},
{
Expand All @@ -53,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -71,7 +64,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -96,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -112,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -140,14 +133,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"An alternative way to implement this, and to avoid "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mix-in"
"# Mix-in"
]
},
{
Expand All @@ -159,7 +145,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -176,12 +162,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Duck` and `Timer` classes now inherit from `SoundMaker`, but"
"The `Duck`, `Timer` and `Dog` classes now inherit from `SoundMaker`, but `Dog` doesn't define its sound attribute."
]
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -196,7 +182,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -211,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -225,7 +211,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -234,7 +220,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -251,19 +237,26 @@
" print(f'{type(item)} says {item.make_sound()}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the `Dog` has no `_sound`, the mix-in method raises an exception."
]
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"<ipython-input-60-1c825f3b7991>\", line 3, in <module>\n",
" File \"<ipython-input-12-1c825f3b7991>\", line 3, in <module>\n",
" print(dog.make_sound())\n",
" File \"<ipython-input-52-918cd6c589ba>\", line 7, in make_sound\n",
" File \"<ipython-input-6-918cd6c589ba>\", line 7, in make_sound\n",
" raise ValueError(f'{type(self)} does not make sound')\n",
"ValueError: <class '__main__.Dog'> does not make sound\n"
]
Expand Down Expand Up @@ -294,9 +287,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions source-code/testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ versatile.
testing per se. It is mainly intended for contract-based programming
and one can prevent code to be emitted for asserts by using the
`-O` optimizatino flag when running the code.
1. `Newick`: illustration of unit testing on real world code.