Define a Measurable Project Contract
Core idea
Build OrderSummary, a local command-line tool that reads a UTF-8 JSON array of orders from standard input and emits one sorted JSON summary. Each record has exactly id (1–20 ASCII letters or digits), category (books or tools), and amount_cents (an integer from 0 to 100000, not a Boolean). IDs must be unique. Accept at most 1000 records and 65536 input bytes; reject duplicate JSON keys and malformed data. Empty arrays are valid. Success means exit status 0 and a single JSON line; invalid input means status 2, no stdout and a fixed diagnostic on stderr. No external services or data files are needed. Store integer cents rather than binary floating-point money. The complete project is in fixtures/v115-capstone; the independent snippets below teach one contract at a time.
orders = [{'amount_cents': 125}, {'amount_cents': 275}]print(sum(r['amount_cents'] for r in orders))print(len(orders))The fixture has a known total of 400 cents and two records. These are independent acceptance expectations, not a copy of the production calculation.
Expected output:
4002Distinguish an integer amount from a Boolean
def valid_amount(value): return type(value) is int and 0 <= value <= 100000for value in (0, 100000, -1, True, 100001): print(valid_amount(value))Explicit type and range checks make boundary behavior measurable; bool is a subclass of int but is not a valid amount.
Expected output:
TrueTrueFalseFalseFalseCommon mistake
Do not start with a vague requirement such as “make it fast and secure.” Define valid inputs, failure outputs, byte limits and the workload before implementation. This is a bounded educational batch tool, not a payment processor.
Practice
Write an acceptance table for empty input array, two valid records, duplicate IDs, a Boolean amount and a 65537-byte input. State status, stdout and stderr for each. Compare the table with the supplied project tests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Project requirements
Check your understanding of project requirements.
What is printed?
print(type(True) is int, isinstance(True, int))What is printed?
print(sum([125, 275]), sum([]))What must an invalid order produce?
Why choose integer cents?