Skip to content

Commit a79d012

Browse files
authored
Merge pull request #330 from rednafi/master
Added type hints to 3-tier structural pattern
2 parents 588f39a + 848013a commit a79d012

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

patterns/structural/3-tier.py

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

6+
from typing import Dict, KeysView, Optional, Type, TypeVar, Union
7+
68

79
class Data:
810
""" Data Store Class """
911

1012
products = {
11-
'milk': {'price': 1.50, 'quantity': 10},
12-
'eggs': {'price': 0.20, 'quantity': 100},
13-
'cheese': {'price': 2.00, 'quantity': 10},
13+
"milk": {"price": 1.50, "quantity": 10},
14+
"eggs": {"price": 0.20, "quantity": 100},
15+
"cheese": {"price": 2.00, "quantity": 10},
1416
}
1517

1618
def __get__(self, obj, klas):
19+
1720
print("(Fetching from Data Store)")
18-
return {'products': self.products}
21+
return {"products": self.products}
1922

2023

2124
class BusinessLogic:
2225
""" Business logic holding data store instances """
2326

2427
data = Data()
2528

26-
def product_list(self):
27-
return self.data['products'].keys()
29+
def product_list(self) -> KeysView[str]:
30+
return self.data["products"].keys()
2831

29-
def product_information(self, product):
30-
return self.data['products'].get(product, None)
32+
def product_information(
33+
self, product: str
34+
) -> Optional[Dict[str, Union[int, float]]]:
35+
return self.data["products"].get(product, None)
3136

3237

3338
class Ui:
3439
""" UI interaction class """
3540

36-
def __init__(self):
41+
def __init__(self) -> None:
3742
self.business_logic = BusinessLogic()
3843

39-
def get_product_list(self):
40-
print('PRODUCT LIST:')
44+
def get_product_list(self) -> None:
45+
print("PRODUCT LIST:")
4146
for product in self.business_logic.product_list():
4247
print(product)
43-
print('')
48+
print("")
4449

45-
def get_product_information(self, product):
50+
def get_product_information(self, product: str) -> None:
4651
product_info = self.business_logic.product_information(product)
4752
if product_info:
48-
print('PRODUCT INFORMATION:')
53+
print("PRODUCT INFORMATION:")
4954
print(
50-
'Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
51-
product.title(), product_info.get('price', 0), product_info.get('quantity', 0)
52-
)
55+
f"Name: {product.title()}, "
56+
+ f"Price: {product_info.get('price', 0):.2f}, "
57+
+ f"Quantity: {product_info.get('quantity', 0):}"
5358
)
5459
else:
55-
print('That product "{0}" does not exist in the records'.format(product))
60+
print(f"That product '{product}' does not exist in the records")
5661

5762

5863
def main():
5964
ui = Ui()
6065
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')
66+
ui.get_product_information("cheese")
67+
ui.get_product_information("eggs")
68+
ui.get_product_information("milk")
69+
ui.get_product_information("arepas")
6570

6671

67-
if __name__ == '__main__':
72+
if __name__ == "__main__":
6873
main()
6974

7075
### OUTPUT ###

0 commit comments

Comments
 (0)