Skip to content

Commit 2d89d5c

Browse files
committed
Added type hints to composite pattern
2 parents 985822a + 4be7de7 commit 2d89d5c

File tree

6 files changed

+147
-117
lines changed

6 files changed

+147
-117
lines changed

patterns/creational/pool.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,34 @@ def __del__(self):
5151

5252

5353
def main():
54-
import queue
54+
"""
55+
>>> import queue
5556
56-
def test_object(queue):
57-
pool = ObjectPool(queue, True)
58-
print('Inside func: {}'.format(pool.item))
57+
>>> def test_object(queue):
58+
... pool = ObjectPool(queue, True)
59+
... print('Inside func: {}'.format(pool.item))
5960
60-
sample_queue = queue.Queue()
61+
>>> sample_queue = queue.Queue()
6162
62-
sample_queue.put('yam')
63-
with ObjectPool(sample_queue) as obj:
64-
print('Inside with: {}'.format(obj))
65-
print('Outside with: {}'.format(sample_queue.get()))
63+
>>> sample_queue.put('yam')
64+
>>> with ObjectPool(sample_queue) as obj:
65+
... print('Inside with: {}'.format(obj))
66+
Inside with: yam
6667
67-
sample_queue.put('sam')
68-
test_object(sample_queue)
69-
print('Outside func: {}'.format(sample_queue.get()))
68+
>>> print('Outside with: {}'.format(sample_queue.get()))
69+
Outside with: yam
70+
71+
>>> sample_queue.put('sam')
72+
>>> test_object(sample_queue)
73+
Inside func: sam
74+
75+
>>> print('Outside func: {}'.format(sample_queue.get()))
76+
Outside func: sam
7077
7178
if not sample_queue.empty():
7279
print(sample_queue.get())
73-
80+
"""
7481

7582
if __name__ == '__main__':
76-
main()
77-
78-
### OUTPUT ###
79-
# Inside with: yam
80-
# Outside with: yam
81-
# Inside func: sam
82-
# Outside func: sam
83+
import doctest
84+
doctest.testmod()

patterns/structural/3-tier.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,36 @@ def get_product_information(self, product: str) -> None:
6161

6262

6363
def main():
64-
ui = Ui()
65-
ui.get_product_list()
66-
ui.get_product_information("cheese")
67-
ui.get_product_information("eggs")
68-
ui.get_product_information("milk")
69-
ui.get_product_information("arepas")
70-
64+
"""
65+
>>> ui = Ui()
66+
>>> ui.get_product_list()
67+
PRODUCT LIST:
68+
(Fetching from Data Store)
69+
milk
70+
eggs
71+
cheese
72+
<BLANKLINE>
73+
74+
>>> ui.get_product_information("cheese")
75+
(Fetching from Data Store)
76+
PRODUCT INFORMATION:
77+
Name: Cheese, Price: 2.00, Quantity: 10
78+
79+
>>> ui.get_product_information("eggs")
80+
(Fetching from Data Store)
81+
PRODUCT INFORMATION:
82+
Name: Eggs, Price: 0.20, Quantity: 100
83+
84+
>>> ui.get_product_information("milk")
85+
(Fetching from Data Store)
86+
PRODUCT INFORMATION:
87+
Name: Milk, Price: 1.50, Quantity: 10
88+
89+
>>> ui.get_product_information("arepas")
90+
(Fetching from Data Store)
91+
That product 'arepas' does not exist in the records
92+
"""
7193

7294
if __name__ == "__main__":
73-
main()
74-
75-
### OUTPUT ###
76-
# PRODUCT LIST:
77-
# (Fetching from Data Store)
78-
# cheese
79-
# eggs
80-
# milk
81-
#
82-
# (Fetching from Data Store)
83-
# PRODUCT INFORMATION:
84-
# Name: Cheese, Price: 2.00, Quantity: 10
85-
# (Fetching from Data Store)
86-
# PRODUCT INFORMATION:
87-
# Name: Eggs, Price: 0.20, Quantity: 100
88-
# (Fetching from Data Store)
89-
# PRODUCT INFORMATION:
90-
# Name: Milk, Price: 1.50, Quantity: 10
91-
# (Fetching from Data Store)
92-
# That product "arepas" does not exist in the records
95+
import doctest
96+
doctest.testmod()

patterns/structural/composite.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,34 @@ def render(self) -> None:
5959
print(f"Ellipse: {self.name}")
6060

6161

62-
if __name__ == "__main__":
63-
ellipse1 = Ellipse("1")
64-
ellipse2 = Ellipse("2")
65-
ellipse3 = Ellipse("3")
66-
ellipse4 = Ellipse("4")
62+
def main():
63+
"""
64+
>>> ellipse1 = Ellipse("1")
65+
>>> ellipse2 = Ellipse("2")
66+
>>> ellipse3 = Ellipse("3")
67+
>>> ellipse4 = Ellipse("4")
68+
69+
>>> graphic1 = CompositeGraphic()
70+
>>> graphic2 = CompositeGraphic()
6771
68-
graphic1 = CompositeGraphic()
69-
graphic2 = CompositeGraphic()
72+
>>> graphic1.add(ellipse1)
73+
>>> graphic1.add(ellipse2)
74+
>>> graphic1.add(ellipse3)
75+
>>> graphic2.add(ellipse4)
7076
71-
graphic1.add(ellipse1)
72-
graphic1.add(ellipse2)
73-
graphic1.add(ellipse3)
74-
graphic2.add(ellipse4)
77+
>>> graphic = CompositeGraphic()
7578
76-
graphic = CompositeGraphic()
79+
>>> graphic.add(graphic1)
80+
>>> graphic.add(graphic2)
7781
78-
graphic.add(graphic1)
79-
graphic.add(graphic2)
82+
>>> graphic.render()
83+
Ellipse: 1
84+
Ellipse: 2
85+
Ellipse: 3
86+
Ellipse: 4
87+
"""
8088

81-
graphic.render()
8289

83-
### OUTPUT ###
84-
# Ellipse: 1
85-
# Ellipse: 2
86-
# Ellipse: 3
87-
# Ellipse: 4
90+
if __name__ == "__main__":
91+
import doctest
92+
doctest.testmod()

patterns/structural/decorator.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ def render(self):
5555
return "<i>{}</i>".format(self._wrapped.render())
5656

5757

58+
def main():
59+
"""
60+
>>> simple_hello = TextTag("hello, world!")
61+
>>> special_hello = ItalicWrapper(BoldWrapper(simple_hello))
62+
63+
>>> print("before:", simple_hello.render())
64+
before: hello, world!
65+
66+
>>> print("after:", special_hello.render())
67+
after: <i><b>hello, world!</b></i>
68+
"""
69+
5870
if __name__ == '__main__':
59-
simple_hello = TextTag("hello, world!")
60-
special_hello = ItalicWrapper(BoldWrapper(simple_hello))
61-
print("before:", simple_hello.render())
62-
print("after:", special_hello.render())
63-
64-
### OUTPUT ###
65-
# before: hello, world!
66-
# after: <i><b>hello, world!</b></i>
71+
import doctest
72+
doctest.testmod()

patterns/structural/front_controller.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,23 @@ def __init__(self, request):
5858
self.type = self.tablet_type
5959

6060

61-
if __name__ == '__main__':
62-
front_controller = RequestController()
63-
front_controller.dispatch_request(Request('mobile'))
64-
front_controller.dispatch_request(Request('tablet'))
65-
66-
front_controller.dispatch_request(Request('desktop'))
67-
front_controller.dispatch_request('mobile')
68-
69-
70-
### OUTPUT ###
71-
# Displaying mobile index page
72-
# Displaying tablet index page
73-
# cant dispatch the request
74-
# request must be a Request object
61+
def main():
62+
"""
63+
>>> front_controller = RequestController()
64+
65+
>>> front_controller.dispatch_request(Request('mobile'))
66+
Displaying mobile index page
67+
68+
>>> front_controller.dispatch_request(Request('tablet'))
69+
Displaying tablet index page
70+
71+
>>> front_controller.dispatch_request(Request('desktop'))
72+
cant dispatch the request
73+
74+
>>> front_controller.dispatch_request('mobile')
75+
request must be a Request object
76+
"""
77+
78+
if __name__ == "__main__":
79+
import doctest
80+
doctest.testmod()

patterns/structural/mvc.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,38 @@ def show_item_information(self, item_name):
110110
self.view.show_item_information(item_type, item_name, item_info)
111111

112112

113-
if __name__ == "__main__":
114-
115-
model = ProductModel()
116-
view = ConsoleView()
117-
controller = Controller(model, view)
118-
controller.show_items()
119-
controller.show_item_information("cheese")
120-
controller.show_item_information("eggs")
121-
controller.show_item_information("milk")
122-
controller.show_item_information("arepas")
123-
124-
125-
### OUTPUT ###
126-
# PRODUCT LIST:
127-
# cheese
128-
# eggs
129-
# milk
130-
#
131-
# PRODUCT INFORMATION:
132-
# Name: Cheese, Price: 2.00, Quantity: 10
133-
#
134-
# PRODUCT INFORMATION:
135-
# Name: Eggs, Price: 0.20, Quantity: 100
136-
#
137-
# PRODUCT INFORMATION:
138-
# Name: Milk, Price: 1.50, Quantity: 10
139-
#
140-
# That product "arepas" does not exist in the records
113+
def main():
114+
"""
115+
>>> model = ProductModel()
116+
>>> view = ConsoleView()
117+
>>> controller = Controller(model, view)
118+
119+
>>> controller.show_items()
120+
PRODUCT LIST:
121+
milk
122+
eggs
123+
cheese
124+
<BLANKLINE>
125+
126+
>>> controller.show_item_information("cheese")
127+
PRODUCT INFORMATION:
128+
Name: cheese, Price: 2.00, Quantity: 10
129+
<BLANKLINE>
130+
131+
>>> controller.show_item_information("eggs")
132+
PRODUCT INFORMATION:
133+
Name: eggs, Price: 0.20, Quantity: 100
134+
<BLANKLINE>
135+
136+
>>> controller.show_item_information("milk")
137+
PRODUCT INFORMATION:
138+
Name: milk, Price: 1.50, Quantity: 10
139+
<BLANKLINE>
140+
141+
>>> controller.show_item_information("arepas")
142+
That product "arepas" does not exist in the records
143+
"""
144+
145+
if __name__ == '__main__':
146+
import doctest
147+
doctest.testmod()

0 commit comments

Comments
 (0)