Skip to content

Commit b2a3d55

Browse files
committed
Added type hints to 3-tier structural pattern
1 parent a822f7f commit b2a3d55

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

patterns/structural/3-tier.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,77 @@
33
Separates presentation, application processing, and data management functions.
44
"""
55

6+
from typing import Dict, KeysView, Optional, Type, TypeVar, Union
7+
8+
T = TypeVar("T")
9+
610

711
class Data:
812
""" Data Store Class """
913

1014
products = {
11-
'milk': {'price': 1.50, 'quantity': 10},
12-
'eggs': {'price': 0.20, 'quantity': 100},
13-
'cheese': {'price': 2.00, 'quantity': 10},
15+
"milk": {"price": 1.50, "quantity": 10},
16+
"eggs": {"price": 0.20, "quantity": 100},
17+
"cheese": {"price": 2.00, "quantity": 10},
1418
}
1519

16-
def __get__(self, obj, klas):
20+
def __get__(
21+
self, obj: Optional[T], klas: Type[T]
22+
) -> Dict[str, Dict[str, Dict[str, Union[int, float]]]]:
23+
1724
print("(Fetching from Data Store)")
18-
return {'products': self.products}
25+
return {"products": self.products}
1926

2027

2128
class BusinessLogic:
2229
""" Business logic holding data store instances """
2330

2431
data = Data()
2532

26-
def product_list(self):
27-
return self.data['products'].keys()
33+
def product_list(self) -> KeysView[str]:
34+
return self.data["products"].keys()
2835

29-
def product_information(self, product):
30-
return self.data['products'].get(product, None)
36+
def product_information(
37+
self, product: str
38+
) -> Optional[Dict[str, Union[int, float]]]:
39+
return self.data["products"].get(product, None)
3140

3241

3342
class Ui:
3443
""" UI interaction class """
3544

36-
def __init__(self):
45+
def __init__(self) -> None:
3746
self.business_logic = BusinessLogic()
3847

39-
def get_product_list(self):
40-
print('PRODUCT LIST:')
48+
def get_product_list(self) -> None:
49+
print("PRODUCT LIST:")
4150
for product in self.business_logic.product_list():
4251
print(product)
43-
print('')
52+
print("")
4453

45-
def get_product_information(self, product):
54+
def get_product_information(self, product: str) -> None:
4655
product_info = self.business_logic.product_information(product)
4756
if product_info:
48-
print('PRODUCT INFORMATION:')
57+
print("PRODUCT INFORMATION:")
4958
print(
50-
'Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
51-
product.title(), product_info.get('price', 0), product_info.get('quantity', 0)
52-
)
59+
f"Name: {product.title()}, "
60+
+ f"Price: {product_info.get('price', 0):.2f}, "
61+
+ f"Quantity: {product_info.get('quantity', 0):}"
5362
)
5463
else:
55-
print('That product "{0}" does not exist in the records'.format(product))
64+
print(f"That product '{product}' does not exist in the records")
5665

5766

5867
def main():
5968
ui = Ui()
6069
ui.get_product_list()
61-
ui.get_product_information('cheese')
62-
ui.get_product_information('eggs')
63-
ui.get_product_information('milk')
64-
ui.get_product_information('arepas')
70+
ui.get_product_information("cheese")
71+
ui.get_product_information("eggs")
72+
ui.get_product_information("milk")
73+
ui.get_product_information("arepas")
6574

6675

67-
if __name__ == '__main__':
76+
if __name__ == "__main__":
6877
main()
6978

7079
### OUTPUT ###

0 commit comments

Comments
 (0)