flowchart TD
subgraph Local["Local Machine"]
direction LR
W["1. Working Dir<br/>Edit scripts &<br/>notebooks"] -->|"git add"| S["2. Staging Area<br/>Next snapshot<br/>(Index)"]
S -->|"git commit"| L["3. Local Repo<br/>Commit history<br/>(.git/ SHA-1)"]
end
subgraph Remote["Shared Origin"]
direction LR
R["4. GitHub Remote<br/>Shared origin, Pull Requests & Peer Review"]
end
Local -->|"git push"| Remote
Remote -. "git pull" .-> Local
15 Version Control with Git & GitHub, Clean Code, Type Hints, and Research Project Templates
Chapter Mission (Part III Capstone): Transform personal analysis scripts into auditable, peer-review-ready scientific software. Master Git’s three-zone architecture and feature branching, automated
.gitignoreand secret-leak guards, PEP 8 / Ruff code hygiene, Python type hints (TypedDict,Optional), structuredloggingwith leak-proofFileHandlercleanup, cryptographic SHA-256 dataset provenance manifests, and standardized Cookiecutter research templates.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch15 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch15 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch15 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this Part III Capstone chapter, you will be able to:
- Navigate Git’s three local zones (Working Directory, Staging Area, Local Repository) and collaborate safely via branches, clean commit subjects, and GitHub Pull Requests.
- Audit
.gitignorerules programmatically withfnmatchand detect leaked credentials (ghp_,AIza,sk-,API_KEY=,PASSWORD=) before a commit ever leaves your laptop. - Enforce readable, self-documenting research code using PEP 8, Ruff linting/formatting, Python type annotations, and
TypedDictreturn contracts. - Replace ephemeral
print()statements with severity-leveledloggingpipelines whoseFileHandlerresources always detach and close insidefinally:blocks. - Fingerprint raw clinical CSV and JSON datasets with
hashlib.sha256()to build and verify cryptographic provenance manifests that detect even a single-byte edit. - Organize multi-month studies inside a clean Cookiecutter research repository template and pass a 100-point Part III Capstone Engineering Exam.
15.1 Stage 1 — First-Principles Intuition: Why analysis_FINAL_v3_fixed.py Fails Peer Review
Six months after submitting a manuscript, Reviewer #2 asks a routine question: “Table 2 reports a mean maternal hemoglobin of \(11.15\text{ g/dL}\) across \(N = 412\) mothers, yet your supplementary file has \(415\) rows—which three records were dropped, and which version of the cleaning script generated Figure 1?”
If your research folder contains clean_v1.py, clean_v2_new.py, and clean_FINAL_really.py alongside an unlogged overwrite of raw_data.csv, answering Reviewer #2 becomes a forensic nightmare. Professional scientific engineering eliminates this guesswork with four interlocking pillars:
Think of a reproducible research repository like a regulated clinical trial binder:
- Tamper-Evident History (Git & GitHub): Every edit is recorded as an immutable, cryptographically chained commit with a concise subject line, author, and timestamp—never overwritten by
v2_finalfilenames. - Confidentiality Wall (
.gitignore& Secret Scanners): Raw patient registries (*.csv) and credentials (.env, API tokens) stay strictly off GitHub while code, tests, and tiny provenance manifests are shared openly. - Typed, Self-Explaining Contracts (PEP 8, Ruff & Type Hints): Consistent formatting and explicit types (
list[float],Optional[float],TypedDict) turn silent runtime surprises into immediate editor warnings. - Chain of Custody (
logging& SHA-256 Fingerprints): Every pipeline run records its exact input file’s 64-character SHA-256 hash and logs every skipped sentinel row (INFO/WARNING) to disk.
15.2 Stage 2 — Visual Mental Model: Git’s 3-Zone & Remote Workflow
Before memorizing commands, lock in the four physical spaces where your research code lives and the exact commands that move snapshots between them:
Understanding those four zones explains almost every Git command you will ever run:
git add <file>copies selected changes from your Working Directory into the Staging Area, letting you bundle related edits into one atomic commit while leaving half-finished experiments unstaged.git commit -m "..."seals the Staging Area into a permanent, content-addressed snapshot inside.git/on your local machine—no internet connection required.git switch -c feat/hb-auditcreates an isolated feature branch somainalways stays clean and paper-ready until your Pull Request passes review andpytest..gitignore+ Pre-Commit Guard: Anything matching.gitignorenever enters the Staging Area in the first place, keeping raw clinical files and.envsecrets safely local.
15.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_03_engineering_workflow/ch15_git_clean_code/01_walkthrough.ipynb in Colab and execute every cell alongside the dissections below.
15.3.1 Everyday Git Workflow, .gitignore Auditing & Secret Scanning
In your local terminal (outside Colab), a clean feature-branch research cycle uses just seven core commands:
git status # Inspect working tree & stage
git switch -c feat/add-sha256-manifest # Isolate new work on a branch
git add src/provenance.py tests/ # Stage only reviewed code
git diff --staged # Audit exact lines before commit
git commit -m "Add SHA-256 manifest builder for cohort CSVs"
git log --oneline -n 5 # Verify concise commit history
git push -u origin feat/add-sha256-manifestNotice the commit subject rule enforced by professional teams: keep the first line imperative, concise (\(5 \le \text{len} \le 72\) characters), and free of a trailing period so git log --oneline and GitHub Pull Request headers never truncate awkwardly.
Now let’s audit .gitignore coverage with Python’s standard-library fnmatch and scan a configuration snippet for leaked credentials—run this cell verbatim in 01_walkthrough.ipynb:
import fnmatch
gitignore_text = (
"# Never commit raw clinical data or secrets\n"
"*.csv\n"
".env\n"
"*.log\n"
"__pycache__/*\n"
)
staged_candidates = [
"README.md",
"src/clean_cohort.py",
"raw_mch_2026.csv",
".env",
"run.log",
]
patterns = [
line.strip()
for line in gitignore_text.splitlines()
if line.strip() and not line.strip().startswith("#")
]
tracked = sorted(
fname for fname in staged_candidates
if not any(fnmatch.fnmatch(fname, pat) for pat in patterns)
)
print("Active .gitignore patterns:", patterns)
print("Safe to track in Git:", tracked)
config_sample = (
"STUDY_ID = 'MCH-BD-2026'\n"
"TOKEN = 'ghp_exampleLeak999'\n"
"API_KEY=demo_secret_token\n"
)
secret_tags = ("ghp_", "AIza", "sk-", "API_KEY=", "PASSWORD=")
flagged_lines = [
idx for idx, ln in enumerate(config_sample.splitlines(), start=1)
if any(tag in ln for tag in secret_tags)
]
print("Flagged secret lines (1-based):", flagged_lines)Expected Output:
Key mechanics to inspect closely:
- Skipping Comments & Blank Lines: Lines starting with
#are documentation inside.gitignore; failing to skip them would mistakenly treat# *.pyas an ignore rule. - Negating
any(...):not any(fnmatch.fnmatch(fname, pat) ...)returns the unignored files—the exact files Git is allowed to commit. - 1-Based Line Numbers:
enumerate(..., start=1)aligns flagged secret line numbers directly with VS Code, Vim, and GitHub diff line numbers.
15.3.2 Clean Code (PEP 8 / Ruff), Type Hints, and TypedDict Contracts
Clean research code follows PEP 8 conventions—snake_case function/variable names, PascalCase class names, UPPER_CASE constants, 4-space indentation, short readable lines, and one clear responsibility per function. Modern Python teams enforce these rules automatically in milliseconds with Ruff:
Pairing Ruff with Python type hints (list[float], Optional[float], and TypedDict) documents the exact shape of every input and output without changing runtime speed:
from typing import Optional, TypedDict
class CohortSummary(TypedDict):
site: str
valid_n: int
mean_hb: Optional[float]
def summarize_site_hb(
site: str,
readings: list[float],
) -> CohortSummary:
"""Return a typed hemoglobin summary for one clinic site."""
clean = [val for val in readings if 3.0 <= val <= 22.0]
mean_val: Optional[float] = (
round(sum(clean) / len(clean), 2) if clean else None
)
return {
"site": site.strip().title(),
"valid_n": len(clean),
"mean_hb": mean_val,
}
dhaka_report = summarize_site_hb(" dhaka ", [10.4, 999.0, 11.6])
empty_report = summarize_site_hb("sylhet", [999.0])
print("Dhaka summary:", dhaka_report)
print("Empty summary:", empty_report)Expected Output:
Notice how Optional[float] explicitly warns every collaborator (and static type checkers like mypy or pyright) that mean_hb can be None when a cohort has zero valid rows—preventing downstream TypeError crashes before the pipeline ever runs.
15.3.3 Structured logging vs. print() (With Clean Handler Teardown)
print() is handy for quick interactive exploration, but it vanishes from terminal scrollback, has no severity levels (DEBUG, INFO, WARNING, ERROR, CRITICAL), and cannot be routed cleanly to an audit log file. Python’s standard-library logging module solves all three problems:
import logging
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
log_path = Path(tmp) / "cohort_run.log"
logger = logging.getLogger("ch15_walkthrough_demo")
logger.setLevel(logging.INFO)
logger.propagate = False
handler = logging.FileHandler(log_path, mode="w", encoding="utf-8")
fmt = logging.Formatter("[%(levelname)s] %(message)s")
handler.setFormatter(fmt)
logger.addHandler(handler)
try:
logger.info("Pipeline started for Dhaka ANC cohort")
logger.warning("Row 4: skipped sentinel hb_g_dl='999'")
logger.info("Pipeline completed: 2 valid, 1 skipped")
finally:
logger.removeHandler(handler)
handler.close()
print(log_path.read_text(encoding="utf-8").strip())Expected Output:
Because logging.getLogger(name) returns the same singleton logger object across repeated function calls in a notebook or test suite, forgetting logger.removeHandler(handler) causes every future call to write duplicate lines, and forgetting handler.close() leaks open file handles that block tempfile.TemporaryDirectory() cleanup on Windows. Wrapping teardown in finally: guarantees spotless cleanup every single time.
15.3.4 Cryptographic Dataset Provenance (hashlib.sha256) & Research Templates
Because raw clinical CSV files stay out of Git (.gitignore), how do you prove six months later that data/raw/mch_v1.csv was never accidentally edited in Excel? You compute its SHA-256 cryptographic digest—a deterministic 64-character hexadecimal fingerprint where flipping even a single digit changes roughly half of the output bits (the avalanche effect):
import hashlib
from pathlib import Path
import tempfile
def sha256_file(filepath: Path) -> str:
"""Compute lowercase 64-char SHA-256 hex digest in 64 KB chunks."""
hasher = hashlib.sha256()
with open(filepath, "rb") as f:
while chunk := f.read(65536):
hasher.update(chunk)
return hasher.hexdigest()
with tempfile.TemporaryDirectory() as tmp:
raw_csv = Path(tmp) / "mch_v1.csv"
raw_csv.write_text("id,hb_g_dl\nP1,10.8\n", encoding="utf-8")
digest_v1 = sha256_file(raw_csv)
# Flip a single digit (10.8 -> 10.9) to demonstrate avalanche effect
raw_csv.write_text("id,hb_g_dl\nP1,10.9\n", encoding="utf-8")
digest_v2 = sha256_file(raw_csv)
print("Digest v1 (first 16):", digest_v1[:16], "len=", len(digest_v1))
print("Digest v2 (first 16):", digest_v2[:16], "len=", len(digest_v2))
print("Identical files?:", digest_v1 == digest_v2)Expected Output:
Combine Git, .gitignore, type-checked src/ modules, tests/, and a tiny committed data/manifest.json inside a standardized Cookiecutter Data Science research project layout:
mch_anemia_study/
├── .gitignore # Blocks data/raw/*.csv, .env, *.log, cache
├── README.md # Study overview, setup & reproduction steps
├── pyproject.toml # Dependencies, Ruff & pytest configuration
├── data/
│ ├── manifest.json # Committed SHA-256 fingerprints (no PHI!)
│ ├── raw/ # Read-only source CSVs (gitignored)
│ └── processed/ # Regenerable clean cohorts (gitignored)
├── notebooks/ # Numbered exploratory notebooks (01_eda.ipynb)
├── src/
│ └── mch_study/ # Reusable, type-hinted Python modules
├── tests/ # Automated pytest suite (Chapter 10)
└── reports/ # Figures & manuscript tables15.3.5 Break It On Purpose! (Provenance & Logging Error Anatomy)
Python’s Traceback:
Diagnosis: Calling open(path, "rb") or path.read_bytes() assumes the dataset exists on disk. In a provenance verifier, a deleted file should be reported cleanly as "missing" rather than crashing the audit script.
Fix: Guard with if not fpath.is_file(): before attempting to read bytes or compute a digest.
Python’s Traceback:
Diagnosis: Cryptographic hashes operate on raw bytes, never Unicode str objects (because the same characters can be encoded differently in UTF-8 vs. UTF-16). Passing a text string—or opening a file in text mode "r" instead of "rb"—immediately triggers TypeError.
Fix: Open files in binary mode ("rb") or call csv_text.encode("utf-8") before passing data into hashlib.sha256(...).
Python’s Traceback:
Diagnosis: Python’s logging module recognizes only the standard severity names ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL") or their integer constants. Passing a custom string like "URGENT" or a lowercase string like "info" directly to setLevel(...) raises ValueError.
Fix: Normalize input with .strip().upper() and validate it against the five standard level names before logging.
15.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: Before tagging v1.0.0 on GitHub for manuscript submission, your PI wants a single automated gatekeeper function prepare_release_bundle(repo_dir, log_path) that: (1) hashes every .csv and .json file inside repo_dir / "data" into repo_dir / "manifest.json", (2) scans all .py scripts inside repo_dir / "src" for leaked secret prefixes, (3) writes an INFO/WARNING audit log to log_path with clean handler teardown, and (4) returns a typed ReleaseAudit dictionary indicating whether the bundle is release_ready.
15.4.1 Blueprint (Completed on Paper Before Coding)
- Input & Output Contract:
- Inputs:
repo_dir: Path(containingdata/andsrc/) andlog_path: Path. - Side effects: Indented
manifest.jsonwritten insiderepo_dir; audit log written cleanly tolog_path. - Return value:
ReleaseAudit(TypedDict) withdata_files,manifest_path,secret_leaks, andrelease_ready.
- Inputs:
- Edge Cases & Traps:
- Never leave the
FileHandlerattached or open—placelogger.removeHandler(handler)andhandler.close()insidefinally:. - Sort
data/entries by filename somanifest.jsondiffs stay deterministic in Git. - Check
p.is_file()before reading bytes so subdirectories never raiseIsADirectoryError.
- Never leave the
- Step-by-Step Algorithm:
- Configure an isolated logger (
propagate = False) with a UTF-8FileHandler. - Inside
try:, hash all regular.csv/.jsonfiles inrepo_dir / "data"intomanifest. - Scan every
*.pyfile inrepo_dir / "src"line by line (1-based) for the five secret tokens; log aWARNINGfor every leak. - Write
manifest.jsonwithindent=2, computeready = len(manifest) > 0 and leaks == 0, and log final status. - Detach and close the handler in
finally:, then return the typedReleaseAuditdict.
- Configure an isolated logger (
- Verification Plan (Hand Check):
- 1 CSV file in
data/+ 1 clean Python script insrc/\(\rightarrow\)data_files = 1,secret_leaks = 0,release_ready = True, and 2INFOlog lines.
- 1 CSV file in
15.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
import hashlib
import json
import logging
from pathlib import Path
import tempfile
from typing import TypedDict
class ReleaseAudit(TypedDict):
data_files: int
manifest_path: str
secret_leaks: int
release_ready: bool
def prepare_release_bundle(
repo_dir: Path,
log_path: Path,
) -> ReleaseAudit:
"""Build a SHA-256 manifest, scan src/ for secrets, and log audit."""
logger = logging.getLogger("ch15_release_gate")
logger.setLevel(logging.INFO)
logger.propagate = False
handler = logging.FileHandler(log_path, mode="w", encoding="utf-8")
handler.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
logger.addHandler(handler)
secret_tags = ("ghp_", "AIza", "sk-", "API_KEY=", "PASSWORD=")
manifest: dict[str, dict[str, object]] = {}
leaks = 0
try:
data_dir = repo_dir / "data"
for p in sorted(data_dir.iterdir(), key=lambda x: x.name):
if p.is_file() and p.suffix.lower() in {".csv", ".json"}:
digest = hashlib.sha256(p.read_bytes()).hexdigest()
manifest[p.name] = {
"bytes": p.stat().st_size,
"sha256": digest,
}
logger.info("Hashed %d dataset file(s)", len(manifest))
src_dir = repo_dir / "src"
for py_file in sorted(src_dir.glob("*.py")):
text = py_file.read_text(encoding="utf-8")
for ln_no, line in enumerate(text.splitlines(), start=1):
if any(tag in line for tag in secret_tags):
leaks += 1
logger.warning(
"Leak in %s line %d", py_file.name, ln_no
)
manifest_file = repo_dir / "manifest.json"
with open(manifest_file, "w", encoding="utf-8") as out_f:
json.dump(manifest, out_f, indent=2)
ready = len(manifest) > 0 and leaks == 0
logger.info("Release ready: %s", ready)
finally:
logger.removeHandler(handler)
handler.close()
return {
"data_files": len(manifest),
"manifest_path": manifest_file.name,
"secret_leaks": leaks,
"release_ready": ready,
}
with tempfile.TemporaryDirectory() as tmp:
repo = Path(tmp)
(repo / "data").mkdir()
(repo / "src").mkdir()
(repo / "data" / "mch_clean.csv").write_text(
"id,hb_g_dl\nP1,11.4\n", encoding="utf-8"
)
(repo / "src" / "analyze.py").write_text(
"STUDY = 'MCH-BD-2026'\n", encoding="utf-8"
)
audit_log = repo / "release_audit.log"
result = prepare_release_bundle(repo, audit_log)
print("Audit Result:\n" + json.dumps(result, indent=2))
print("Audit Log:\n" + audit_log.read_text(encoding="utf-8").strip())Output:
15.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_03_engineering_workflow/ch15_git_clean_code/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and implement all six reproducible-engineering functions below:
- To-Do 15.1 (
.gitignoreAuditing —check_gitignore_coverage(gitignore_text, filenames)): Parse non-blank, non-#patterns fromgitignore_text, match withfnmatch.fnmatch, and return a sorted list of UNIGNORED filenames. - To-Do 15.2 (Commit Hygiene —
validate_commit_message(message)): Inspect only the first subject line (non-empty, \(5 \le \text{len} \le 72\), no trailing".") and return(is_valid: bool, reason: str). - To-Do 15.3 (Secret Scanner —
scan_for_leaked_secrets(text)): Scan lines 1-indexed for case-sensitive tokensghp_,AIza,sk-,API_KEY=, andPASSWORD=, returning a sorted list of unique 1-based matching line numbers. - To-Do 15.4 (Structured Logging —
write_structured_log(log_path, level, message)): Append a formatted"[LEVEL] message"UTF-8 log line with uppercase level and properly closed/detached file handler. - To-Do 15.5 (Cryptographic Hashing —
compute_file_sha256(filepath)): Stream binary bytes ("rb") intohashlib.sha256()and return the lowercase 64-character hex digest. - To-Do 15.6 (Dataset Manifest —
build_dataset_manifest(data_dir)): Scan regular.csvand.jsonfiles indata_dirsorted by name, returningfilename -> {"bytes": int, "sha256": str}.
15.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Test your Part III engineering intuition on paper before checking Stage 8:
- Git 3-Zone Mechanics: You edited
clean.pyandnotes.txt, rangit add clean.py, and then rangit commit -m "Fix sentinel filter". Which file is included in the new commit, and where do the edits tonotes.txtremain? - Branch Isolation: Why should you run
git switch -c feat/anemia-modelinstead of committing experimental model changes directly ontomain? .gitignoreComment Trap: A.gitignorecontains# *.pyon line 1 and*.csvon line 2. Shouldanalysis.pybe returned bycheck_gitignore_coverageas unignored? Why?- Multi-Line Commit Subjects: A commit message has a 30-character first line (no period), a blank line, and a 120-character second paragraph ending with a period. Should
validate_commit_messagereturnTrueorFalse? - Case-Sensitive Secret Scanning: Why does
scan_for_leaked_secretsmatchAPI_KEY=abcandghp_xyz, yet ignoreapi_key=placeholder? - Runtime Type Hints: What happens when Python executes
def mean_hb(vals: list[float]) -> float:and a caller passes atupleof floats at runtime? Do Python type hints automatically raiseTypeError? - Duplicate Log Lines Bug: You write a helper function that creates
handler = logging.FileHandler(path)and callslogger.addHandler(handler)on every invocation without callinglogger.removeHandler(handler). After calling the function 3 times, how many total lines appear in the log file (\(1 + 2 + 3\))? - Binary Mode for Hashing: Why does
hashlib.sha256(open(path, "r").read())raiseTypeErroreven on a valid UTF-8.csvfile? - SHA-256 Avalanche Property: A \(10\text{ MB}\) clinical CSV has one value changed from
10.8to10.9. How much of its 64-character SHA-256 digest changes, and why is that useful for data provenance? - Manifest vs. Raw Data in Git: Why do reproducible research templates put
data/raw/*.csvinside.gitignorewhile committingdata/manifest.jsondirectly into Git?
15.7 Stage 7 — Part III Capstone Hands-On Coding Exam (100 Points)
Open part_03_engineering_workflow/ch15_git_clean_code/03_coding_exam.ipynb, complete all three capstone engineering tasks, and run tutor.check_exam("ch15", exam_submissions) to earn your Part III Capstone score out of 100 points.
15.7.1 Exam Task 15.1 (30 Points): Pre-Push Repository Hygiene Auditor
Write audit_repo_hygiene(repo_dir) to inspect a research repository directory before pushing to GitHub:
- Check four boolean conditions on
Path(repo_dir):has_readme:README.mdexists as a regular file.has_gitignore:.gitignoreexists as a regular file.has_env_leak:.envexists as a regular file.has_checkpoints:.ipynb_checkpointsexists as a directory.
- Compute
ready_to_push = has_readme and has_gitignore and not has_env_leak and not has_checkpoints. - Return a dictionary containing all five boolean keys.
15.7.2 Exam Task 15.2 (35 Points): Cryptographic Dataset Provenance Verifier
Write verify_data_provenance(data_dir, manifest_dict) to verify datasets on disk against a saved manifest (filename -> {"bytes": int, "sha256": str}):
- Iterate over
sorted(manifest_dict)so all output lists stay alphabetically sorted. - If
Path(data_dir) / fnamedoes not exist as a file, recordfnamein"missing". - Otherwise recompute its SHA-256 hex digest and place
fnameinto"matched"(if equal tomanifest_dict[fname]["sha256"]) or"modified"(if different). - Return
{"matched": ..., "modified": ..., "missing": ..., "all_verified": bool}whereall_verifiedisTrueiff both"modified"and"missing"are empty.
15.7.3 Exam Task 15.3 (35 Points): Logged, Provenance-Tracked Cleaning Pipeline
Write run_logged_cleaning_pipeline(in_csv, out_json, log_path) combining CSV cleaning, SHA-256 provenance, structured logging, and indented JSON export:
- Compute the lowercase 64-character SHA-256 hex digest of
in_csv. - Read
in_csvwithcsv.DictReaderexpecting columnhb_g_dl; skip"","N/A", and"999"while countingrejected_countand emittingWARNINGlog lines tolog_path, and collect validfloatvalues. - Emit
INFOlog lines for pipeline start and completion, and guaranteeFileHandlerdetachment and closure in afinally:block. - Compute
mean_hbrounded to 2 decimal places (orNoneifvalid_count == 0). - Write indented UTF-8 JSON (
indent=2) with keys{"source_sha256", "valid_count", "rejected_count", "mean_hb"}toout_jsonand return that dictionary.
15.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
15.8.1 A. Concept & Execution Trace Test Key
- Only
clean.pyis recorded in the commit; the edits tonotes.txtremain safely in the Working Directory (unstaged). - Feature branches keep
mainalways clean, tested, and ready to reproduce published tables while you iterate safely—and let collaborators review changes via a Pull Request before merging. - Yes,
analysis.pyis returned as unignored because lines beginning with#are comments in.gitignore, not active ignore patterns. True—commit subject validation checks only the first subject line, allowing longer explanatory paragraphs and punctuation in the commit body.- Real provider prefixes and environment variable keys (
ghp_,AIza,sk-,API_KEY=,PASSWORD=) are case-sensitive; matching case-sensitively avoids false alarms on ordinary lowercase variable names or prose comments. - No—Python executes the function normally without raising
TypeErrorbecause type hints are metadata checked by static tools (mypy,pyright, IDE linters), not enforced at runtime. - \(1 + 2 + 3 = 6\) total lines! Call 1 attaches handler #1 (1 line), call 2 attaches handler #2 (writes 2 lines), and call 3 attaches handler #3 (writes 3 lines). Always call
logger.removeHandler(handler)andhandler.close()infinally:. open(path, "r").read()returns a decoded Unicodestr, whereashashlib.sha256()strictly requires rawbytes(from binary mode"rb").- Roughly half of all 64 hex characters change completely (producing a totally unrecognizable digest), so any silent spreadsheet edit, truncated download, or version mix-up is detected with near-certainty.
- Raw
.csvfiles may be huge or contain restricted patient/field data that must never be uploaded to public GitHub, whereasdata/manifest.jsonis tiny, contains zero patient records, and cryptographically locks the exact dataset version required to reproduce the paper.
15.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import fnmatch
import hashlib
import logging
from pathlib import Path
# To-Do 15.1
def check_gitignore_coverage(gitignore_text, filenames):
"""Return sorted list of filenames NOT matched by .gitignore."""
patterns = [
line.strip()
for line in gitignore_text.splitlines()
if line.strip() and not line.strip().startswith("#")
]
unignored = [
fname for fname in filenames
if not any(fnmatch.fnmatch(fname, pat) for pat in patterns)
]
return sorted(unignored)
# To-Do 15.2
def validate_commit_message(message):
"""Validate first commit subject line; return (is_valid, reason)."""
lines = message.splitlines()
subject = lines[0].strip() if lines else ""
if not subject:
return (False, "Subject line cannot be empty")
if len(subject) < 5:
return (False, "Subject line must be at least 5 characters")
if len(subject) > 72:
return (False, "Subject line must be at most 72 characters")
if subject.endswith("."):
return (False, "Subject line should not end with a period")
return (True, "Valid commit subject")
# To-Do 15.3
def scan_for_leaked_secrets(text):
"""Return 1-based line numbers containing sensitive secret tokens."""
patterns = ("ghp_", "AIza", "sk-", "API_KEY=", "PASSWORD=")
return [
line_no
for line_no, line in enumerate(text.splitlines(), start=1)
if any(pat in line for pat in patterns)
]
# To-Do 15.4
_LOG_LEVELS = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
def write_structured_log(log_path, level, message):
"""Append '[LEVEL] message' to log_path via a closed FileHandler."""
level_name = level.strip().upper()
if level_name not in _LOG_LEVELS:
raise ValueError(f"Unknown log level: {level!r}")
logger = logging.getLogger("ch15_todo_logger")
logger.setLevel(logging.DEBUG)
logger.propagate = False
handler = logging.FileHandler(log_path, mode="a", encoding="utf-8")
handler.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
logger.addHandler(handler)
try:
logger.log(_LOG_LEVELS[level_name], message)
finally:
logger.removeHandler(handler)
handler.close()
# To-Do 15.5
def compute_file_sha256(filepath):
"""Return lowercase 64-char SHA-256 hex digest of filepath."""
hasher = hashlib.sha256()
with open(filepath, "rb") as f:
while chunk := f.read(65536):
hasher.update(chunk)
return hasher.hexdigest()
# To-Do 15.6
def build_dataset_manifest(data_dir):
"""Map sorted .csv/.json filenames to byte size and SHA-256."""
manifest = {}
for p in sorted(Path(data_dir).iterdir(), key=lambda x: x.name):
if p.is_file() and p.suffix.lower() in {".csv", ".json"}:
digest = hashlib.sha256(p.read_bytes()).hexdigest()
manifest[p.name] = {
"bytes": p.stat().st_size,
"sha256": digest,
}
return manifest15.8.3 C. Annotated Solutions — Part III Capstone Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
import csv
import hashlib
import json
import logging
from pathlib import Path
# Exam Task 15.1 (30 pts)
def audit_repo_hygiene(repo_dir):
"""Inspect repo_dir for docs, .gitignore, .env leaks & checkpoints."""
root = Path(repo_dir)
has_readme = (root / "README.md").is_file()
has_gitignore = (root / ".gitignore").is_file()
has_env_leak = (root / ".env").is_file()
has_checkpoints = (root / ".ipynb_checkpoints").is_dir()
ready_to_push = (
has_readme
and has_gitignore
and not has_env_leak
and not has_checkpoints
)
return {
"has_readme": has_readme,
"has_gitignore": has_gitignore,
"has_env_leak": has_env_leak,
"has_checkpoints": has_checkpoints,
"ready_to_push": ready_to_push,
}
# Exam Task 15.2 (35 pts)
def verify_data_provenance(data_dir, manifest_dict):
"""Verify data_dir files against manifest_dict SHA-256 digests."""
root = Path(data_dir)
matched = []
modified = []
missing = []
for fname in sorted(manifest_dict):
fpath = root / fname
if not fpath.is_file():
missing.append(fname)
continue
actual_sha = hashlib.sha256(fpath.read_bytes()).hexdigest()
if actual_sha == manifest_dict[fname]["sha256"]:
matched.append(fname)
else:
modified.append(fname)
return {
"matched": matched,
"modified": modified,
"missing": missing,
"all_verified": len(modified) == 0 and len(missing) == 0,
}
# Exam Task 15.3 (35 pts)
def run_logged_cleaning_pipeline(in_csv, out_json, log_path):
"""Hash, clean, log, and summarize hb_g_dl CSV to indented JSON."""
src_sha = hashlib.sha256(Path(in_csv).read_bytes()).hexdigest()
logger = logging.getLogger("ch15_capstone_pipeline")
logger.setLevel(logging.INFO)
logger.propagate = False
handler = logging.FileHandler(log_path, mode="w", encoding="utf-8")
handler.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
logger.addHandler(handler)
valid_vals = []
rejected = 0
try:
logger.info("Pipeline started (sha256=%s)", src_sha)
with open(in_csv, "r", encoding="utf-8", newline="") as in_f:
for row_num, row in enumerate(csv.DictReader(in_f), start=1):
raw = row["hb_g_dl"].strip()
if raw in ("", "N/A", "999"):
rejected += 1
logger.warning("Row %d rejected: %r", row_num, raw)
else:
valid_vals.append(float(raw))
mean_hb = (
round(sum(valid_vals) / len(valid_vals), 2)
if valid_vals
else None
)
summary = {
"source_sha256": src_sha,
"valid_count": len(valid_vals),
"rejected_count": rejected,
"mean_hb": mean_hb,
}
logger.info(
"Pipeline finished: valid=%d, rejected=%d",
len(valid_vals),
rejected,
)
finally:
logger.removeHandler(handler)
handler.close()
with open(out_json, "w", encoding="utf-8") as out_f:
json.dump(summary, out_f, indent=2)
return summary15.8.4 D. Debugging Post-Mortem: Top 3 Chapter 15 Failure Modes
- Leaking
logging.FileHandlerAcross Calls: Adding aFileHandlerto a named logger withoutlogger.removeHandler(handler)andhandler.close()inside afinally:block silently duplicates every subsequent log message and can locktempfile.TemporaryDirectory()folders on cleanup. - Hashing Text Mode Instead of Binary Mode (
"r"vs."rb"): Passing decoded strings intohashlib.sha256()triggersTypeError: Strings must be encoded before hashing(orUnicodeDecodeErroron binary assets). Always read raw bytes viaopen(path, "rb")orPath(path).read_bytes(). - Forgetting Existence & Negation Guards: Returning matched files instead of unignored files in
.gitignoreaudits, or hashing a missing manifest entry without checkingfpath.is_file()first, flips security checks upside down or crashes mid-audit withFileNotFoundError.
Congratulations on completing Part III (Software Engineering & Reproducible Research Workflow)! You now command the full modern Python engineering toolkit—modules, environments, object-oriented design, Git & GitHub version control, type hints, structured logging, and cryptographic dataset provenance. In Part IV, you will put these reproducible templates to work at scale with NumPy, Pandas, publication-grade data visualization, and biostatistical inference.


