Uh oh!
There was an error while loading. Please reload this page.
forked from faif/python-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmvc.py
More file actions
Latest commit
149 lines (114 loc) · 3.71 KB
/
Copy pathmvc.py
File metadata and controls
149 lines (114 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
*TL;DR
Separates data in GUIs from the ways it is presented, and accepted.
"""
fromabcimportABC, abstractmethod
classModel(ABC):
@abstractmethod
def__iter__(self):
pass
@abstractmethod
defget(self, item):
"""Returns an object with a .items() call method
that iterates over key,value pairs of its information."""
pass
@property
@abstractmethod
defitem_type(self):
pass
classProductModel(Model):
classPrice(float):
"""A polymorphic way to pass a float with a particular
__str__ functionality."""
def__str__(self):
return"{:.2f}".format(self)
products= {
"milk": {"price": Price(1.50), "quantity": 10},
"eggs": {"price": Price(0.20), "quantity": 100},
"cheese": {"price": Price(2.00), "quantity": 10},
}
item_type="product"
def__iter__(self):
foriteminself.products:
yielditem
defget(self, product):
try:
returnself.products[product]
exceptKeyErrorase:
raiseKeyError(str(e) +" not in the model's item list.")
classView(ABC):
@abstractmethod
defshow_item_list(self, item_type, item_list):
pass
@abstractmethod
defshow_item_information(self, item_type, item_name, item_info):
"""Will look for item information by iterating over key,value pairs
yielded by item_info.items()"""
pass
@abstractmethod
defitem_not_found(self, item_type, item_name):
pass
classConsoleView(View):
defshow_item_list(self, item_type, item_list):
print(item_type.upper() +" LIST:")
foriteminitem_list:
print(item)
print("")
@staticmethod
defcapitalizer(string):
returnstring[0].upper() +string[1:].lower()
defshow_item_information(self, item_type, item_name, item_info):
print(item_type.upper() +" INFORMATION:")
printout="Name: %s"%item_name
forkey, valueinitem_info.items():
printout+=", "+self.capitalizer(str(key)) +": "+str(value)
printout+="\n"
print(printout)
defitem_not_found(self, item_type, item_name):
print('That {} "{}" does not exist in the records'.format(item_type, item_name))
classController:
def__init__(self, model, view):
self.model=model
self.view=view
defshow_items(self):
items=list(self.model)
item_type=self.model.item_type
self.view.show_item_list(item_type, items)
defshow_item_information(self, item_name):
try:
item_info=self.model.get(item_name)
exceptException:
item_type=self.model.item_type
self.view.item_not_found(item_type, item_name)
else:
item_type=self.model.item_type
self.view.show_item_information(item_type, item_name, item_info)
defmain():
"""
>>> model = ProductModel()
>>> view = ConsoleView()
>>> controller = Controller(model, view)
>>> controller.show_items()
PRODUCT LIST:
milk
eggs
cheese
<BLANKLINE>
>>> controller.show_item_information("cheese")
PRODUCT INFORMATION:
Name: cheese, Price: 2.00, Quantity: 10
<BLANKLINE>
>>> controller.show_item_information("eggs")
PRODUCT INFORMATION:
Name: eggs, Price: 0.20, Quantity: 100
<BLANKLINE>
>>> controller.show_item_information("milk")
PRODUCT INFORMATION:
Name: milk, Price: 1.50, Quantity: 10
<BLANKLINE>
>>> controller.show_item_information("arepas")
That product "arepas" does not exist in the records
"""
if__name__=="__main__":
importdoctest
doctest.testmod()