Skip to content
← Back to explainers

Python explainer

Before You Start a Capstone

About 4 min to read. Allow extra time to work through examples.

Each capstone combines four earlier items into one larger exercise. See which items each capstone builds on, and how to build and test it one part at a time.

This article uses Python. You can make it your practice mode to browse related content across the site.

A capstone statement is long. The order-model capstone has more than twenty rules, and its tests check all of them at once. If you write the whole thing and then press Run, a failing case tells you which value in the report is wrong. It does not tell you which rule you broke.

So build one part at a time. Pick a small rule, write the code for it, and check it with a few assert lines before you go on. Here is a first check for the import-rules capstone, where an entry such as "pricing.domain" covers that module and every module inside it:

def covers(entry: str, module: str) -> bool:
    return module == entry or module.startswith(entry + ".")


assert covers("pricing.domain", "pricing.domain")
assert covers("pricing.domain", "pricing.domain.rules")
assert not covers("pricing.domain", "pricing.domain_extra")

The third assert catches a real bug. A plain module.startswith(entry) would say that pricing.domain covers pricing.domain_extra.

Work through a capstone in this order

  1. Read the example and the "What the driver checks" section first. Together they show what your finished code returns.
  2. Read the starter. It supplies the types, the exceptions and the test driver, solve. Leave solve as it is.
  3. Write one part, and check it with a few assert lines in a scratch file on your own computer.
  4. Run mypy --strict on your file after each part. A type error is easier to fix while the new code is small. On the site, Run skips mypy and Submit runs it.
  5. Press Run once every part exists, and compare the report key by key with the expected output.
  6. Before you submit, read the edge-case rules once more. Submit also runs hidden test cases, and they test those rules.

What each capstone builds on

Each capstone uses ideas from four earlier items. If you get stuck on one part, go back to the item it comes from. The capstones do not depend on each other, so you can do them in any order.

Capstone: Give Each Upload State Its Own Type builds on:

Capstone: Check a Package's Layer and Import Rules builds on:

Capstone: Build an Immutable Order Model from Untrusted Data builds on:

Capstone: Extend the Worker Pool to Keep Going After Failures builds on:

Capstone: Fix Five Performance Problems in an Order Pipeline builds on:

Check your understanding Why check each part with its own assert lines before you press Run? Show answer

Run compares the whole report, and one wrong value in it can come from any of several parts. An assert on one part, with an input small enough to work out by hand, fails on the rule you broke. You also fix the bug while the new code is small, before other parts depend on it.

step 1 of 6

Capstone Projects

Up next Capstone: Give Each Upload State Its Own Type