runtime
277 / 500
question 277 Data Models & Typing · medium

Order rows into Order objects

An export has one row per order line. Write dataclasses Line(sku, quantity, unit_price) and Order(order_id, customer_id, lines), where lines defaults to a new empty list for each order, and Order has a total() method. Then write orders_from_rows(rows) that groups the rows into Orders, in the order each order first appears.

`Line`sku: str, quantity: int, unit_price: float
`Order`order_id: str, customer_id: str, lines: list (default: a new empty list); total() → sum of quantity × unit price, rounded to 2 places
Functionorders_from_rows(rows); each row has order_id, customer_id, sku, quantity (int) and unit_price (float)
Rulerows for the same order must agree on the customer; otherwise ValueError

Loading your progress…

Order rows into Order objects — Python practice · Runtime