Skip to content

betting_combat.feeds.base

base.py — what every feed shares: its tables, made sure once, and the two verbs between our contracts and the database (through common-db: a COPY to staging, a merge, counts back).

Feed the base: ``ensure`` (tables + touch triggers, idempotent), ``write`` (facts →
rows, upserted on the key, or appended for a ledger; in batches), ``read``
(rows → facts, scoped), ``held`` (a column's values already stored: a build's
resume)
stream a fetch over many items, landed as the answers come (see its doc)
WriteError a write the database refused (the result's error, the table)

A feed is one objective: it combines providers into our tables and carries the three ways its data moves: build (a whole date range: the history), update (the recent past: after a card) and, where the data has one, live (in-fight). Every one is rerunnable: the keys are deterministic, an unchanged row is never touched, so a second run writes nothing new. A build lands as it goes and resumes: an item that failed is reported, not fatal, and a rerun skips what is stored and final (only the failed and the unfinished are asked again).

Classes

Failure

Feed

One objective’s tables and their reads and writes.

ensure

ensure() -> None

Every table of the feed (and its updated_at trigger): safe on every start. A table already whole is left alone: its DDL (ADD COLUMN IF NOT EXISTS included) takes an exclusive lock that stalls every writer of the table and of the ones keyed to it (found 2026-11-10: a card refresh’s ensure on markets held the trades writes 27 min behind a long read).

held

held(table: type[Stored], column: str, latest: str | None = None, count: str | None = None, where: Mapping[str, Any] | None = None) -> dict[Any, Any]

What a build already stored: column’s distinct values in table (where: equal columns), each with its newest latest or how many distinct count it has (None with neither).

read

read(table: type[Stored], where: Mapping[str, Any] | None = None) -> list[Any]

table’s rows (where: common-db’s predicates) as its contract’s facts.

write

write(table: type[Stored], facts: Iterable[BaseModel]) -> dict[str, int]

Land facts in table: upserted on its key (or appended), BATCH rows a write. The counts summed; a refused write raises WriteError.

WriteError

Bases: RuntimeError

Functions

fan_out

fan_out(items: Sequence[T], fn: Callable[[T], Awaitable[R]], limit: int = 8) -> list[R]

fn over every item, limit at a time (the provider’s pacer still caps the rate), results in the items’ order: a slow vendor’s answers overlap instead of queueing.

stream

stream(items: Sequence[T], fn: Callable[[T], Awaitable[R]], land: Callable[[list[R]], Awaitable[Any]], limit: int = 8, every: int = 100) -> list[Failure]

fn over every item, limit at a time and none waiting on another; every every answers are landed as they come (land, one at a time), the rest at the end. An item whose fn raises is a Failure (the others go on). A refused land stops the run: the items still out are cancelled, nothing more lands, its error is raised (what landed before it stays). Nothing outlives the call.