Skip to content

API reference

This page provides interface signatures and behavioral contracts. See Quickstart for the complete model, Providers for source substitution, and Indexed data for organizing multiple acquisition channels.

Interface Role in the solar electricity model
DataDescriptor Identify irradiance, solar power, grid energy, and cost, and define formulas for derived quantities
DataIoC Register inputs and providers, then compute and cache results on demand
IndexedData Represent sensor readings as indexed data objects
UniqueData Share data such as the electricity tariff across acquisition channels
DataNDArray Represent hourly irradiance as an indexed NumPy array

Core

dataioc.DataIoC(allow_implicit_registering=True, record_all=False)

Resolve data dependencies on demand and cache constructed values.

Parameters:

  • allow_implicit_registering –

    Allow a requested target's own builder to be registered on first access. If false, only explicitly registered data and builders can be retrieved or built.

  • record_all –

    Retain all container access records. By default, only dependencies from the current access are retained for diagnostic output. If true, the complete access tree is retained and may add overhead.

logger property

The dependency access logger used for diagnostics.

with_data(*data)

Register existing values and return this container.

Parameters:

  • *data (Any, default: () ) –

    Values to register by their types. Values created through an indexed type, such as Sensor[1](...), retain their descriptors.

Returns:

  • DataIoC –

    This container, allowing chained registration calls.

add(data_type, data=None)

Register existing data or a target's own lazy builder.

Passing a type or descriptor without data registers the builder defined by that target. Passing an existing value as data_type registers it by type. Passing both a key and a non-None value registers that value under the key.

Parameters:

  • data_type (Union[DataT, type[DataT], DataDescriptor[DataT]]) –

    Existing value to register, or the type or descriptor used as a key.

  • data (Optional[DataT], default: None ) –

    Existing non-None value to register under data_type. Because None selects builder registration, assign through container[key] to store an explicit None value.

Returns:

  • DataIoC –

    This container, allowing chained registration calls.

Raises:

  • TypeError –

    If no builder can be found or the supplied key is invalid.

add_provider(data_type, provider)

Register an alternative lazy builder for a data key.

Registering another provider for the same key replaces the builder used by future uncached requests. Existing cached values are retained.

Parameters:

  • data_type (Union[type[DataT], DataDescriptor[DataT]]) –

    Type or descriptor whose value the provider builds.

  • provider (Union[SupportsBuild, Callable]) –

    Callable accepting a container, or an object that defines __build__.

Returns:

  • DataIoC –

    This container, allowing chained registration calls.

Raises:

  • TypeError –

    If provider is neither callable nor an object with __build__.

__getitem__(dtype)

__getitem__(dtype: DataDescriptor[DataT]) -> DataT
__getitem__(dtype: type[DataDescriptor[DataT]]) -> DataT
__getitem__(dtype: type[DataT]) -> DataT

Resolve and return the value identified by dtype.

A cached value is returned immediately. Otherwise, the registered or implicit builder is called and a successful result, including None, is cached under the requested key.

Parameters:

  • dtype (Any) –

    Data type or descriptor to resolve.

Returns:

  • Any –

    The registered or constructed value.

Raises:

  • RuntimeError –

    If implicit registration is disabled and no builder is registered.

  • TypeError –

    If no suitable builder exists.

__setitem__(data_type, data)

Store data under a type or descriptor key.

Assigning to an indexed ID 0 key also makes the value available through an unindexed class lookup. Assigning to a class creates the corresponding ID 0 mapping as well.

find_builder(dtype)

Find the registered builder for a type or descriptor.

A builder registered by class can be reused by IndexedDataTypeDescriptor instances at every ID. A builder registered for a specific DataDescriptor applies only to that descriptor.

Parameters:

  • dtype (Union[type[DataT], DataDescriptor[DataT]]) –

    Type or descriptor whose builder should be found.

Returns:

  • Callable or None –

    The matching builder, with indexed context bound when necessary, or None if no builder is registered.

dataioc.DataDescriptor(id=DefaultWeakID)

Bases: Generic[DataT]

Identify a value in DataIoC and optionally define how to build it.

Parameters:

  • id –

    Data ID. When omitted, the descriptor is a weak reference to ID 0.

Notes

A DataDescriptor is used as a dictionary key, so its subclasses must remain hashable and comparable. A subclass satisfies this requirement automatically when each of its attributes is itself hashable and comparable.

IDs distinguish multiple values of the same type. The default ID is 0 when no ID is specified. For example, readings from three sensors of the same type can be identified by Sensor, Sensor[1], and Sensor[2] and mapped to different values in DataIoC.

Examples:

An unindexed descriptor such as SensorData has a weak ID and identifies a data type. When used to retrieve data inside __build__, it is automatically rebound to the concrete ID currently being built.

>>> from dataioc import DataDescriptor, DataIoC, DataNDArray
>>>
>>> class SensorData(DataNDArray):
...     def __new__(cls, data, **kwargs):
...         return super().__new__(cls, data, **kwargs)
>>>
>>> class Sum(DataDescriptor):
...     def __build__(self, container: DataIoC):
...         return container[SensorData].sum()  # SensorData follows Sum's ID
>>>
>>> container = DataIoC().with_data(
...     SensorData([1, 1, 1]), SensorData[1]([2, 2, 2])
... )
>>> print(container[Sum[0]], container[Sum[1]])
3 6

An explicitly indexed descriptor such as SensorData[1] has a strong ID and identifies one specific data group. It is not rebound inside __build__.

>>> class OffsetSensor0(DataDescriptor):
...     def __build__(self, container: DataIoC):
...         base = container[SensorData[0]].sum()  # Explicitly read ID 0
...         return base + container[SensorData]
>>>
>>> print(container[OffsetSensor0[0]], container[OffsetSensor0[1]])
[4 4 4] [5 5 5]

id property writable

The non-negative data ID represented by this descriptor.

signed_id property

The internal signed ID, where a negative value marks a weak binding.

is_weak_id property

Whether this descriptor may inherit the current build ID.

keys property

Attribute names that participate in equality and hashing.

params property

Constructor parameters reconstructed from the descriptor state.

__build__(container)

Build the value identified by this descriptor.

Subclasses override this method to request dependencies from container and return the resulting value. The base implementation requires the value to be provided explicitly.

Parameters:

  • container (DataIoC) –

    Container used to resolve dependencies.

Returns:

  • DataT –

    The constructed value.

Raises:

  • NotImplementedError –

    If the subclass does not provide a builder.

index_implicit(new_index)

Weakly rebind to new_index, preserving an existing strong ID.

index_explicit(new_index)

Explicitly rebind to new_index, overriding any existing ID.

index(new_index, weak=True)

Bind this descriptor to another ID for related values of the same type.

For example, readings from three sensors of the same type can be identified by:

  • Sensor[0]
  • Sensor[1]
  • Sensor[2]

Parameters:

  • new_index –

    New data ID.

  • weak –

    Whether the rebinding is weak. A weak rebinding has no effect when this descriptor already has a strong ID.

Returns:

  • DataDescriptor –

    This descriptor if a weak rebinding is blocked by a strong ID; otherwise, a copy bound to new_index.

__getitem__(index)

Return a descriptor explicitly rebound to index.

Parameters:

  • index –

    New data ID.

Returns:

Notes

Internal code should prefer index_implicit so weak IDs can inherit the current build ID automatically.

dataioc.IndexedData

Marker base class for data types that can have one value per ID.

dataioc.UniqueData

Make an indexed data type share one value across all build IDs.

NumPy

dataioc.DataNDArray

Bases: ndarray, IndexedData

NumPy array base class that participates in indexed data resolution.

A single input array is viewed as the subclass without an unnecessary copy. Multiple input arrays must have the same sample count and are stacked as columns. Shape-preserving ufunc results retain the subclass when their dtype is the promoted input/output dtype. Slices and reshaped arrays are returned as plain numpy.ndarray instances.

__new__(*arrays, force_column_stack=False, **kwargs)

Construct an indexed array from one or more array-like objects.

Parameters:

  • *arrays (ArrayLike, default: () ) –

    One or more array-like objects. Multiple inputs are stacked as columns.

  • force_column_stack –

    Stack a single input as one column instead of preserving its shape.

  • **kwargs –

    Additional arguments accepted by subclass constructors.

Raises:

  • ValueError –

    If no arrays are provided or their sample counts differ.

  • TypeError –

    If an input is scalar.

reshape(*shape, **kwargs)

Return a reshaped plain numpy.ndarray.

__getitem__(key)

Return slices as plain arrays while preserving NumPy scalar behavior.

__build__(container) classmethod

Require instances of the array subclass to be provided explicitly.