Create the Order Model
Lesson 43 of 48 in Coddy's Python for Ruby Developers course.
Build one order processor through four connected steps: model an order, delegate pricing, validate records, and aggregate a report. Translate Ruby collaborator objects and integer-cent calculations into Python modules. All six files remain in place across the project. Keep previous behavior unless a task explicitly changes it. The continuation steps begin from your preceding editable code; their locked driver supplies the current input and output contract.
Challenge
BeginnerBuild an in-memory order processor. This first step models one order; later steps add pricing, reject bad records, and aggregate a report. Implement Order(customer, quantity, unit_cents) and subtotal(), then use them in a complete program. Input is three lines: a nonempty customer name with no colon, a positive integer quantity, and a nonnegative integer unit price in cents. Print exactly customer:quantity:subtotal and a newline. Keep money as integer cents. No prompts, currency symbols, or decimal conversion. The starter runs but its subtotal is intentionally unfinished.
Keep the named classes, methods, and functions in the complete program. After normal execution, a locked driver calls these APIs on fresh objects and asserts their behavior without printing or reading more input.
For step 1, complete Order.subtotal() in order.py. The policy classes and helper functions are unfinished scaffolds for later steps; leave them in place. The imports and entry-point wrapper are supplied scaffolding, so no advanced module setup is expected. For example, from order import Order makes the class in order.py available in another file. Importing these modules does not read input; execution starts in the locked driver.
All four steps use the same files. Edit order.py for Order, standard_policy.py and bulk_policy.py for the pricing classes, functions.py for make_policy and parse_order, and main.py for input and output inside main(). driver.py is locked: it calls main() once, then runs the supplied API checks.
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 Ruby Developers
1From Ruby to Python
Run Your First Python ProgramStrings and InterpolationNames and Mutable StringsDivision and ParsingFrom end to IndentationRecap: Rewrite a Receipt4Iteration and Transformations
each and Python foreach_pair and enumeratenext Becomes continueselect, map, and sort_byRecap: Build a Ranking2Values and Decisions
nil Becomes None== and equal?Zero Is False in Pythonelsif and Conditional ValuesRecap: Eligibility Rules3Collections and References
Array Methods and ListsParallel Assignment and TuplesHash Defaults and Dict Keysdup, Copies, and SetsRecap: Clean an Inventory6Reliable Python Programs
gets, input, and Formattingrescue and Context Managersrequire and Import GuardsHints Do Not Enforce TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler