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
BeginnerBegin 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
1From Go to Python
Run Your First Python ProgramPrinting, Quotes, and CommentsBindings Beyond Go TypesDivision and Growing IntegersIndentation and ScopeRecap: A Python Receipt4Iteration and Transformations
range and Value IterationIndexes and Map EntriesSentinels Without Truth TrapsComprehensions and Stable SortRecap: Stable Ranking2Values, Absence, and Branches
None, nil, and Zero ValuesEquality Is Not IdentityTruth Tests Beyond boolBranches and Value SelectionRecap: Missing or Eligible3Slices, Maps, and Sharing
Slices Versus Python ListsMultiple Values and TuplesGo Maps and Python dictsSets and Shallow CopiesRecap: Inventory Collections6Errors, Modules, and Hints
Text and Exact OutputErrors Versus ExceptionsPackages and Module ExecutionHints Versus Static TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler