|
3 | 3 | Separates presentation, application processing, and data management functions. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from typing import Dict, KeysView, Optional, Type, TypeVar, Union |
| 7 | + |
6 | 8 |
|
7 | 9 | class Data: |
8 | 10 | """ Data Store Class """ |
9 | 11 |
|
10 | 12 | 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}, |
14 | 16 | } |
15 | 17 |
|
16 | 18 | def __get__(self, obj, klas): |
| 19 | + |
17 | 20 | print("(Fetching from Data Store)") |
18 | | - return {'products': self.products} |
| 21 | + return {"products": self.products} |
19 | 22 |
|
20 | 23 |
|
21 | 24 | class BusinessLogic: |
22 | 25 | """ Business logic holding data store instances """ |
23 | 26 |
|
24 | 27 | data = Data() |
25 | 28 |
|
26 | | - def product_list(self): |
27 | | - return self.data['products'].keys() |
| 29 | + def product_list(self) -> KeysView[str]: |
| 30 | + return self.data["products"].keys() |
28 | 31 |
|
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) |
31 | 36 |
|
32 | 37 |
|
33 | 38 | class Ui: |
34 | 39 | """ UI interaction class """ |
35 | 40 |
|
36 | | - def __init__(self): |
| 41 | + def __init__(self) -> None: |
37 | 42 | self.business_logic = BusinessLogic() |
38 | 43 |
|
39 | | - def get_product_list(self): |
40 | | - print('PRODUCT LIST:') |
| 44 | + def get_product_list(self) -> None: |
| 45 | + print("PRODUCT LIST:") |
41 | 46 | for product in self.business_logic.product_list(): |
42 | 47 | print(product) |
43 | | - print('') |
| 48 | + print("") |
44 | 49 |
|
45 | | - def get_product_information(self, product): |
| 50 | + def get_product_information(self, product: str) -> None: |
46 | 51 | product_info = self.business_logic.product_information(product) |
47 | 52 | if product_info: |
48 | | - print('PRODUCT INFORMATION:') |
| 53 | + print("PRODUCT INFORMATION:") |
49 | 54 | 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):}" |
53 | 58 | ) |
54 | 59 | 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") |
56 | 61 |
|
57 | 62 |
|
58 | 63 | def main(): |
59 | 64 | ui = Ui() |
60 | 65 | 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") |
65 | 70 |
|
66 | 71 |
|
67 | | -if __name__ == '__main__': |
| 72 | +if __name__ == "__main__": |
68 | 73 | main() |
69 | 74 |
|
70 | 75 | ### OUTPUT ### |
|
0 commit comments