Create the Order Model
Lesson 43 of 48 in Coddy's Python for Lua Developers course.
Build one order processor through four connected steps: represent orders, delegate pricing, validate batch records, and aggregate a report. Transfer Lua table-based composition while using Python classes and named modules. Keep all six files and previous APIs unless the next task explicitly changes their behavior. Integer cents keep pricing exact. The driver owns input/output.
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 Lua Developers
1From Lua to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler