Skip to content
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
29 changes: 17 additions & 12 deletions patterns/creational/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,23 @@ def construct_building(cls):
return building


# Client
if __name__ == "__main__":
house = House()
print(house)
flat = Flat()
print(flat)
def main():
"""
>>> house = House()
>>> house
Floor: One | Size: Big

>>> flat = Flat()
>>> flat
Floor: More than One | Size: Small

# Using an external constructor function:
complex_house = construct_building(ComplexHouse)
print(complex_house)
>>> complex_house = construct_building(ComplexHouse)
>>> complex_house
Floor: One | Size: Big and fancy
"""

### OUTPUT ###
# Floor: One | Size: Big
# Floor: More than One | Size: Small
# Floor: One | Size: Big and fancy

if __name__ == "__main__":
import doctest
doctest.testmod()
28 changes: 15 additions & 13 deletions patterns/creational/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ def unregister_object(self, name):


def main():
dispatcher = PrototypeDispatcher()
prototype = Prototype()
"""
>>> dispatcher = PrototypeDispatcher()
>>> prototype = Prototype()

d = prototype.clone()
a = prototype.clone(value='a-value', category='a')
b = prototype.clone(value='b-value', is_checked=True)
dispatcher.register_object('objecta', a)
dispatcher.register_object('objectb', b)
dispatcher.register_object('default', d)
print([{n: p.value} for n, p in dispatcher.get_objects().items()])
>>> d = prototype.clone()
>>> a = prototype.clone(value='a-value', category='a')
>>> b = prototype.clone(value='b-value', is_checked=True)
>>> dispatcher.register_object('objecta', a)
>>> dispatcher.register_object('objectb', b)
>>> dispatcher.register_object('default', d)

>>> [{n: p.value} for n, p in dispatcher.get_objects().items()]
[{'objecta': 'a-value'}, {'objectb': 'b-value'}, {'default': 'default'}]
"""

if __name__ == '__main__':
main()

### OUTPUT ###
# [{'objectb': 'b-value'}, {'default': 'default'}, {'objecta': 'a-value'}]
if __name__ == '__main__':
import doctest
doctest.testmod()
27 changes: 16 additions & 11 deletions patterns/other/graph_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ def find_shortest_path(self, start, end, path=None):
return shortest


# example of graph usage
graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']}
def main():
"""
# example of graph usage
>>> graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']}

# initialization of new graph search object
graph1 = GraphSearch(graph)
# initialization of new graph search object
>>> graph1 = GraphSearch(graph)

>>> print(graph1.find_path('A', 'D'))
['A', 'B', 'C', 'D']
>>> print(graph1.find_all_path('A', 'D'))
[['A', 'B', 'C', 'D'], ['A', 'B', 'D'], ['A', 'C', 'D']]
>>> print(graph1.find_shortest_path('A', 'D'))
['A', 'B', 'D']
"""

print(graph1.find_path('A', 'D'))
print(graph1.find_all_path('A', 'D'))
print(graph1.find_shortest_path('A', 'D'))

### OUTPUT ###
# ['A', 'B', 'C', 'D']
# [['A', 'B', 'C', 'D'], ['A', 'B', 'D'], ['A', 'C', 'D']]
# ['A', 'B', 'D']
if __name__ == "__main__":
import doctest
doctest.testmod()
19 changes: 10 additions & 9 deletions patterns/structural/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ def scale(self, pct):


def main():
shapes = (CircleShape(1, 2, 3, DrawingAPI1()), CircleShape(5, 7, 11, DrawingAPI2()))
"""
>>> shapes = (CircleShape(1, 2, 3, DrawingAPI1()), CircleShape(5, 7, 11, DrawingAPI2()))

for shape in shapes:
shape.scale(2.5)
shape.draw()
>>> for shape in shapes:
... shape.scale(2.5)
... shape.draw()
API1.circle at 1:2 radius 7.5
API2.circle at 5:7 radius 27.5
"""


if __name__ == '__main__':
main()

### OUTPUT ###
# API1.circle at 1:2 radius 7.5
# API2.circle at 5:7 radius 27.5
import doctest
doctest.testmod()