flowchart TD
A["Enter try: block<br/>(run risky code)"] --> B{"Exception<br/>raised?"}
B -- "No" --> C["Run else: block<br/>(only on clean success)"]
B -- "Yes" --> D{"Matches an<br/>except: type?"}
D -- "Yes" --> E["Run matching<br/>except: handler"]
D -- "No" --> F["Uncaught exception<br/>queued to propagate"]
C --> G["Run finally: block<br/>(ALWAYS executes)"]
E --> G
F --> G
G --> H["Continue normally<br/>(or re-raise uncaught)"]
10 Exceptions, Defensive Programming, Testing, and Systematic Debugging
Chapter Mission: Transform fragile scripts that crash—or worse, publish silent nonsense—into peer-reviewable, fault-tolerant research software. Master Python’s full
try/except/else/finallylifecycle, specific exception handling, explicitraise ValueErrorcontracts, custom clinical exceptions, automatedassert/pytest-style unit tests, and systematic debugging workflows.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch10 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch10 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch10 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Trace and apply every clause of Python’s
try/except/else/finallyexception-handling lifecycle. - Catch specific exception classes (
ValueError,TypeError,KeyError) using EAFP style while avoiding the silent-bug trap of bareexcept:. - Enforce defensive clinical and statistical input contracts by choosing deliberately between returning
None(valid but undefined) and raisingValueError(impossible or corrupt input). - Define domain-specific custom exceptions (such as
InvalidVitalSignError(ValueError)) to make research error logs immediately readable to collaborators and reviewers. - Write automated
assertandpytest-style unit tests—including boundary checks, floating-point tolerances, and expected-exception checks—to catch broken formulas before submission. - Apply a repeatable four-step Systematic Debugging Protocol (Reproduce \(\rightarrow\) Isolate \(\rightarrow\) Inspect \(\rightarrow\) Lock In with a Regression Test) to eliminate bugs permanently.
10.1 Stage 1 — First-Principles Intuition: Loud Failures Beat Silent Retractions
When a novice’s script crashes with a red traceback, their first instinct is frustration. Yet in scientific computing, a loud crash is a gift. The true nightmare of computational research is a program that never crashes, silently converts a stray "999" sentinel or missing hemoglobin value into a distorted cohort mean, and prints a clean-looking Table 1 that triggers a journal retraction two years later.
Defensive programming and automated testing invert that risk: you teach your code to inspect every incoming value at the gate, reject impossible inputs loudly, quarantine corrupt rows cleanly during batch runs, and verify every mathematical formula automatically before a single number enters your manuscript.
Think of every research function as a biosafety laboratory with a double-door intake airlock:
- Gate Inspection (
raise ValueError): Before running any assay, the intake technician checks the specimen label, sample type, and physical plausibility (e.g., human body temperature between \(30.0\) and \(45.0\) degrees Celsius). Impossible samples are rejected loudly so they can never contaminate the downstream database. - Quarantine Tray (
try/except): When processing a crate of \(5{,}000\) field tubes, one cracked vial should not shut down the entire hospital lab. A batch controller wraps each tube insidetry / except ValueError, routing clean samples to the analysis rack and logging rejected tubes into a written quarantine ledger. - Guaranteed Sterilization (
finally): Whether a test succeeds or fails, the technician always logs the timestamp and closes the file handle or freezer door. - Daily Calibration Controls (
assert/ Unit Tests): Every morning, the lab runs known control standards (e.g., \(70\text{ kg}\) at \(1.75\text{ m}\) must yield BMI \(\approx 22.86\)) before trusting real patient results. If a calibration test fails, anAssertionErrorhalts the bench immediately.
10.1.1 Two Idioms: LBYL vs. EAFP
Python programmers recognize two complementary styles of defensive coding:
- LBYL (Look Before You Leap): Explicitly check preconditions (
if key in d:,if cases == 0:) before acting. Ideal for validating clinical bounds andNoneguards. - EAFP (Easier to Ask for Forgiveness than Permission): Attempt the operation directly inside
try:and handle the exact failure (except (KeyError, TypeError):). Ideal for parsing dirty strings (float(raw_val)) and reading deeply nested JSON/dictionary payloads.
10.2 Stage 2 — Visual Mental Model: Exception Control-Flow & Systematic Debugging
Every try statement follows one exact deterministic path through its four clauses (try, except, else, finally). Study the routing diagram carefully:
Notice two subtleties that even experienced researchers sometimes miss:
else:runs only whentry:raises zero exceptions. Keeping post-parse logic insideelse:keeps thetry:block tiny so you never accidentally swallow a bug from later lines!finally:always runs—even iftry:succeeded, even ifexcept:caught an error, even if an uncaught exception is about to crash the script, and even iftry:executes areturnstatement.
Never “guess and randomly edit lines” when a script misbehaves. Follow the same scientific method you use at the lab bench:
- Reproduce: Shrink the failure to the smallest input that triggers the bug reliably (e.g., a single patient dictionary instead of a CSV of \(10{,}000\) rows).
- Isolate: Read the traceback bottom-up—last line gives the exception class and message; the line immediately above shows the exact failing expression. Narrow down which function first produces a wrong value.
- Inspect: Print intermediate values and types (
print(repr(x), type(x))) or trace one call frame by hand. Verify assumptions (e.g., is"38.5"astr, or isTruesneaking pastisinstance(x, int)?). - Verify & Lock In: Once fixed, add an
asserttest for that exact failing input so the bug can never return silently in a future revision.
10.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_02_data_structures_algorithms/ch10_exceptions_testing/01_walkthrough.ipynb in Colab and execute every cell alongside the dissections below.
10.3.1 Full Exception Lifecycle: try / except / else / finally
def parse_reading_with_audit(raw_text):
"""Demonstrate full try / except / else / finally lifecycle."""
try:
value = float(raw_text)
except (ValueError, TypeError) as exc:
print(f" [EXCEPT] Rejected {raw_text!r}: {exc}")
result = None
else:
print(f" [ELSE] Parsed clean float: {value}")
result = value
finally:
print(" [FINALLY] Audit entry closed.")
return result
r1 = parse_reading_with_audit("38.4")
r2 = parse_reading_with_audit("N/A")
print("Returned:", r1, r2)Expected Output:
Look closely at the execution order:
- For
"38.4",float("38.4")succeeds, so Python skipsexcept, enterselse, and closes withfinally. - For
"N/A",float("N/A")raisesValueError, so Python jumps directly intoexcept (ValueError, TypeError) as exc:, skipselse, and still runsfinally. - Binding the exception object with
as excgives you the exact diagnostic message for your audit log.
10.3.2 Specific Exceptions & EAFP Nested Dictionary Lookups
Surveillance APIs and clinic JSON exports frequently omit districts, drop optional metrics, or store None for an entire division that failed to report. With EAFP, a single compact try / except (KeyError, TypeError) safely guards every failure mode at once:
surveillance = {
"Dhaka": {"cases": 140, "deaths": 3},
"Sylhet": {"cases": 25, "deaths": 0},
"Barishal": None,
}
def get_metric(report, district, metric):
"""Fetch nested metric safely via EAFP."""
try:
return report[district][metric]
except (KeyError, TypeError):
return None
print("Dhaka cases: ", get_metric(surveillance, "Dhaka", "cases"))
print("Sylhet deaths: ", get_metric(surveillance, "Sylhet", "deaths"))
print("Barishal cases:", get_metric(surveillance, "Barishal", "cases"))
print("Missing dist: ", get_metric(surveillance, "Khulna", "cases"))Expected Output:
except: in Research Code!
Writing a naked except: (without naming specific exception classes) catches everything—including NameError from a misspelled variable, ZeroDivisionError from a math bug, and even KeyboardInterrupt when you try to stop a runaway loop! Always name the narrowest tuple of expected exceptions, such as except (ValueError, TypeError): or except (KeyError, TypeError):.
10.3.3 Defensive Programming: raise ValueError vs. Returning None
A common design question in biostatistics is: “When an input looks wrong, should my function return None or raise ValueError?” Follow this crisp scientific rule:
- Return
Nonewhen the input is factually valid, but the statistic is mathematically undefined (for example,cases == 0in a quiet week where Case Fatality Rate \(0 / 0\) is undefined). raise ValueErrorwhen the input is physically or logically impossible (for example, negative deaths or negative cases—a sure sign of corrupted data).
def compute_cfr(deaths, cases):
"""Compute Case Fatality Rate (%), raising on impossible counts."""
if deaths < 0 or cases < 0:
raise ValueError("deaths and cases must be non-negative")
if cases == 0:
return None
return round((deaths / cases) * 100, 2)
print("Valid CFR (12/400):", compute_cfr(12, 400))
print("Zero cases (0/0): ", compute_cfr(0, 0))Expected Output:
10.3.4 Custom Domain Exception (InvalidVitalSignError)
When your research package grows past a handful of functions, subclassing ValueError lets you create a self-documenting clinical exception in just two lines:
class InvalidVitalSignError(ValueError):
"""Raised when a clinical vital sign is outside human bounds."""
def check_temp(temp_c):
"""Validate human body temperature in [30.0, 45.0] C."""
if isinstance(temp_c, bool) or not isinstance(temp_c, (int, float)):
raise InvalidVitalSignError(f"Non-numeric temp: {temp_c!r}")
if not (30.0 <= temp_c <= 45.0):
raise InvalidVitalSignError(f"Implausible temp: {temp_c} C")
return float(temp_c)
for sample in [37.4, 41.2, 999.0]:
try:
clean = check_temp(sample)
print("Accepted:", clean)
except InvalidVitalSignError as exc:
print("Flagged vital sign:", exc)Expected Output:
Because InvalidVitalSignError inherits from ValueError, callers can catch it specifically with except InvalidVitalSignError: or generically with except ValueError:. Also notice the isinstance(temp_c, bool) guard—in Python, bool is a subclass of int (isinstance(True, int) is True!), so checking bool explicitly is an essential defensive habit.
10.3.5 Automated Unit Testing: assert & pytest-Style Test Functions
Python’s built-in assert condition, "optional message" statement does nothing when condition is True and raises AssertionError immediately when condition is False. Grouping assertions into small test_*() functions follows the exact convention used by pytest—Python’s industry-standard testing framework:
def test_compute_cfr_nominal():
assert compute_cfr(12, 400) == 3.0
assert compute_cfr(0, 250) == 0.0
def test_compute_cfr_zero_cases():
assert compute_cfr(0, 0) is None
def test_compute_cfr_rejects_negatives():
try:
compute_cfr(-3, 100)
except ValueError:
pass
else:
raise AssertionError("Expected ValueError for negative deaths")
for test_fn in (
test_compute_cfr_nominal,
test_compute_cfr_zero_cases,
test_compute_cfr_rejects_negatives,
):
test_fn()
print(f"PASS: {test_fn.__name__}")Expected Output:
Look closely at test_compute_cfr_rejects_negatives: a good test suite verifies not only that valid inputs return the right numbers, but also that invalid inputs actually raise the promised exception! In a standalone .py project tested with pytest, you can write that same check concisely using with pytest.raises(ValueError): compute_cfr(-3, 100).
10.3.6 Break It On Purpose! (Exception & Testing Error Anatomy)
Python’s Traceback:
Diagnosis: Our explicit guard clause inside compute_cfr intercepted a physically impossible negative death count (-5) and stopped execution immediately with a human-readable message rather than returning a nonsensical -5.0% fatality rate.
Fix: Catch ValueError in the calling batch pipeline (quarantining the row) or correct the upstream data entry before calling compute_cfr.
Python’s Traceback:
Diagnosis: buggy_cfr(12, 400) returned 0.03 (a proportion) instead of 3.0 (a percentage). Without an automated assert, this \(100\times\) scale error could silently flow straight into a manuscript table!
Fix: Multiply by 100 (round((deaths / cases) * 100, 2)) and re-run the test suite to confirm PASS.
Python’s Traceback:
Diagnosis: "Dhaka" existed in report, so the first lookup succeeded, but the inner dictionary did not contain "icu_beds". Direct bracket indexing without exception handling crashed the entire script.
Fix: Wrap nested lookups in an EAFP helper (get_metric(report, "Dhaka", "icu_beds")) catching (KeyError, TypeError) and returning None.
10.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: A maternal health mobile survey uploads daily batches of raw intake dictionaries (patient_id, temp_c, sbp_mmhg). Some rows arrive as numeric strings ("37.8"), some contain impossible sentinel temperatures ("999"), and some drop required keys entirely. Build a two-layer defensive pipeline—a strict single-row validator raising InvalidVitalSignError and a resilient batch processor that quarantines corrupt rows without crashing—and verify it with assert.
10.4.1 Blueprint (Completed on Paper Before Coding)
- Layered Architecture (Single Responsibility):
validate_intake_row(row)\(\rightarrow\) enforces required keys (patient_id,temp_c,sbp_mmhg), numeric casting, and physiological bounds (\(30.0 \le \text{temp\_c} \le 45.0\), \(50.0 \le \text{sbp\_mmhg} \le 260.0\)); raisesInvalidVitalSignErroron any violation.process_intake_batch(raw_rows)\(\rightarrow\) loops withenumerate(raw_rows), catchesInvalidVitalSignErrorper row intoquarantine, appends clean dicts inelse:toaccepted, and returns(accepted, quarantine).
- Edge Cases & Traps:
- Calling
float(row["temp_c"])before verifying"temp_c" in rowleaksKeyErrorinstead ofInvalidVitalSignError. - Catching
InvalidVitalSignErrorinside the loop (not outside!) ensures row 1’s failure never aborts rows 2 and 3.
- Calling
- Verification Plan (Hand Check):
- Input batch has 4 rows:
M-01(valid string numbers),M-02(temp_c="999"\(\rightarrow\) quarantined),M-03(missingsbp_mmhg\(\rightarrow\) quarantined),M-04(valid numeric values). - Expected result:
len(good) == 2andlen(bad) == 2.
- Input batch has 4 rows:
10.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
def validate_intake_row(row):
"""Validate one clinic intake dict or raise InvalidVitalSignError."""
for key in ("patient_id", "temp_c", "sbp_mmhg"):
if key not in row:
raise InvalidVitalSignError(f"Missing field: {key}")
try:
temp_c = float(row["temp_c"])
sbp = float(row["sbp_mmhg"])
except (ValueError, TypeError) as exc:
raise InvalidVitalSignError(f"Non-numeric vital: {exc}")
if not (30.0 <= temp_c <= 45.0):
raise InvalidVitalSignError(f"Bad temp_c: {temp_c}")
if not (50.0 <= sbp <= 260.0):
raise InvalidVitalSignError(f"Bad sbp_mmhg: {sbp}")
return {"patient_id": row["patient_id"], "temp_c": temp_c, "sbp": sbp}
def process_intake_batch(raw_rows):
"""Return (accepted_rows, quarantine_log) without crashing."""
accepted = []
quarantine = []
for idx, row in enumerate(raw_rows):
try:
clean = validate_intake_row(row)
except InvalidVitalSignError as exc:
quarantine.append(f"Row {idx}: {exc}")
else:
accepted.append(clean)
return (accepted, quarantine)
sample_rows = [
{"patient_id": "M-01", "temp_c": "37.8", "sbp_mmhg": "118"},
{"patient_id": "M-02", "temp_c": "999", "sbp_mmhg": "120"},
{"patient_id": "M-03", "temp_c": "38.6"},
{"patient_id": "M-04", "temp_c": 36.7, "sbp_mmhg": 142},
]
good, bad = process_intake_batch(sample_rows)
assert len(good) == 2 and len(bad) == 2
print("Accepted:", good)
print("Quarantined:", bad)Output:
10.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_02_data_structures_algorithms/ch10_exceptions_testing/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and complete all six defensive programming and testing functions below. Pass each function name (without parentheses!) to tutor.check(...):
- To-Do 10.1 (Warm-Up —
parse_float_safely(raw_val, default=None)): Convertraw_valviafloat(raw_val)insidetry / except (ValueError, TypeError), returningdefaultfor dirty inputs such as"N/A",None, or"". - To-Do 10.2 (Raising vs. Returning
None—safe_cfr_percent(deaths, cases)): RaiseValueErrorifdeaths < 0orcases < 0; returnNoneifcases == 0; otherwise returnround((deaths / cases) * 100, 2). - To-Do 10.3 (Input Contract —
validate_body_temp(temp_c)): RaiseValueErroriftemp_cis not numeric (or is abool) or lies outside the inclusive bounds \([30.0, 45.0]\); otherwise returnfloat(temp_c). - To-Do 10.4 (EAFP Lookup —
extract_nested_metric(report_dict, district, metric)): Safely look upreport_dict[district][metric], catching(KeyError, TypeError)and returningNonewhile preserving legitimate0values. - To-Do 10.5 (
try/except/elseLoop —clean_numeric_series(raw_values)): Convert valid items tofloat, skip and count unparseable values/None, and return the 2-tuple(clean_floats, error_count). - To-Do 10.6 (Automated Testing —
verify_bmi_function(bmi_fn)): Runassertchecks with a floating-point tolerance (< 0.05) on a suppliedbmi_fn(weight_kg, height_m)across multiple distinct test pairs, returningTruewhen valid and lettingAssertionErrorfire on buggy functions.
10.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Test your exception-flow and debugging reasoning on paper before checking Stage 8:
Full Lifecycle Trace: What exact lines does this snippet print?
Exception Path Trace: If
"42"above is replaced with"N/A", which letters are printed?The Bare
except:Hazard: Why isexcept:without a specific exception type dangerous in a data-cleaning loop?Design Choice (
raisevs.None): Insafe_cfr_percent(deaths, cases), why shouldsafe_cfr_percent(0, 0)returnNone, whereassafe_cfr_percent(-2, 50)raisesValueError?The Python
boolSubclass Trap: What doesisinstance(True, int)evaluate to in Python, and why does that matter for validating \(2 \times 2\) contingency table counts?Exception Order in
validate_body_temp("37.5"): If a function checksif temp_c < 30.0:before checkingisinstance(temp_c, (int, float)), what exception fires instead ofValueError?Custom Exception Inheritance: Given
class InvalidVitalSignError(ValueError): pass, willexcept ValueError:catch a raisedInvalidVitalSignError? What about the reverse (except InvalidVitalSignError:catching a plainValueError)?Loop Isolation: Why must
try / except ValueErrorsit inside theforloop ofbatch_validate_recordsrather than wrapping the entireforloop from the outside?Testing with Multiple Inputs: Why is a single assertion
assert bmi_fn(80, 2.0) == 20.0insufficient to catch a fake functiondef fake(w, h): return 20.0?Reading a Traceback: When Python prints a 20-line traceback across four functions, which end of the traceback (top or bottom) should you read first, and why?
10.7 Stage 7 — Chapter 10 Hands-On Coding Exam (100 Points)
Open part_02_data_structures_algorithms/ch10_exceptions_testing/03_coding_exam.ipynb, implement all three defensive functions, and run tutor.check_exam("ch10", exam_submissions) to earn your Chapter 10 score out of \(100\) points.
10.7.1 Exam Task 10.1 (30 Points): Defensive Patient Record Parser
Write parse_patient_record(raw_dict) to validate and clean a single patient record dictionary:
- Verify
raw_dictis adictcontaining all three required keys:"patient_id","temp_c", and"hb_g_dl"(raiseValueError—notKeyError—if any key is missing). - Verify
"patient_id"is a non-empty string after.strip()(raiseValueErrorotherwise). - Cast
"temp_c"and"hb_g_dl"tofloat, rejecting booleans and non-numeric values withValueError. - Enforce inclusive clinical bounds:
temp_cin \([30.0, 45.0]\) andhb_g_dlin \([2.0, 25.0]\) (raiseValueErrorif outside either range). - Return the clean typed dictionary
{"patient_id": pid, "temp_c": temp_c, "hb_g_dl": hb_g_dl}.
10.7.2 Exam Task 10.2 (35 Points): Resilient Batch Record Validator
Write batch_validate_records(raw_records) to validate an entire list of raw patient records without crashing:
- Call
parse_patient_record(record)insidetry / except ValueError as exc:for every record inraw_records. - Append each valid cleaned dictionary to
valid_list. - Append each rejected record’s error message string (
str(exc)) torejected_errors. - Return the 2-tuple
(valid_list, rejected_errors)cleanly for mixed, all-valid, all-invalid, and empty batches.
10.7.3 Exam Task 10.3 (35 Points): Strictly Validated \(2 \times 2\) Odds Ratio
Write compute_safe_odds_ratio(a, b, c, d) for a \(2 \times 2\) contingency table:
- Validate that all four cell counts (
a,b,c,d) are non-negativeintvalues and notbool—raiseValueErrorif any input is negative, afloat, abool, astr, orNone. - Return
Noneimmediately ifb == 0orc == 0. - Otherwise compute the cross-product ratio \(\frac{a \times d}{b \times c}\) and return it rounded to 2 decimal places.
10.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
10.8.1 A. Concept & Execution Trace Test Key
- Prints
BthenC(trysucceeded, soexceptis skipped,elseruns, andfinallyalways runs). - Prints
AthenC(ValueErrorjumps toexcept,elseis skipped, andfinallyalways runs). - A bare
except:silently swallows real programming bugs (NameError,Syntax-like runtime typos,ZeroDivisionError) and evenKeyboardInterrupt, masking root causes during debugging. - Zero cases (
0, 0) is a legitimate surveillance week with an undefined ratio (None), whereas negative deaths (-2) is physically impossible corrupt data that must fail loudly withValueError. isinstance(True, int)evaluates toTruebecauseboolsubclassesint(True == 1,False == 0). Withoutisinstance(val, bool), boolean flags could silently masquerade as patient counts!TypeError('<' not supported between instances of 'str' and 'float'). Always validate types before numeric range comparisons.except ValueError:catchesInvalidVitalSignError(Yes—subclasses match parent handlers). The reverse is No:except InvalidVitalSignError:will not catch a plainValueError.- If
try / exceptwraps the outside of the loop, the very first bad row exits the entire loop immediately, abandoning every valid record that follows it! - A constant dummy function
lambda w, h: 20.0passes any single test expecting20.0. Testing at least two inputs with different expected BMIs (e.g.,20.0and25.0) exposes hardcoded shortcuts immediately. - Read from the bottom up: the final line names the exact exception type and message, and the innermost frame immediately above points to the exact expression that triggered the failure.
10.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
# To-Do 10.1
def parse_float_safely(raw_val, default=None):
"""Convert raw_val to float, or return default on parse error."""
try:
return float(raw_val)
except (ValueError, TypeError):
return default
# To-Do 10.2
def safe_cfr_percent(deaths, cases):
"""Return CFR (%) rounded to 2 decimals, or None if cases == 0."""
if deaths < 0 or cases < 0:
raise ValueError("deaths and cases must be non-negative")
if cases == 0:
return None
return round((deaths / cases) * 100, 2)
# To-Do 10.3
def validate_body_temp(temp_c):
"""Return float(temp_c) if numeric and in [30.0, 45.0], else raise."""
if isinstance(temp_c, bool) or not isinstance(temp_c, (int, float)):
raise ValueError(f"Temperature must be numeric, got {temp_c!r}")
if not (30.0 <= temp_c <= 45.0):
raise ValueError(f"Temperature {temp_c} outside [30.0, 45.0]")
return float(temp_c)
# To-Do 10.4
def extract_nested_metric(report_dict, district, metric):
"""Safely return report_dict[district][metric], or None on error."""
try:
return report_dict[district][metric]
except (KeyError, TypeError):
return None
# To-Do 10.5
def clean_numeric_series(raw_values):
"""Return (clean_floats, error_count) skipping bad values/None."""
clean_floats = []
error_count = 0
for val in raw_values:
try:
num = float(val)
except (ValueError, TypeError):
error_count += 1
else:
clean_floats.append(num)
return (clean_floats, error_count)
# To-Do 10.6
def verify_bmi_function(bmi_fn):
"""Assert bmi_fn(weight_kg, height_m) matches BMI; return True."""
assert abs(bmi_fn(80, 2.0) - 20.0) < 0.05, "Failed (80, 2.0)"
assert abs(bmi_fn(64, 1.6) - 25.0) < 0.05, "Failed (64, 1.6)"
assert abs(bmi_fn(70, 1.75) - 22.86) < 0.05, "Failed (70, 1.75)"
return True10.8.3 C. Annotated Solutions — Chapter 10 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
# Exam Task 10.1 (30 pts)
def parse_patient_record(raw_dict):
"""Validate patient record dict and return clean typed dict."""
if not isinstance(raw_dict, dict):
raise ValueError("Record must be a dictionary")
for key in ("patient_id", "temp_c", "hb_g_dl"):
if key not in raw_dict:
raise ValueError(f"Missing required key: {key}")
raw_pid = raw_dict["patient_id"]
if not isinstance(raw_pid, str) or not raw_pid.strip():
raise ValueError("patient_id must be a non-empty string")
pid = raw_pid.strip()
raw_temp = raw_dict["temp_c"]
raw_hb = raw_dict["hb_g_dl"]
if isinstance(raw_temp, bool) or isinstance(raw_hb, bool):
raise ValueError("Clinical vitals cannot be boolean")
try:
temp_c = float(raw_temp)
hb_g_dl = float(raw_hb)
except (ValueError, TypeError) as exc:
raise ValueError(f"Non-numeric vital sign: {exc}")
if not (30.0 <= temp_c <= 45.0):
raise ValueError(f"temp_c out of range [30.0, 45.0]: {temp_c}")
if not (2.0 <= hb_g_dl <= 25.0):
raise ValueError(f"hb_g_dl out of range [2.0, 25.0]: {hb_g_dl}")
return {"patient_id": pid, "temp_c": temp_c, "hb_g_dl": hb_g_dl}
# Exam Task 10.2 (35 pts)
def batch_validate_records(raw_records):
"""Validate records via parse_patient_record without crashing."""
valid_list = []
rejected_errors = []
for record in raw_records:
try:
cleaned = parse_patient_record(record)
except ValueError as exc:
rejected_errors.append(str(exc))
else:
valid_list.append(cleaned)
return (valid_list, rejected_errors)
# Exam Task 10.3 (35 pts)
def compute_safe_odds_ratio(a, b, c, d):
"""Validate non-negative ints (not bool); return OR or None."""
for val in (a, b, c, d):
if isinstance(val, bool) or not isinstance(val, int) or val < 0:
raise ValueError(f"Invalid 2x2 cell count: {val!r}")
if b == 0 or c == 0:
return None
return round((a * d) / (b * c), 2)10.8.4 D. Debugging Post-Mortem: Top 3 Chapter 10 Failure Modes
- Leaking
KeyErrororTypeErrorWhenValueErrorWas Promised: In Tasks 10.1 and 10.3, comparing"37.5" < 30.0or indexingraw_dict["hb_g_dl"]before checking types and keys raisesTypeErrororKeyError, slipping past a batch handler that listens only forexcept ValueError:. Always validate structure and types first so every contract violation surfaces consistently asValueError. - Forgetting That
boolIs a Subclass ofint: Callingisinstance(True, int)returnsTruein Python. In clinical counts (compute_safe_odds_ratio) and vital-sign validators (validate_body_temp), always rejectisinstance(val, bool)explicitly before checkingisinstance(val, int). - Wrapping the Outer Loop Instead of Each Record: Placing
try / except ValueError:outsidefor record in raw_records:aborts the entire cohort the instant one bad tablet entry appears. Always keeptry / excepttightly scoped to the single operation that can fail, and verify your recovery logic with automatedasserttests!
You now possess the safety net behind every trustworthy computational publication: narrow exception handling, crisp raise ValueError versus None contracts, quarantine-capable batch pipelines, and automated assert verification suites. Every Part III–V dataset you analyze from here onward will rest on code that fails loudly when data breaks and proves its math automatically before submission.


