Create the Order Model
Lesson 43 of 48 in Coddy's Python for R Developers course.
Build an order processor in four connected steps: model orders, delegate pricing, validate records, and aggregate a report. Replace R list/environment records and function collaborators with Python instance objects and modules. Use integer cents and explicit return values. The same six files remain throughout the project; preserve prior APIs unless the next task explicitly changes them. Locked drivers define the current input/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 R Developers
1From R to Python
Run Your First Python Programcat, print, and StringsBindings and Copy SemanticsInteger Arithmetic and NABraces Become SuitesRecap: Rewrite a Receipt4Iteration and Transformations
seq_along and rangeitems and enumeratenext and continuelapply and ComprehensionsRecap: Build a Ranking2Values and Decisions
NULL, NA, and NoneVector Equality and IdentityScalar Conditions, Not Vectorsifelse and Scalar SelectionRecap: Eligibility Rules3Vectors to Containers
Vectors, Lists, and Index ZeroTuples and Exact UnpackingNamed Lists and Dictionariesunique and Shallow CopiesRecap: Clean an Inventory6Reliable Python Programs
readLines and Text ContractsConditions and Cleanupsource and Module ImportsAnnotations and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler