Tools¶
Tool
¶
Bases: Protocol
One modelling framework's view of a record.
Implementations are stateless (a module-level singleton is fine): every method takes the record or model it operates on. A structural type, for annotating code that takes any tool - not a dispatch table; tools are reached by importing them.
A Record rather than a record throughout, so a tool builds from a
directory as readily as from an overlay and has no reason to know layering
exists.
Notes
requires
¶
requires(record: RecordLike) -> Requirements
What this tool needs from record to build a model.
Record-dependent, not a constant: which attributes are required follows the record's own component types and its declared schema.
Source code in src/datarecord/tools/base.py
verify
¶
verify(record: RecordLike) -> Requirements
build
¶
build(record: RecordLike) -> Any
to_datarecord
¶
to_datarecord(model: Any) -> RecordLike
model presented as a layer write_record can persist.
The inverse of build. Framework-specific: undoing a framework's own
shape is exactly what a tool knows and the record layer does not.
Notes
Source code in src/datarecord/tools/base.py
results
¶
results(model: Any) -> Frames
This model's result attributes in the record's long form.
Keyed by attribute, each frame in the long schema - name, the dim
columns, value - the same shape Record.outputs presents, so results
go straight to write_record or to set(..., kind="outputs"). One frame
spans every component type, needing no type column.
Narwhals frames, so the seam names no one dataframe library, and lazy so an implementation may fetch a result attribute on demand rather than materialising every one.
Source code in src/datarecord/tools/base.py
Requirements
dataclass
¶
Requirements(
dims: frozenset[str] = frozenset(),
entity_types: frozenset[str] = frozenset(),
attributes: frozenset[tuple[str, str]] = frozenset(),
unsupported_keys: frozenset[
tuple[str, str]
] = frozenset(),
unsupported_values: frozenset[
tuple[str, str]
] = frozenset(),
names: frozenset[str] = frozenset(),
)
What a tool needs from a record, and what a given record fails to supply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dims
|
frozenset of str
|
Dims the tool cannot build a model without. |
frozenset()
|
entity_types
|
frozenset of str
|
Component types the tool requires the record to define members for. |
frozenset()
|
attributes
|
frozenset of tuple of (str, str)
|
|
frozenset()
|
unsupported_keys
|
frozenset of tuple of (str, str)
|
|
frozenset()
|
unsupported_values
|
frozenset of tuple of (str, str)
|
|
frozenset()
|
names
|
frozenset of str
|
Names two of the framework's components claim, which a record scopes across every type. |
frozenset()
|
describe
¶
A one-line summary, for an error message or a log line.
Source code in src/datarecord/tools/base.py
Schema
dataclass
¶
Schema(attrs: dict[str, tuple[Attr, ...]] = dict())
A tool's attribute mapping against the record's vocabulary.
Only differing attributes need an entry; anything unlisted is read under the
same name, so an empty Schema is the identity. Attr.source names
attributes in the record's vocabulary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
dict of str to tuple of Attr
|
Per component type, the attributes that are renamed or computed. |
dict()
|
attr
¶
attr(ctype: str, name: str) -> Attr
ctype's mapping for name, or the identity when it has none.
sources
¶
Which record attribute(s) ctype's name needs; itself, if unmapped.
What verify checks resolvability against: a renamed or computed
attribute is satisfied by its sources, not by its own name.
Source code in src/datarecord/tools/base.py
resolve
¶
resolve(
record: RecordLike, ctype: str, name: str
) -> DuckDBPyRelation
ctype's name as a long relation over record, mapping applied.
Attr
dataclass
¶
One tool attribute and the record attribute(s) it is built from.
The vocabulary seam: a rename, or a value computed from several,
declared rather than open-coded in a build.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool's name for the attribute. |
required |
source
|
tuple of str
|
Record attribute name(s) it is read from. A rename has one. |
required |
compute
|
callable
|
Maps one resolved long relation per |
None
|
Notes
resolve
¶
resolve(record: RecordLike) -> DuckDBPyRelation
This attribute's long relation, read through the Record interface.
The record rather than the record, so a tool builds from any backing.
Notes
Source code in src/datarecord/tools/base.py
UnsupportedRecordError
¶
UnsupportedRecordError(tool: str, missing: Requirements)
Bases: ValueError
A record does not define everything the tool needs (Tool.verify).
Source code in src/datarecord/tools/base.py
PyPSA¶
The PyPSA tool needs the optional extra: pip install datarecord[pypsa].