Appendix B · Putting ideas together.
See how input, validation, calculation, and output form one clear program.
Start here.
A complete Python program combines the ideas from earlier chapters. It receives input, converts it into useful values, applies rules, and produces a result. The example's input is a list of dictionaries, each with a done field. The function completed_count uses a generator expression to produce 1 for each row whose done value is truthy, and sum adds those ones. With True, False, True, the result is 2.
There is a hidden assumption in that short function: every row has a done key containing a real Boolean. A missing key raises KeyError, while the string 'False' is truthy because nonempty strings count as true. Data from CSV commonly arrives as strings, so it must be converted under an explicit rule before being counted. The second example makes that boundary visible by checking each field and raising a clear ValueError for an unsuitable value.
This appendix reads worked code as a chain of contracts. A file parser promises rows; a validation function promises well-shaped values; a calculation promises a result; an output function promises a saved or displayed report. Each layer should be understandable on its own. Once those relationships are clear, a larger automation is a composition of familiar steps rather than one mysterious script.
Build the model.
The chapters have introduced values, decisions, functions, collections, files, services, and output formats. A larger program connects these pieces, but it still follows a simple flow: receive data, understand its shape, apply a rule, and communicate the result. Each step has a contract. A parser promises a particular kind of value, a calculation promises a result for valid input, and an output step promises a visible or saved artifact.
Two programs can print the same answer for one example while behaving differently for empty or malformed input. That is why the data path matters more than a single final number. A clear program makes conversion and validation visible before using the values it receives. The examples below show a small count calculation and then a version that states its input requirement explicitly.
How it works.
Imagine a report maker that reads a CSV of tasks, calculates completed counts by week, stores a summary in SQLite, and saves a chart. The CSV parser yields dictionaries whose values are initially text. A validation function converts each row into the program's own record shape. The counting function then works with known Booleans and dates rather than guessing what strings mean. A final layer writes the summary to a new output path.
This separation prevents file and service details from leaking into the calculation. A pure function such as completed_count depends only on its input and returns a result; it does not open a file or send a message. The file-reading layer owns encodings and missing paths. The database layer owns transactions and parameterised SQL. The output layer owns naming and preserving previous files. Each failure can be described where its cause is known.
The same architecture applies to a web response, spreadsheet, scanned document, or speech transcript. External data arrives with uncertainty, so the program checks its shape before giving it more power. A preview matters when an action sends, uploads, deletes, or overwrites data. The complete story of an automation is therefore a sequence of explicit boundaries, not a long uninterrupted block of commands.
Read it line by line.
def completed_count(rows):
return sum(1 for row in rows if row['done'])
sample = [{'done': True}, {'done': False}, {'done': True}]
print(completed_count(sample))The generator visits each record and contributes 1 only when done is truthy. sum returns 2. This is a pure calculation: it needs no file, database, or service. With an empty list it returns 0. A missing done field would raise KeyError, while the string 'False' would incorrectly count as true because nonempty strings are truthy; the second example addresses that assumption.
See the rule again.
def completed_count(rows):
total = 0
for row in rows:
if not isinstance(row.get('done'), bool):
raise ValueError('done must be a Boolean')
total += int(row['done'])
return total
print(completed_count([{'done': True}, {'done': False}]))The output is 1. A missing key or string value now raises a clear error rather than changing the count silently. For CSV input, convert an agreed set of text values to Booleans before calling this function.
A mistake to avoid.
The string 'False' is truthy in Python because it is nonempty. Validate or convert incoming CSV text before treating it as a Boolean; otherwise a report can count unfinished tasks as finished.