Skip to content

Commit f6bc58d

Browse files
committed
Ädded comments and lost types
1 parent 6af5a82 commit f6bc58d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

patterns/structural/mvc.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
class Model(ABC):
12+
"""The Model is the data layer of the application."""
1213
@abstractmethod
1314
def __iter__(self):
1415
pass
@@ -26,6 +27,7 @@ def item_type(self) -> str:
2627

2728

2829
class ProductModel(Model):
30+
"""The Model is the data layer of the application."""
2931
class Price(float):
3032
"""A polymorphic way to pass a float with a particular
3133
__str__ functionality."""
@@ -52,12 +54,13 @@ def get(self, product: str) -> dict:
5254

5355

5456
class View(ABC):
57+
"""The View is the presentation layer of the application."""
5558
@abstractmethod
5659
def show_item_list(self, item_type: str, item_list: dict) -> None:
5760
pass
5861

5962
@abstractmethod
60-
def show_item_information(self, item_type: str, item_name: str, item_info: str) -> None:
63+
def show_item_information(self, item_type: str, item_name: str, item_info: dict) -> None:
6164
"""Will look for item information by iterating over key,value pairs
6265
yielded by item_info.items()"""
6366
pass
@@ -68,17 +71,20 @@ def item_not_found(self, item_type: str, item_name: str) -> None:
6871

6972

7073
class ConsoleView(View):
74+
"""The View is the presentation layer of the application."""
7175
def show_item_list(self, item_type: str, item_list: dict) -> None:
7276
print(item_type.upper() + " LIST:")
7377
for item in item_list:
7478
print(item)
7579
print("")
7680

7781
@staticmethod
78-
def capitalizer(string) -> str:
82+
def capitalizer(string: str) -> str:
83+
"""Capitalizes the first letter of a string and lowercases the rest."""
7984
return string[0].upper() + string[1:].lower()
8085

81-
def show_item_information(self, item_type, item_name, item_info) -> None:
86+
def show_item_information(self, item_type: str, item_name: str, item_info: dict) -> None:
87+
"""Will look for item information by iterating over key,value pairs"""
8288
print(item_type.upper() + " INFORMATION:")
8389
printout = "Name: %s" % item_name
8490
for key, value in item_info.items():
@@ -91,9 +97,10 @@ def item_not_found(self, item_type: str, item_name: str) -> None:
9197

9298

9399
class Controller:
94-
def __init__(self, model_class, view_class) -> None:
95-
self.model = model_class
96-
self.view = view_class
100+
"""The Controller is the intermediary between the Model and the View."""
101+
def __init__(self, model_class: Model, view_class: View) -> None:
102+
self.model: Model = model_class
103+
self.view: View = view_class
97104

98105
def show_items(self) -> None:
99106
items = list(self.model)
@@ -106,22 +113,23 @@ def show_item_information(self, item_name: str) -> None:
106113
:param str item_name: the name of the {item_type} item to show information about
107114
"""
108115
try:
109-
item_info = self.model.get(item_name)
116+
item_info: str = self.model.get(item_name)
110117
except Exception:
111-
item_type = self.model.item_type
118+
item_type: str = self.model.item_type
112119
self.view.item_not_found(item_type, item_name)
113120
else:
114-
item_type = self.model.item_type
121+
item_type: str = self.model.item_type
115122
self.view.show_item_information(item_type, item_name, item_info)
116123

117124

118125
class Router:
126+
"""The Router is the entry point of the application."""
119127
def __init__(self):
120128
self.routes = {}
121129

122-
def register(self, path: str, controller_class, model_class, view_class) -> None:
123-
model_instance = model_class()
124-
view_instance = view_class()
130+
def register(self, path: str, controller_class: Controller, model_class: Model, view_class: View) -> None:
131+
model_instance: Model = model_class()
132+
view_instance: View = view_class()
125133
self.routes[path] = controller_class(model_instance, view_instance)
126134

127135
def resolve(self, path: str) -> Controller:

0 commit comments

Comments
 (0)