Appendix B: Final Exam
Goal: Prove that you can build a Python tool on your own, from the specification to CI-checked delivery · Duration: 150 min practical + 20 min questions · Prerequisite: Day 15
Rules
- You build one tool:
healthzchecks HTTP endpoints from a TOML file in parallel and reports the result as a table or JSON with a clean exit code. - Allowed: Appendix A,
--help, docs.python.org, the docs of typer, httpx and pydantic. Not allowed: AI assistants and copying code from the chapters. - You must not change the given test file. Scoring is done exclusively with the check script at the end.
- Passed: at least 70 of 100 points and task A6 (quality) fulfilled.
mkdir -p ~/py-lab/pruefung && cd ~/py-lab/pruefung
uv init -q -p 3.14 --package --name healthz healthz
cd healthz && mkdir -p tests lab/wwwPart 1: Practical exam
Specification
healthz check services.toml [--json] [--concurrency 10]
healthz --version
└─ config.load_config() ──► [Service, ...] ──► checks.check_all() ──► [Result, ...]
ConfigError ──► Exit 2 pydantic TaskGroup + Semaphore Exit 0 | 1
healthz.checks.Service(pydantic):name(not empty),url(http/https only, without credentials likehttps://user:pw@host),expect: int = 200,timeout: float = 2.0(greater than 0).healthz.checks.Result:name,url,ok,status(int | None),ms(duration),error(name of the exception, or empty).healthz.config.load_config(path) -> list[Service]reads[[service]]tables. Missing file, broken TOML, invalid fields, no services or duplicate names:ConfigError.async healthz.checks.check_all(services, client, concurrency=10) -> list[Result]: one sharedhttpx.AsyncClient, at mostconcurrencysimultaneous requests, results in the order of the input.okmeans: status equalsexpect. Network errors becomeok=False, status=None, error="ConnectError"– never an exception.- CLI (
healthz = "healthz.cli:app", typer):checkprints one line per service, with--jsona list of objects with exactly the fields ofResult. Exit 0: all ok, 1: at least one not ok, 2:ConfigError(message on stderr).--versionprintshealthz <version>. - Quality: ruff with
extend-select = ["E501", "B", "UP", "SIM", "I", "S"], mypy strict onsrc, pytest with coverage of at least 80 %. - Delivery: version 1.0.0, wheel in
dist/, GitHub Actions workflow.github/workflows/ci.yml(uv, ruff, mypy, pytest).
Given tests
cat > tests/test_pruefung.py <<'EOF'
"""Vorgegebene Prüfungstests – nicht ändern."""
import asyncio
import json
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import httpx
import pytest
from typer.testing import CliRunner
from healthz.checks import Service, check_all
from healthz.cli import app
from healthz.config import ConfigError, load_config
runner = CliRunner()
OK_TOML = """
[[service]]
name = "api"
url = "https://api.lab/healthz"
[[service]]
name = "db"
url = "http://db.lab:8080/"
expect = 204
timeout = 0.5
"""
def write(tmp_path, text):
path = tmp_path / "services.toml"
path.write_text(text, encoding="utf-8")
return path
def test_load_config(tmp_path):
services = load_config(write(tmp_path, OK_TOML))
assert [s.name for s in services] == ["api", "db"]
assert (services[0].expect, services[0].timeout) == (200, 2.0)
assert (services[1].expect, services[1].timeout) == (204, 0.5)
@pytest.mark.parametrize(
"text",
[
"", # keine Services
"kaputt =", # kein TOML
'[[service]]\nname = "x"\n', # url fehlt
'[[service]]\nname = "x"\nurl = "ftp://x.lab/"\n', # nur http(s)
'[[service]]\nname = "x"\nurl = "https://u:[email protected]/"\n', # Zugangsdaten
'[[service]]\nname = "x"\nurl = "https://x.lab/"\ntimeout = 0\n',
OK_TOML + '\n[[service]]\nname = "api"\nurl = "https://b.lab/"\n', # doppelt
],
)
def test_load_config_fehler(tmp_path, text):
with pytest.raises(ConfigError):
load_config(write(tmp_path, text))
def handler(request: httpx.Request) -> httpx.Response:
if request.url.host == "down.lab":
raise httpx.ConnectError("refused", request=request)
return httpx.Response(200 if request.url.host == "ok.lab" else 503)
def test_check_all_reihenfolge_und_fehler():
services = [
Service(name="ok", url="https://ok.lab/"),
Service(name="bad", url="https://bad.lab/"),
Service(name="down", url="https://down.lab/"),
Service(name="anders", url="https://ok.lab/", expect=204),
]
async def run():
transport = httpx.MockTransport(handler)
async with httpx.AsyncClient(transport=transport) as client:
return await check_all(services, client, concurrency=2)
results = asyncio.run(run())
assert [(r.name, r.ok, r.status) for r in results] == [
("ok", True, 200),
("bad", False, 503),
("down", False, None),
("anders", False, 200),
]
assert "ConnectError" in results[2].error
assert all(r.ms >= 0 for r in results)
class Lab(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200 if self.path == "/healthz" else 404)
self.end_headers()
def log_message(self, *args):
pass
@pytest.fixture
def lab_url():
server = ThreadingHTTPServer(("127.0.0.1", 0), Lab)
threading.Thread(target=server.serve_forever, daemon=True).start()
yield f"http://127.0.0.1:{server.server_address[1]}"
server.shutdown()
def test_cli_exit_codes_und_json(tmp_path, lab_url):
good = f'[[service]]\nname = "lab"\nurl = "{lab_url}/healthz"\n'
assert runner.invoke(app, ["check", str(write(tmp_path, good))]).exit_code == 0
bad = good + f'\n[[service]]\nname = "weg"\nurl = "{lab_url}/weg"\n'
result = runner.invoke(app, ["check", str(write(tmp_path, bad)), "--json"])
assert result.exit_code == 1
data = json.loads(result.output)
assert [(d["name"], d["ok"], d["status"]) for d in data] == [
("lab", True, 200),
("weg", False, 404),
]
assert set(data[0]) == {"name", "url", "ok", "status", "ms", "error"}
def test_cli_config_fehler_und_version(tmp_path):
result = runner.invoke(app, ["check", str(write(tmp_path, "kaputt ="))])
assert result.exit_code == 2
version = runner.invoke(app, ["--version"])
assert version.exit_code == 0
assert version.output.startswith("healthz ")
EOFLab data
echo ok > lab/www/healthz
cat > lab/services.toml <<'EOF'
[[service]]
name = "lab"
url = "http://127.0.0.1:8781/healthz"
[[service]]
name = "fehlt"
url = "http://127.0.0.1:8781/gibt-es-nicht"
[[service]]
name = "aus"
url = "http://127.0.0.1:9/"
timeout = 0.5
EOFYou start the lab server yourself for trying things out; the check script starts it on its own:
uv run python -m http.server 8781 --bind 127.0.0.1 --directory lab/wwwTasks
| Task | Content | Points |
|---|---|---|
| A1 | Project: Python 3.14, command healthz points to
healthz.cli:app |
5 |
| A2 | load_config with pydantic validation and
ConfigError |
15 |
| A3 | check_all: TaskGroup, semaphore, catch errors per
service |
20 |
| A4 | CLI: table, --json, exit codes 0/1/2,
--version |
15 |
| A5 | at least 3 tests of your own (e.g. timeout, empty list) | 10 |
| A6 | ruff, ruff format, mypy strict, coverage ≥ 80 % | 10 |
| A7 | version 1.0.0 via uv version, wheel via
uv build |
10 |
| A8 | Live: lab, fehlt, aus return
ok/not ok/not ok |
10 |
| A9 | CI workflow, valid according to actionlint | 5 |
Expected output in the live test (times vary):
OK 200 76ms lab http://127.0.0.1:8781/healthz
FAIL 404 12ms fehlt http://127.0.0.1:8781/gibt-es-nicht
FAIL ConnectError 2ms aus http://127.0.0.1:9/
Check script
cat > pruefung.sh <<'EOF'
#!/usr/bin/env bash
# Final exam check script: awards points per task
cd ~/py-lab/pruefung/healthz || exit 1
P=0
pts() {
if eval "$3" >/dev/null 2>&1; then echo "✔ $2 (+$1)"; P=$((P + $1))
else echo "✘ $2 (0/$1)"; fi
}
T="uv run pytest -q --no-cov tests/test_pruefung.py"
uv run python -m http.server 8781 --bind 127.0.0.1 --directory lab/www \
>/dev/null 2>&1 &
sleep 2
pts 5 "A1 Projekt und Kommando" "grep -qx 3.14 .python-version && \
grep -q '^healthz = \"healthz.cli:app\"' pyproject.toml"
pts 15 "A2 Konfiguration" "$T -k load_config"
pts 20 "A3 Parallele Checks" "$T -k check_all"
pts 15 "A4 CLI und Exit-Codes" "$T -k cli"
pts 10 "A5 Eigene Tests (mind. 3)" \
"test \$(uv run pytest -q --no-cov --collect-only | grep -c '::') -ge 14"
pts 10 "A6 Qualität" "uv run ruff check && uv run ruff format --check && \
uv run mypy && uv run pytest -q"
pts 10 "A7 Version 1.0.0 und Wheel" "uv run healthz --version | \
grep -qx 'healthz 1.0.0' && test -f dist/healthz-1.0.0-py3-none-any.whl"
pts 10 "A8 Live gegen Lab-Server" "uv run healthz check lab/services.toml --json \
| jq -e '[.[].ok] == [true, false, false]'"
pts 5 "A9 CI-Workflow gültig" \
"test -f .github/workflows/ci.yml && uvx --from actionlint-py actionlint"
pkill -f 'http.server 8781'
echo "Punkte: $P / 100"
EOF
bash pruefung.shExpected with a complete solution:
✔ A1 Projekt und Kommando (+5)
✔ A2 Konfiguration (+15)
✔ A3 Parallele Checks (+20)
✔ A4 CLI und Exit-Codes (+15)
✔ A5 Eigene Tests (mind. 3) (+10)
✔ A6 Qualität (+10)
✔ A7 Version 1.0.0 und Wheel (+10)
✔ A8 Live gegen Lab-Server (+10)
✔ A9 CI-Workflow gültig (+5)
Punkte: 100 / 100
--no-cov switches off the coverage threshold for the
partial runs – otherwise A2 would fail simply because four tests don’t
cover 80 % of the code. The given tests count 11; with three of your
own, it’s 14.
Tip: Work in the order of the tests: get
uv run pytest -q --no-cov -k load_configgreen first, thencheck_all, then the CLI. Quality and delivery go quickly in the last 20 minutes if ruff and mypy run along from the start.
A verified reference solution is in the companion material under
code/anhang-b-healthz/. Only look at it after the exam.
Part 2: Questions
One or two sentences per question, without aids. 20 of 25 correct: theory passed.
- What is in
uv.lock, what is in.venv, and which of them belongs in the repo? - What does
uv init --packagedo compared touv init, and what is[project.scripts]for? - Why is
def f(items=[])a bug, and how does@dataclasssolve it? - What is the difference between
Iterable[LogEntry]andlist[LogEntry]as a parameter type? - What is the
*indef summarize(entries, *, since=None)for? - Why can’t naive and time-zone-aware
datetimevalues be compared, and what is the rule in this book? - Why
subprocess.run([...])instead ofsubprocess.run("...", shell=True)? - Which exit codes does the book use, and why is 2 reserved for usage errors?
- What does
raise CertError(...) from errdo? - Why do you patch
checkcert.core.socket.create_connectionand notsocket.create_connection? - What are
tmp_path,monkeypatchandcaploggood for in pytest? - What does
mypy --strictcheck additionally, and why is a type hint alone not enough? - Why does an HTTP client always need a timeout, and which status codes do you retry with backoff?
- What does pydantic take care of at the boundary of a tool that a plain dict cannot?
- When does
asynciohelp, whenProcessPoolExecutor, and why don’t threads help with CPU load in the standard build? - What does a
TaskGroupdo when a task raises an exception? - Why does
costreportcompute money in cents asint(orDecimal) instead offloat? - What do
tottimeandcumtimeshow in cProfile? - Why did
@cacheonparse_stampmakelogsumslower, and when would it be right? - What does a generator save compared to a list, and when is the file
opened in
read_entries? - Why does
logsumsplit the file into byte ranges instead of sending lines to the processes? - What distinguishes
uv tool install, a wheel and a zipapp when shipping? - What is
stream_options={"include_usage": True}for, and what does the TTFT measure? - Why is structured LLM output with pydantic reliable in form, but not in content?
- Name three guardrails for an agent loop with tool calling.
Answer key
uv.lockpins all versions exactly (goes in the repo),.venvis the local environment (never in the repo;uv syncrebuilds it).- An installable package in the
src/layout with a build backend;[project.scripts]turns a function or app into a command on the PATH. - The default is created once at definition time and shared by all
calls; dataclasses forbid mutable defaults and require
field(default_factory=list). Iterableaccepts anything iterable, generators included – the function only promises to iterate once and stays memory-friendly.- All parameters after it are keyword-only; calls stay readable and the parameters can be reordered later.
- Without a time zone the point in time is ambiguous, and Python
raises
TypeError; the book always computes in UTC and treats input without a zone as UTC. - The argument list is passed without a shell, so spaces and
;in file names stay harmless – no command injection. - 0 ok, 1 functional finding, 2 usage error, 3 environment broken or Nagios UNKNOWN; 2 is the convention of argparse/click/typer, and a retry doesn’t help there.
- It raises your own exception and attaches the original one as
__cause__; the traceback shows both, and tests can check the cause. - You have to patch where the name is looked up – in the module
checkcert.core, which importedsocket. tmp_path: a fresh folder per test;monkeypatch: temporarily replace attributes and environment variables;caplog: capture and check log messages.- Type hints on every function, no implicit
Any, no unchecked optionals; Python itself ignores hints at runtime. - Without a timeout, a tool hangs forever on a dead server; retry 429, 500, 502, 503, 504 and transport errors – with backoff and jitter, but not other 4xx.
- Convert and check types (URL, date, literal), enforce required fields, produce understandable errors and generate a JSON schema.
asynciofor lots of waiting on network/disk, processes for CPU load; in the standard build, the GIL lets only one thread execute Python code at a time.- It cancels the remaining tasks and raises an
ExceptionGroup, which you take apart withexcept*. floatcannot represent amounts like 0.10 exactly; sums over thousands of lines would otherwise be off by cents.tottimeis the time in the function itself,cumtimeincludes all functions it calls.- Only 25 % hits: hashing and bookkeeping cost more, and the unbounded
cache holds 748,000 objects; it’s right when there are many repetitions,
and then with
lru_cache(maxsize=…). - Only one entry lives in memory at a time instead of all of them; the
file is opened only on the first
next(), i.e. insidesummarize. - Pickling millions of strings costs more than the parsing; byte ranges transfer only two numbers, and each process reads by itself.
uv tool installcreates a dedicated venv with a command; a wheel is the standard package for an index and pip; a zipapp is a single file but needs a suitable Python on the target.- Without the option, the stream delivers no token counts; TTFT is the time to the first token, i.e. the perceived waiting time.
- The schema enforces form and types, not truth: the model can return valid JSON with a wrong command or a wrong assessment.
- An allowlist of tools, validating arguments, upper limits for steps, tool calls and time; also only read-only tools and never LLM text passed to a shell.
Part 3: Self-assessment
| Topic | I can do it | done in the lab |
|---|---|---|
| uv, projects, ruff, pytest (Day 0) | [ ] | [ ] |
| Syntax, comprehensions (Day 1) | [ ] | [ ] |
| Functions, dataclasses, modules, mypy (Day 2) | [ ] | [ ] |
| typer, pathlib, subprocess, exit codes (Day 3) | [ ] | [ ] |
| Exceptions, logging, fixtures, mocks (Day 4) | [ ] | [ ] |
| Packaging, pre-commit, versioning (Day 5) | [ ] | [ ] |
| httpx, retries, pydantic (Day 6) | [ ] | [ ] |
| asyncio, TaskGroup, threads vs. processes (Day 7) | [ ] | [ ] |
| csv, sqlite3, polars (Day 8) | [ ] | [ ] |
| FastAPI, TestClient, Dockerfile (Day 9) | [ ] | [ ] |
| LLM APIs, streaming, structured output (Day 10) | [ ] | [ ] |
| Embeddings, RAG, evaluation (Day 11) | [ ] | [ ] |
| Tool calling, agent loop, MCP (Day 12) | [ ] | [ ] |
| scikit-learn, getting started with PyTorch (Day 13) | [ ] | [ ] |
| Profiling, optimization, distribution, CI (Day 14) | [ ] | [ ] |
| Tools under time pressure (Day 15) | [ ] | [ ] |
Every row without two checkmarks: work through the chapter once more – this time without reading the Expected blocks beforehand.