Project: Validate Orders
Lesson 45 of 48 in Coddy's Python for Go Developers course.
Challenge
BeginnerKeep the model and pricing policies. Implement parse_order(line) returning an Order or raising ValueError. Input becomes a nonnegative record count then that many customer,quantity,unit_cents,policy lines. Strip all four fields. Require exactly four fields, nonempty customer, int-parsed quantity > 0, int-parsed price >= 0, and policy standard or bulk. Names never contain colon or comma; valid integer text is ordinary signed base-ten, invalid numeric fields may be arbitrary. Reject every other record. Print ok:customer:total or invalid per record and continue. An empty batch prints nothing. Handle ValueError per record, where a Go batch parser would check a returned error and continue. Carry forward all files, complete parse_order without removing make_policy, and replace the old single-order input/output in main().
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()
assert StandardPolicy().total(1009) == 1009
assert BulkPolicy().total(999) == 999
assert BulkPolicy().total(1009) == 909
assert make_policy("standard").total(1200) == 1200
assert make_policy("bulk").total(1200) == 1080
class ProbePolicy:
def __init__(self):
self.calls = []
def total(self, subtotal):
self.calls.append(subtotal)
return subtotal + 17
probe_policy = ProbePolicy()
probe_order = Order("probe", 2, 11, probe_policy)
assert (probe_order.customer, probe_order.quantity, probe_order.unit_cents) == ("probe", 2, 11)
assert probe_order.subtotal() == 22 and probe_order.total() == 39
probe_order.quantity = 3
assert probe_order.total() == 50 and probe_policy.calls == [22, 33]
probe_order.policy = StandardPolicy()
assert probe_order.total() == 33
parsed = parse_order(" Probe , 2 , 505 , bulk ")
assert isinstance(parsed, Order)
assert (parsed.customer, parsed.quantity, parsed.unit_cents) == ("Probe", 2, 505)
assert parsed.subtotal() == 1010 and parsed.total() == 909
for invalid_line in ["broken", "A,1,2,standard,extra", ",1,2,bulk", "A,0,1,standard", "A,1,-1,bulk", "A,no,2,standard", "A,1,2,vip"]:
try:
parse_order(invalid_line)
except ValueError:
pass
else:
raise AssertionError("parse_order must reject invalid records")
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