Skip to content

Commit 3b58565

Browse files
committed
Added typing
1 parent ab82cbe commit 3b58565

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

patterns/structural/mvc.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def __iter__(self):
1414
pass
1515

1616
@abstractmethod
17-
def get(self, item):
17+
def get(self, item: str) -> dict:
1818
"""Returns an object with a .items() call method
1919
that iterates over key,value pairs of its information."""
2020
pass
2121

2222
@property
2323
@abstractmethod
24-
def item_type(self):
24+
def item_type(self) -> str:
2525
pass
2626

2727

@@ -30,7 +30,7 @@ class Price(float):
3030
"""A polymorphic way to pass a float with a particular
3131
__str__ functionality."""
3232

33-
def __str__(self):
33+
def __str__(self) -> str:
3434
return f"{self:.2f}"
3535

3636
products = {
@@ -44,7 +44,7 @@ def __str__(self):
4444
def __iter__(self):
4545
yield from self.products
4646

47-
def get(self, product):
47+
def get(self, product: str) -> dict:
4848
try:
4949
return self.products[product]
5050
except KeyError as e:
@@ -53,40 +53,40 @@ def get(self, product):
5353

5454
class View(ABC):
5555
@abstractmethod
56-
def show_item_list(self, item_type, item_list):
56+
def show_item_list(self, item_type: str, item_list: dict) -> None:
5757
pass
5858

5959
@abstractmethod
60-
def show_item_information(self, item_type, item_name, item_info):
60+
def show_item_information(self, item_type: str, item_name: str, item_info: str) -> None:
6161
"""Will look for item information by iterating over key,value pairs
6262
yielded by item_info.items()"""
6363
pass
6464

6565
@abstractmethod
66-
def item_not_found(self, item_type, item_name):
66+
def item_not_found(self, item_type, item_name) -> None:
6767
pass
6868

6969

7070
class ConsoleView(View):
71-
def show_item_list(self, item_type, item_list):
71+
def show_item_list(self, item_type, item_list) -> None:
7272
print(item_type.upper() + " LIST:")
7373
for item in item_list:
7474
print(item)
7575
print("")
7676

7777
@staticmethod
78-
def capitalizer(string):
78+
def capitalizer(string) -> str:
7979
return string[0].upper() + string[1:].lower()
8080

81-
def show_item_information(self, item_type, item_name, item_info):
81+
def show_item_information(self, item_type, item_name, item_info) -> None:
8282
print(item_type.upper() + " INFORMATION:")
8383
printout = "Name: %s" % item_name
8484
for key, value in item_info.items():
8585
printout += ", " + self.capitalizer(str(key)) + ": " + str(value)
8686
printout += "\n"
8787
print(printout)
8888

89-
def item_not_found(self, item_type, item_name):
89+
def item_not_found(self, item_type, item_name) -> None:
9090
print(f'That {item_type} "{item_name}" does not exist in the records')
9191

9292

@@ -95,12 +95,12 @@ def __init__(self, model, view):
9595
self.model = model
9696
self.view = view
9797

98-
def show_items(self):
98+
def show_items(self) -> None:
9999
items = list(self.model)
100100
item_type = self.model.item_type
101101
self.view.show_item_list(item_type, items)
102102

103-
def show_item_information(self, item_name):
103+
def show_item_information(self, item_name) -> None:
104104
"""
105105
Show information about a {item_type} item.
106106
:param str item_name: the name of the {item_type} item to show information about
@@ -117,16 +117,16 @@ def show_item_information(self, item_name):
117117

118118
class Router:
119119
def __init__(self):
120-
self.routes = {}
120+
self.routes: dict = {}
121121

122-
def register(self, path, controller, model, view):
123-
model = model()
124-
view = view()
122+
def register(self, path: str, controller: object, model: object, view: object) -> None:
123+
model: object = model()
124+
view: object = view()
125125
self.routes[path] = controller(model, view)
126126

127-
def resolve(self, path):
127+
def resolve(self, path) -> Controller:
128128
if self.routes.get(path):
129-
controller = self.routes[path]
129+
controller: object = self.routes[path]
130130
return controller
131131
else:
132132
return None
@@ -166,12 +166,12 @@ def main():
166166

167167

168168
if __name__ == "__main__":
169-
router = Router()
169+
router: object = Router()
170170
router.register("products", Controller, ProductModel, ConsoleView)
171-
controller = router.resolve(argv[1])
171+
controller: object = router.resolve(argv[1])
172172

173-
command = str(argv[2]) if len(argv) > 2 else None
174-
args = ' '.join(map(str, argv[3:])) if len(argv) > 3 else None
173+
command: str = str(argv[2]) if len(argv) > 2 else None
174+
args: str = ' '.join(map(str, argv[3:])) if len(argv) > 3 else None
175175

176176
if hasattr(controller, command):
177177
command = getattr(controller, command)

0 commit comments

Comments
 (0)