33Separates data in GUIs from the ways it is presented, and accepted.
44"""
55
6+ from abc import ABC , abstractmethod
67
7- class Model :
8+
9+ class Model (ABC ):
10+ @abstractmethod
811 def __iter__ (self ):
9- raise NotImplementedError
12+ pass
1013
14+ @abstractmethod
1115 def get (self , item ):
1216 """Returns an object with a .items() call method
1317 that iterates over key,value pairs of its information."""
14- raise NotImplementedError
18+ pass
1519
1620 @property
21+ @abstractmethod
1722 def item_type (self ):
18- raise NotImplementedError
23+ pass
1924
2025
2126class ProductModel (Model ):
@@ -27,12 +32,12 @@ def __str__(self):
2732 return "{:.2f}" .format (self )
2833
2934 products = {
30- ' milk' : {' price' : Price (1.50 ), ' quantity' : 10 },
31- ' eggs' : {' price' : Price (0.20 ), ' quantity' : 100 },
32- ' cheese' : {' price' : Price (2.00 ), ' quantity' : 10 },
35+ " milk" : {" price" : Price (1.50 ), " quantity" : 10 },
36+ " eggs" : {" price" : Price (0.20 ), " quantity" : 100 },
37+ " cheese" : {" price" : Price (2.00 ), " quantity" : 10 },
3338 }
3439
35- item_type = ' product'
40+ item_type = " product"
3641
3742 def __iter__ (self ):
3843 for item in self .products :
@@ -45,36 +50,39 @@ def get(self, product):
4550 raise KeyError (str (e ) + " not in the model's item list." )
4651
4752
48- class View :
53+ class View (ABC ):
54+ @abstractmethod
4955 def show_item_list (self , item_type , item_list ):
50- raise NotImplementedError
56+ pass
5157
58+ @abstractmethod
5259 def show_item_information (self , item_type , item_name , item_info ):
5360 """Will look for item information by iterating over key,value pairs
5461 yielded by item_info.items()"""
55- raise NotImplementedError
62+ pass
5663
64+ @abstractmethod
5765 def item_not_found (self , item_type , item_name ):
58- raise NotImplementedError
66+ pass
5967
6068
6169class ConsoleView (View ):
6270 def show_item_list (self , item_type , item_list ):
63- print (item_type .upper () + ' LIST:' )
71+ print (item_type .upper () + " LIST:" )
6472 for item in item_list :
6573 print (item )
66- print ('' )
74+ print ("" )
6775
6876 @staticmethod
6977 def capitalizer (string ):
7078 return string [0 ].upper () + string [1 :].lower ()
7179
7280 def show_item_information (self , item_type , item_name , item_info ):
73- print (item_type .upper () + ' INFORMATION:' )
74- printout = ' Name: %s' % item_name
81+ print (item_type .upper () + " INFORMATION:" )
82+ printout = " Name: %s" % item_name
7583 for key , value in item_info .items ():
76- printout += ', ' + self .capitalizer (str (key )) + ': ' + str (value )
77- printout += ' \n '
84+ printout += ", " + self .capitalizer (str (key )) + ": " + str (value )
85+ printout += " \n "
7886 print (printout )
7987
8088 def item_not_found (self , item_type , item_name ):
@@ -102,16 +110,16 @@ def show_item_information(self, item_name):
102110 self .view .show_item_information (item_type , item_name , item_info )
103111
104112
105- if __name__ == ' __main__' :
113+ if __name__ == " __main__" :
106114
107115 model = ProductModel ()
108116 view = ConsoleView ()
109117 controller = Controller (model , view )
110118 controller .show_items ()
111- controller .show_item_information (' cheese' )
112- controller .show_item_information (' eggs' )
113- controller .show_item_information (' milk' )
114- controller .show_item_information (' arepas' )
119+ controller .show_item_information (" cheese" )
120+ controller .show_item_information (" eggs" )
121+ controller .show_item_information (" milk" )
122+ controller .show_item_information (" arepas" )
115123
116124
117125### OUTPUT ###
0 commit comments