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