Menu
CoddyTech

Project: Model an Order

Lesson 43 of 48 in Coddy's Python for Go Developers course.

Build one in-memory order processor in four steps. Transfer Go struct modeling to Python classes, interface-valued policies to collaborators, explicit parse errors to exceptions, and map aggregation to dicts. Keep the same six files throughout. Each next step states which input and output behavior to replace; each solution is the complete program so far.

challenge icon

Challenge

Beginner

Begin a four-stage in-memory order processor. Implement Order(customer,quantity,unit_cents) and subtotal() in order.py. Inputs are a nonempty customer without a colon, positive integer quantity, and nonnegative integer cents, on three lines. Print customer:quantity:subtotal. Keep integer cents, as in a Go order model, while moving state into a Python class. Complete the subtotal while leaving the later policy/helper scaffolds in place. The starter runs with an unfinished result; all four steps keep the same files.

Editable files: order.py, standard_policy.py, bulk_policy.py, functions.py, main.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.

All four steps keep order.py, standard_policy.py, bulk_policy.py, functions.py, main.py, and locked driver.py. Keep the supplied entry wrapper. The driver calls main() once and then tests the APIs without more input or output. Each solution is the complete program through its step.

Try it yourself

from order import Order
from standard_policy import StandardPolicy
from bulk_policy import BulkPolicy
from functions import make_policy, parse_order
from main import main

main()

probe_order = Order("probe", 3, 7)
assert (probe_order.customer, probe_order.quantity, probe_order.unit_cents) == ("probe", 3, 7)
assert probe_order.subtotal() == 21
probe_order.quantity = 4
assert probe_order.subtotal() == 28
other_order = Order("other", 1, 0)
assert other_order.subtotal() == 0 and probe_order.subtotal() == 28

All lessons in Python for Go Developers

Practice on your own: Online Python compiler