Exercise after Session 4: a World Bank mini-pipeline
Formative, no hand-in. About 90 minutes. Solution on the site Sun 1 Nov at 08:00.
Why this exercise
In the lab you built a pipeline from Statistics Sweden’s API to a regression table. Here you use a second open API, the World Bank’s, which answers in pages. A script that reads only the first page runs without an error and returns a fraction of the data. The exam asks you to predict what code like this does, without AI. So try each task yourself first; use the tutor prompt if you are stuck, not a chatbot that writes it for you.
What you need
The Session 4 lab folder, inside your course folder
(na446a-starter). It holds wb_exercise.py (the
script you fill in), check_exercise_s4.py (the self-check)
and a saved copy of the World Bank’s answers in
data/raw/saved/wb/. Open na446a-starter in VS
Code (the course folder, not the lab folder on its own).
Work in wb_exercise.py, one cell at a time, and
keep the names given in each TODO
(n_first_page, get_all_pages,
read_wb, gdp, panel,
res): the self-check looks for them. Run the self-check
whenever you like:
Open check_exercise_s4.py and press ▷ (Run Python File),
or type in the terminal:
cd s4-lab
uv run check_exercise_s4.py(cd s4-lab once per terminal: the terminal starts in
na446a-starter.) Save your script first: the self-check
reads the saved file.
It tells you which tasks are right and gives a hint for the others.
When all is right, the last line reads 6 of 6 tasks right.
It takes 5–15 seconds online and never changes your script.
No network, or the API is down? Set
USE_SAVED = True in the CHANGE HERE block. Everything then
runs on the saved copy (fetched 29 Sep 2026), and the self-check works
the same way.
Tasks, from easy to hard
| Task | What to do | Time |
|---|---|---|
| 1. One page (easy) | Predict, then run: how many rows exist for five countries over
1960–2025, and how many does the first page hold? Store
n_first_page and n_total; print the country of
the first and last row. |
10 min |
| 2. Every page (medium) | Complete get_all_pages(name): page 1, then a
for loop over the other pages, then an assert
that the rows add up to meta["total"]. Run it for both
indicators. |
20 min |
| 3. One function, many files (medium) | Complete read_wb(path) (one saved page to a DataFrame
with country, year as an integer,
value as a number); loop over the saved GDP pages and stack
them into gdp; assert that no country-year appears
twice. |
15 min |
| 4. One panel (medium) | Wrap Task 3’s loop in read_indicator(name); merge GDP
and unemployment into panel on country and year
(how="outer", validate="1:1",
indicator=True). |
15 min |
| 5. Growth (hard) | Sort by country and year; panel["growth"] = the yearly
percentage change of GDP per person within each
country. |
15 min |
| 6. DuckDB and a table (hard) | Save panel to
data/processed/wb_panel.parquet; with DuckDB (the
self-study slide in the appendix of the S4 slides), query that file for
mean growth and mean unemployment per country over 2010–2019, fastest
growth first; save the result to
output/tables/wb_nordic_2010s.csv. |
15 min |
Questions to answer in your script (as comments)
- Task 1: which countries are on page 1, and why is a script that stops there dangerous, even though it runs without an error?
- Task 4: how many unemployment values are missing, and why exactly that many?
- Task 5: how many growth values are missing, and why? What would
pct_change()give Finland’s first year if you forgot thegroupby? - Task 6: which country grew fastest in the 2010s, and does that country also have the lowest unemployment?
If you finish early (optional, 20 min)
Turn the exercise into a step of your lab pipeline: copy the code
into scripts/04_worldbank.py (paths from
Path(__file__).resolve().parents[1], as in the other
scripts; no # %% cells needed), make it print one log line
with rows in and out, add it to SCRIPTS in
run_all.py, and pass the delete-outputs test. The
self-check does not check this task; your run log does.
Two rules for this exercise
- Mark any line that came from an AI with the comment
# AI:, and make sure you can explain it. - The World Bank data are public. API keys (you need none here) go in
.env, never in code; and never paste personal or register data into a chatbot.