From 93df74d563649053ec92809c12868070df9f504c Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Fri, 15 Nov 2019 11:07:35 +0200 Subject: [PATCH 1/4] Add doctest for graph_search --- patterns/other/graph_search.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/patterns/other/graph_search.py b/patterns/other/graph_search.py index 6970092d..968e4342 100644 --- a/patterns/other/graph_search.py +++ b/patterns/other/graph_search.py @@ -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() From e498b667704581d9d4791061b596afb57220ee95 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Fri, 15 Nov 2019 11:11:53 +0200 Subject: [PATCH 2/4] Add doctest for prototype --- patterns/creational/prototype.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/patterns/creational/prototype.py b/patterns/creational/prototype.py index b82f1e1f..690a6789 100644 --- a/patterns/creational/prototype.py +++ b/patterns/creational/prototype.py @@ -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() From 2d4a555dfdb342a152e14f07ce69c4a41ff8f01c Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Fri, 15 Nov 2019 11:26:20 +0200 Subject: [PATCH 3/4] Add doctest for builder --- patterns/creational/builder.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/patterns/creational/builder.py b/patterns/creational/builder.py index f73a9665..73f7d2fc 100644 --- a/patterns/creational/builder.py +++ b/patterns/creational/builder.py @@ -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() From b62a0a860bf8f64667b606435f52a4f432b17270 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Fri, 15 Nov 2019 11:31:21 +0200 Subject: [PATCH 4/4] Add doctest for bridge --- patterns/structural/bridge.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/patterns/structural/bridge.py b/patterns/structural/bridge.py index 8a9f3c56..64b4b422 100644 --- a/patterns/structural/bridge.py +++ b/patterns/structural/bridge.py @@ -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()