Capstone: a report on the barangay¶
In groups, you write a short report on the three barangays from data/households.csv, in plain Python, from the file to a written summary. Everything you need you have already written: reading the records (Exercise 6.4), counting (4.2, 4.5), sorting and filtering (5.3, 4.3), yields (5.1), formatting a line (2.4), and stopping at bad data (6.6). The capstone puts them together into one notebook that someone else could run from the top and get the same report.
| part | time |
|---|---|
| Plan the report: which questions, which functions | 15 min |
| Write it: functions first, then the report | 90 min |
| Check it: restart and run all, then swap with another group | 15 min |
| Present it: five minutes per group | 30 min |
There is no check for the capstone: what counts is a notebook that runs from top to bottom and a report that answers its questions correctly. Your facilitator and the other groups are your check.
import csv
import course
from course import PATHS
1 · Plan¶
Pick at least four questions for the report. Each becomes a function, and each function a line or a small table in the report.
- How many households and how many people in each barangay?
- How many households in each size class (Exercise 4.1)? Which barangay has the most large households?
- Which heads of household are 65 or older, and where?
- Which farming households did not report a crop, and how many in each barangay?
- What is the yield per hectare of each farm, and which crop has the highest average yield?
- How many people live in households of six or more?
- Your own question.
Write your group's questions in the cell below, as a list of texts. The report's headings will come from it.
questions = [
"How many households and people in each barangay?",
# add yours
]
2 · The records¶
Copy your read_households from 50_text_and_files.ipynb (Exercise 6.4) into the cell below, and read the file. Check what you get: the number of records, and the first one.
def read_households(path):
raise NotImplementedError("copy your read_households from Exercise 6.4 here")
households = course.attempt(read_households, PATHS.data / "households.csv")
course.waiting_for("your read_households") if households is course.NOT_YET else (len(households), households[0])
This part uses your read_households. Finish it above, and this cell runs by itself.
3 · The functions¶
One function per question, each taking the records as its input and returning its answer: a number, a list or a dictionary, never printing. Copy what you wrote in the modules and adapt it; write new ones for new questions. Use Try it on each as you go: course.try_it(per_barangay, households).
# your functions here
4 · The report¶
Build the report as a list of lines, one heading per question and the answer under it, then write it to report.md in the course folder. Markdown keeps it readable as plain text and shows it formatted on the site or in JupyterLab (double-click report.md).
Format numbers the way Exercise 2.4 did: thousands separators for people, one decimal place for shares and yields.
lines = ["# A report on three barangays", ""]
for question in questions:
lines.append(f"## {question}")
lines.append("")
# the answer to this question, as one or more lines
lines.append("")
report = PATHS.root / "report.md"
report.write_text("\n".join(lines), encoding="utf-8")
print(report.read_text(encoding="utf-8"))
# A report on three barangays ## How many households and people in each barangay?
5 · Check it¶
- Kernel → Restart Kernel and Run All Cells. The report must come out the same. If a cell fails, a name was set by a cell that is no longer there, or cells were run out of order: fix the notebook, not the kernel.
- Swap with another group. Run their notebook from the top. Do their numbers add up (the households of the three barangays make 30; the people, 137)? Is every heading answered?
- Bad data. Add a line to a copy of the CSV with a head aged 150, and point your notebook at it. Does it stop with a clear message (Exercise 6.6), or carry on with a wrong report?
6 · Present it¶
Five minutes per group: the questions you chose, one answer that surprised you, and one thing that went wrong on the way and how you found it.