-
Notifications
You must be signed in to change notification settings - Fork 7k
Added type hints to 3-tier structural pattern #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,68 +3,73 @@ | |
Separates presentation, application processing, and data management functions. | ||
""" | ||
|
||
from typing import Dict, KeysView, Optional, Type, TypeVar, Union | ||
|
||
|
||
class Data: | ||
""" Data Store Class """ | ||
|
||
products = { | ||
'milk': {'price': 1.50, 'quantity': 10}, | ||
'eggs': {'price': 0.20, 'quantity': 100}, | ||
'cheese': {'price': 2.00, 'quantity': 10}, | ||
"milk": {"price": 1.50, "quantity": 10}, | ||
"eggs": {"price": 0.20, "quantity": 100}, | ||
"cheese": {"price": 2.00, "quantity": 10}, | ||
} | ||
|
||
def __get__(self, obj, klas): | ||
|
||
print("(Fetching from Data Store)") | ||
return {'products': self.products} | ||
return {"products": self.products} | ||
|
||
|
||
class BusinessLogic: | ||
""" Business logic holding data store instances """ | ||
|
||
data = Data() | ||
|
||
def product_list(self): | ||
return self.data['products'].keys() | ||
def product_list(self) -> KeysView[str]: | ||
return self.data["products"].keys() | ||
|
||
def product_information(self, product): | ||
return self.data['products'].get(product, None) | ||
def product_information( | ||
self, product: str | ||
) -> Optional[Dict[str, Union[int, float]]]: | ||
return self.data["products"].get(product, None) | ||
|
||
|
||
class Ui: | ||
""" UI interaction class """ | ||
|
||
def __init__(self): | ||
def __init__(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only objection is with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its part of the convention here is the discussion that leads me to believe that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an example from |
||
self.business_logic = BusinessLogic() | ||
|
||
def get_product_list(self): | ||
print('PRODUCT LIST:') | ||
def get_product_list(self) -> None: | ||
print("PRODUCT LIST:") | ||
for product in self.business_logic.product_list(): | ||
print(product) | ||
print('') | ||
print("") | ||
|
||
def get_product_information(self, product): | ||
def get_product_information(self, product: str) -> None: | ||
product_info = self.business_logic.product_information(product) | ||
if product_info: | ||
print('PRODUCT INFORMATION:') | ||
print("PRODUCT INFORMATION:") | ||
print( | ||
'Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format( | ||
product.title(), product_info.get('price', 0), product_info.get('quantity', 0) | ||
) | ||
f"Name: {product.title()}, " | ||
+ f"Price: {product_info.get('price', 0):.2f}, " | ||
+ f"Quantity: {product_info.get('quantity', 0):}" | ||
) | ||
else: | ||
print('That product "{0}" does not exist in the records'.format(product)) | ||
print(f"That product '{product}' does not exist in the records") | ||
|
||
|
||
def main(): | ||
ui = Ui() | ||
ui.get_product_list() | ||
ui.get_product_information('cheese') | ||
ui.get_product_information('eggs') | ||
ui.get_product_information('milk') | ||
ui.get_product_information('arepas') | ||
ui.get_product_information("cheese") | ||
ui.get_product_information("eggs") | ||
ui.get_product_information("milk") | ||
ui.get_product_information("arepas") | ||
|
||
|
||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
main() | ||
|
||
### OUTPUT ### | ||
|
Uh oh!
There was an error while loading. Please reload this page.