Skip to content

Errors and generators

A ledger read from a generator, with the bad rows raised as exceptions and caught, and primes from a generator reduced by generator expressions, all in native code.

Run it

python  ledger.ppy
ppy run ledger.ppy
ppy build --standalone ledger.ppy -o dist && ./dist/ledger

What it prints

python ledger.ppy, ppy run ledger.ppy, ppy build --standalone ledger.ppy -o dist && ./dist/ledger

50 accounts, 10055 bad rows (longest message 23), richest 185856, overdrawn 30
78498 primes, widest gap 114, 8169 twin pairs, past the end -1

The program

  • ledger(count) is a generator. It yields 500,000 rows as text, acct7,-120 and the like; now and then a row has no comma, or no amount.
  • amount_of(row) splits a row with partition and raises BadRow, a subclass of ValueError, with a message saying what is missing.
  • settle loops over ledger(ROWS). Each row goes through amount_of in a try; the handler counts the bad row, keeps the longest str(error), and continues. The good rows add up per account in a HashMap. The richest balance and the number overdrawn come from max and sum over generator expressions.
  • primes(limit) yields the primes below 1,000,000 by trial division, and gaps builds a Vec from it, finds the widest gap and the twin pairs with generator expressions, and reads one past the end in a try that catches the IndexError.

How it runs natively

ppy explain ledger.amount_of, ledger.settle, and ledger.gaps each say llvm backend: native.

  • raise BadRow(...) makes an exception object holding the class, the message, and whether the message is CPython's. amount_of releases what it holds and returns a raised status.
  • The call in settle sits in a try, so a raised status goes to the handlers, which compare the exception's class with BadRow and the classes deriving from it. The continue in the handler lets go of the exception first.
  • found[len(found)] is a bounds check. In a module that raises or catches, a failed check raises CPython's IndexError natively, so the except IndexError takes it.
  • ledger and primes are not objects. Each is lowered into the loop that consumes it: the generator's body runs, and at each yield the loop's body runs with the value. A generator expression is lowered the same way.

Timing

Wall time for the whole program, the mean of five runs, measured from a checkout under /tmp on one machine (Python 3.14, an Intel Core Ultra 9 386H under WSL2):

seconds
python ledger.ppy 2.14
ppy run ledger.ppy, after the first run built the cache 0.30
./dist/ledger, the standalone binary 0.31

ppy run also starts CPython, names the cached build from the project's sources, and loads its native library, which the standalone binary does not pay.

Where the code comes from

ledger.ppy is hand-written.

Read on: the exceptions and generators guide.

50_errors_and_generators/ledger.ppy

"""A ledger parsed from a generator, with the bad rows caught as exceptions."""

from collections.abc import Iterator

from ppy import HashMap, Vec

ROWS = 500_000
LIMIT = 1_000_000


class BadRow(ValueError):
    """A row that is not `account,amount`."""


def ledger(count: int) -> Iterator[str]:
    """The rows as text: now and then one without a comma or an amount."""
    for i in range(count):
        if i % 97 == 13:
            yield f"acct{i % 50};oops"
        elif i % 101 == 7:
            yield f"acct{i % 50},"
        else:
            yield f"acct{i % 50},{(i * 37) % 1000 - 505}"


def amount_of(row: str) -> int:
    account, comma, amount = row.partition(",")
    if not comma:
        raise BadRow(f"no comma in {row}")
    if not amount:
        raise BadRow(f"no amount for {account}")
    return int(amount)


def settle(count: int) -> str:
    balances = HashMap[str, int]()
    bad = 0
    longest = 0
    for row in ledger(count):
        try:
            amount = amount_of(row)
        except BadRow as error:
            bad += 1
            longest = max(longest, len(str(error)))
            continue
        account, _, _ = row.partition(",")
        balances[account] = balances.get(account, 0) + amount
    richest = max(balances[name] for name in balances)
    overdrawn = sum(1 for name in balances if balances[name] < 0)
    accounts = f"{len(balances)} accounts, {bad} bad rows (longest message {longest})"
    return f"{accounts}, richest {richest}, overdrawn {overdrawn}"


def primes(limit: int) -> Iterator[int]:
    """The primes below `limit`, by trial division."""
    for n in range(2, limit):
        d = 2
        prime = True
        while d * d <= n:
            if n % d == 0:
                prime = False
                break
            d += 1
        if prime:
            yield n


def gaps(limit: int) -> str:
    found = Vec[int](primes(limit))
    widest = max(found[i + 1] - found[i] for i in range(len(found) - 1))
    twins = sum(1 for i in range(len(found) - 1) if found[i + 1] - found[i] == 2)
    try:
        beyond = found[len(found)]
    except IndexError:
        beyond = -1
    return f"{len(found)} primes, widest gap {widest}, {twins} twin pairs, past the end {beyond}"


def main() -> None:
    print(settle(ROWS))
    print(gaps(LIMIT))


main()

Source: examples/50_errors_and_generators.