Production Incident Runbook
A small, standalone incident runbook that takes a plain-language description of a symptom — the kind of thing you'd type into a bug report or say out loud during an incident — and classifies it against the full 80-issue, Categories 1-8 catalog, returning the likely root cause(s) and fix(es) instead of leaving you to search the lesson page manually.
Example run
SYMPTOM: Service crashes on startup with a KeyError for a missing config key
Issue #1: KeyError
Root cause: A dict was accessed with [] on a key that isn't present -- often a config/env var missing in this environment.
Fix: Use .get(key, default) for optional keys, or validate all required keys explicitly at startup and fail with a clear message.
SYMPTOM: Report generator gets slower as the dataset grows -- looks quadratic
Issue #11: (no exception -- slow)
Root cause: A membership check (`in`) against a list, done repeatedly inside a loop, is O(n) per check -- doing it n times makes the whole function O(n^2).
Fix: Use a set instead of a list for repeated membership checks -- O(1) average case regardless of size.
SYMPTOM: Two threads occasionally deadlock with two locks acquired in different orders
Issue #32: (hang -- deadlock)
Root cause: transfer_a_to_b() acquires lock_a then lock_b; transfer_b_to_a() acquires lock_b then lock_a -- the opposite order. If both threads grab their first lock at roughly the same time, each ends up waiting forever for the lock the other thread is holding.
Fix: Establish one global ordering for acquiring locks (e.g. always sort accounts by ID and lock the lower one first) and have every code path follow it.
SYMPTOM: We're seeing the n+1 query pattern -- one query per row instead of a join
Issue #63: (no exception -- slow)
Root cause: One query fetches all the books, and then a separate query fetches each book's author, one at a time, inside a loop -- for 150 books, that's 151 total queries, each a full network round-trip.
Fix: Use a JOIN to fetch books and their authors together in a single query.
SYMPTOM: Everything is fine, no crash at all
No matching catalog entry for this symptom yet -- check the lesson's catalog for the closest pattern.
starter.py below into an empty folder, then run python3 starter.py. Standard library only — no installs needed.
The core piece: classify_symptom()
Everything else in this project calls this one function — it's the actual matching logic.
def classify_symptom(description):
lowered = description.lower()
matches = []
for entry in CATALOG:
if any(keyword in lowered for keyword in entry["keywords"]):
matches.append(entry)
return matches
Thought process: the design decision that matters here isn't the matching logic itself (a simple substring check) — it's the shape of the catalog data it matches against. Keeping each entry as a plain dict with a fixed set of keys (id, exception, keywords, root_cause, fix) means adding a new issue later is just appending one more dict, not touching the matching function at all. An experienced dev optimizes for "easy to extend" here, since this catalog is explicitly meant to grow to ~100 entries over later chapter passes.
Write the prompt you'd give an AI assistant to build this:
Show a strong version of this prompt
Write a Python function classify_symptom(description) using only the standard library. It takes a list of catalog entries called CATALOG, where each entry is a dict with keys "id", "exception", "keywords" (a list of lowercase strings), "root_cause", and "fix". Lowercase the description argument, then return a list of every CATALOG entry where at least one of its "keywords" is a substring of the lowercased description. Preserve CATALOG's original order in the results. Return an empty list if nothing matches -- don't raise an exception for a no-match case.
The pieces
CATALOG: all 80 issues across Categories 1-8, each with an exception name, matching keywords, a root cause, and a fix — mirroring the lesson's issue cards exactly.classify_symptom(description): the matching logic — lowercase the description, check each catalog entry's keywords against it.format_runbook_entry(entry): turns one matched catalog entry into a readable block.diagnose(description): the public entry point most code would actually call, handling the no-match case gracefully.
Ideas to make it your own
- Add a
confidencefield based on how many keywords matched, and sort results by confidence instead of catalog order. - Feed it real error messages/tracebacks from your own earlier chapter projects and see how well the keyword matching holds up.
- Extend
CATALOGwith your own entries for bugs you've personally hit, using the same symptom/root_cause/fix shape.
Why this project matters
Real incident response almost always starts with someone describing a symptom in plain language, not a stack trace — "the checkout page is crashing for some users" comes before anyone finds the actual KeyError. A tool that maps loose, human descriptions to known patterns and their fixes is exactly the kind of internal tooling real engineering teams build once they've hit the same handful of bugs enough times to notice the pattern.
More project ideas
From Chapter 7 onward, you get a genuine choice of what to build. The Production Incident Runbook above is fully built out with a starter and reference solution — the four ideas below are not. Each is a real, grounded use case solvable with only what Categories 1-8 have taught so far. No starter or solution files are provided on purpose — building one of these unassisted is the point.
1 A Traceback-to-Exception-Type Extractor
Problem: Given a full, multi-line Python traceback as a string, extract just the exception type and message from the last line.
What it should do: Return a tuple (exception_type, message), e.g. ("KeyError", "'DATABASE_URL'"), from a real traceback's last line.
Suggested approach: The last non-empty line of a traceback always has the shape ExceptionType: message — split on the first ": ".
2 A "Would This Crash?" Static Checker
Problem: Given a small snippet of code as a string, flag common crash-prone patterns without running it.
What it should do: Check a code string for patterns like a bare except:, an overly broad except Exception:, or an unguarded range(, and report which patterns it found.
Suggested approach: Simple substring/regex checks (Chapter 18) on the code text — a pattern-spotter, not a real AST-based linter.
3 A Symptom-to-Fix Quiz Generator
Problem: Turn the full Categories 1-8 catalog into a self-quiz: show a random symptom, let the user guess the exception type, then reveal the root cause and fix.
What it should do: Pick a random CATALOG entry, print its root_cause as a clue, prompt the user for a guess, and report whether they were right.
Suggested approach: random.choice(CATALOG) plus a simple case-insensitive string comparison against the guess.
4 A Multi-Symptom Incident Report
Problem: A single incident often has more than one symptom reported at once — the runbook should handle a list of symptom descriptions, not just one.
What it should do: Run diagnose() on each of a list of symptom strings and produce one combined report, plus a summary counting distinct catalog issues involved in total.
Suggested approach: Loop over the list, reuse this project's classify_symptom(), and track a running set of matched issue IDs for the summary count.