-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_parser.py
53 lines (46 loc) · 2.17 KB
/
json_parser.py
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
from typing import Any, Dict, List, Tuple
from drink import Drink
from order_parser import OrderParser
from pizza import Pizza
from product import Product
class JsonParser(OrderParser):
"""A class to parse JSON object into internal data structures."""
def get_product_list(self, json) -> List[Product]:
"""Return the list of products in this order."""
list_products = []
product_dictionaries_list = json["products"]
for product in product_dictionaries_list:
if product["product_category"] == "pizza":
pizza = Pizza(
product["size"],
product["toppings"],
product["type"])
list_products.append(pizza)
elif product["product_category"] == "drink":
drink = Drink(product["type"])
list_products.append(drink)
return list_products
def get_json(self, product_list: List[Tuple[int, Product]]) \
-> Tuple[int, Dict[str, Any]]:
"""Return a dictionary to be jsonified from product list."""
json = {"products": []}
for cart_item_id, product in product_list:
product_dictionary = {}
product_dictionary["cart_item_id"] = cart_item_id
if isinstance(product, Pizza):
product_dictionary["product_category"] = "pizza"
product_dictionary["size"] = product.get_size().lower()
product_dictionary["type"] = product.get_type().lower()
product_dictionary["toppings"] = [
name.lower() for name in product.get_toppings()]
elif isinstance(product, Drink):
product_dictionary["product_category"] = "drink"
product_dictionary["type"] = product.get_type().lower()
json["products"].append(product_dictionary)
return json
def get_address(self, json) -> str:
"""Return the address of this order."""
return json["delivery_method"]["details"]["address"]
def get_order_no(self, json) -> int:
"""Return the order number of this order."""
return json["delivery_method"]["details"]["order_no"]