Skip to content

Commit af1da2c

Browse files
authored
Merge pull request #5 from gjbex/development
Add factory pattern example
2 parents cc33e9c + 711050e commit af1da2c

20 files changed

+353
-8
lines changed

source-code/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ was used to develop it.
2020
programming in Python.
2121
1. `operators-functools`: illustrates some applications of the `operator`
2222
and `functools` modules in Python's standard library.
23-
1. `pyparsing`: illustration of unit testing on real world code.
2423
1. `typing`: illustrates how to use type annotation in Python, and
2524
demonstrates `mypy`.
2625
1. `testing`: illustrates writing unit tests with `unittest` and

source-code/descriptors/typed_property.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python
22

3+
import sys
34

4-
class TypedProperty(object):
5+
6+
class TypedProperty():
57

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

1416
def __set__(self, instance, value):
1517
if not isinstance(value, self._type):
16-
raise TypeError('value {0} is not of {1}'.format(value,
17-
self._type))
18+
raise TypeError(f'value {value} is not of type {self._type}')
1819
setattr(instance, self._name, value)
1920

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

3839
def __str__(self):
39-
return '{0}\n {1}, {2}'.format(self.title, self.author, self.year)
40+
return f'{self.title}\n {self.author}, {self.year}'
4041

4142

4243
if __name__ == '__main__':
43-
import sys
44-
4544
book1 = Book()
4645
print('showing defaults:')
4746
print(str(book1) + '\n')
@@ -54,6 +53,6 @@ def __str__(self):
5453
try:
5554
book3 = Book(1984, 'George Orwell', 1948)
5655
except TypeError as error:
57-
sys.stderr.write('### error: {0}\n'.format(error))
56+
print(f'### error: {error}', file=sys.stderr)
5857
sys.exit(1)
5958
sys.exit(0)

source-code/design-patters/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ Code to illustrate some design patterns in Python.
66

77
1. `decorator_design_pattern.ipynb`: notebook that illustrate how decorators
88
can be used to change the behaviour of objects.
9+
1. `factory_design_pattern.ipynb`: notebook illustrating how a factory class
10+
can be used to conveniently construct many objects with the same properties.

source-code/design-patters/factory_design_pattern.ipynb

Lines changed: 344 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)