Python Execution Model

Later ML code is still Python code. Precise reasoning about objects, imports, cleanup and package boundaries prevents silent state errors that framework knowledge cannot repair.

Section 1 of 5 · 9 units · about 32 hours

0/9

By the end you can Build and install a typed src-layout package from a blank directory, then trace and repair one seeded aliasing, cleanup or import defect without trial-and-error edits.

Units

Build it

Mini project · about 8 to 12 hours

Installable taskrunner package with a typed registry and two seeded defects

Build the installable task runner package the later projects extend, with a typed task registry, a lazy run log and deterministic cleanup. Two seeded defects, one aliasing and one lifetime, train evidence-first diagnosis with a regression test for each.

  1. 1Scaffold a src-layout package with uv init, build its wheel with uv build, install the wheel into a fresh environment and run its console entry point outside the repository, showing the installed copy is imported.
  2. 2Model tasks as a frozen slotted dataclass validated in __post_init__, registered by a decorator that rejects duplicates, and checked against the AlgoMaster Python Data Classes and Decorators exercises, with mypy --strict clean.
  3. 3Stream the JSON Lines run log through a generator and build the summary from generator expressions, then predict which peak grows with file size and compare peaks with tracemalloc against a list-building version.
  4. 4Give each run a directory through a context manager that writes a marker and closes the log on every path, map failures to a small chained exception hierarchy, and test every path.
  5. 5Seed an aliasing defect where a shallow copy shares nested run parameters across runs, write the regression test first, watch it fail, then fix it and explain why the chosen copy strategy removes the sharing.
  6. 6Seed a cleanup defect where a stored bound method keeps a cycle alive, diagnose it with a weakref probe and the collector, then fix it so the regression test passes with the collector disabled.
What you hand in
  • The taskrunner repository: src-layout package, wheel, and a README with install and import evidence.
  • Tests for validation, registration, laziness, cleanup and causes, with mypy --strict clean.
  • Two defect write-ups with failing and passing regression output, diagnosis evidence and the laziness peak comparison.
How to check it
  • Self-grade with the rubric below
How it's graded

Installable package

Pass: The wheel installs into a fresh environment and the entry point runs outside the repository.

Great: A test asserts the imported package is the installed copy, and the README rebuilds it from a clean checkout.

Typed model

Pass: TaskSpec rejects bad values and round-trips, duplicates are rejected, and mypy --strict is clean.

Great: Decorated tasks keep their own type, and the write-up shows the rejected Protocol typing and why.

Resources and laziness

Pass: Cleanup and markers hold on every path with causes chained, and the learner's tests cover each path.

Great: The peak comparison shows the generator holding flat while the list version grows, as predicted.

Seeded defects

Pass: Both regression tests fail on the seeded code and pass after the fix.

Great: Each write-up names the shared reference or cycle behind the bug and why the fix removes it.

Resources

  • Stas' Python Cookbook - selected pathStas Bekman

    Run Part I on dictionaries, iteration, functions, closures, decorators, classes and context managers; Part II on modules, files, paths, environment and arguments; and Part IV on versions, dependencies, packaging and code quality. Finish by building one src-layout package from a blank directory.

  • Python language referencePython Software Foundation

    Check Data model, Execution model, Import system and Exceptions whenever a claim depends on Python semantics. These pages are the authority when the course and a runtime observation disagree.

  • Packaging Python ProjectsPython Packaging Authority

    Follow the packaging tutorial once from a blank directory, using a src layout, pyproject.toml, an isolated build and a clean-environment installation test.

  • AlgoMaster Python - structured revisionAlgoMaster

    Use Functions, Modules and Packages, OOP, Advanced OOP, Exception Handling, Iterators and Generators, Type Hints and Annotations only after the cookbook pass. Open it for recall practice or a diagnosed weak mechanism, not as a second complete course.

  • dataclasses - data classesPython Software Foundation

    Read the @dataclass parameters (eq, order, frozen, slots), field() with default_factory, frozen instances, post-init processing, replace, asdict and astuple. Use for the validated frozen dataclass in attributes-protocols-dataclasses.

  • typing - type hints and protocolsPython Software Foundation

    Read Protocols for structural typing and runtime_checkable limits, Generics for TypeVar generics, and assert_type for narrowing checks. Use for the Protocol and narrowing parts of type-hints-protocols; PEP 695 syntax itself is in the language reference.

  • gc - cycle garbage collectorPython Software Foundation

    Read the cycle collector interface: collect, get_count, garbage and disable/enable. Use for the cycle-collection and measurement parts of refcounting-gc-weakrefs.

  • weakref - weak referencesPython Software Foundation

    Read weak-reference objects (ref callbacks, proxy objects) and the WeakValueDictionary example. Use for the weak-reference part of refcounting-gc-weakrefs.