Project: Model an Order
Lesson 43 of 48 in Coddy's Python for C Developers course.
Build one order processor in four connected steps. Replace a C struct-and-callback design with Python objects, then validate batch input and summarize totals. Keep integer cents and use only in-memory data. All six files remain present at every step: order.py, standard_policy.py, bulk_policy.py, functions.py, main.py, and locked driver.py. Each step keeps the completed earlier APIs while changing the input/output behavior described in its task.
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. 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.
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.
Keep integer cents, as in a C money model, while using Python-managed objects for orders.
Your code goes in order.py, standard_policy.py, bulk_policy.py, functions.py, main.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.
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 C Developers
1From C Programs to Python
Run Python Without mainPrinting Without Format CodesNames Versus Typed StorageDivision and Integer LimitsSuites Instead of BracesRecap: A Python Receipt4Iteration and Transformations
Iteration Without PointersIndexes Without Pointer MathSentinels and Loop ControlComprehensions and SortingRecap: Stable Ranking2Values and Truth Tests
Absence Is Not a Null PointerContent Versus Object IdentityTruth Tests and OperandsConditional Value SelectionRecap: Missing or Eligible3Containers and Aliasing
Lists Versus C ArraysTuples and Struct ValuesMappings and Lookup LoopsPointers, Copies, and AliasesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputError Codes Versus ExceptionsImports Are Not IncludesAnnotations Versus C TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler