Python theorytheory 0/50 · 0%
Tooling · medium

36. Working with argparse-style scripts (mocked)

Structuring reusable, testable script logic.

Even without running as a CLI, structuring script logic into small pure functions (rather than top-level code) makes it testable and reusable — this matters because the grader in this course calls your functions directly.

python
def parse_amount(raw: str) -> float:
    cleaned = raw.strip().replace(",", "")
    return float(cleaned)

def format_report(rows: list[dict]) -> str:
    lines = [f"{r['name']}: {r['value']}" for r in rows]
    return "\n".join(lines)

Separating "pure logic" functions from I/O (printing, file reads, network calls) means the logic can be tested without mocking the outside world — a core software engineering practice used constantly in real Python codebases, including blockchain tooling scripts.

Check your understanding

  1. 1. Why separate pure logic from I/O in scripts?

  2. 2. What does `raw.strip().replace(",", "")` do to " 1,000 "?

  3. 3. What return type does `format_report` produce?