Skip to content

Building blocks

Extension API. Use these types to define component data, processors, and processor resources.

Component

Base class for typed entity data.

Define a component by subclassing Component and declaring Pydantic fields. Keep components small and focused on one concern.

Examples:

>>> class Position(Component):
...     x: float = 0.0
...     y: float = 0.0

get_type_by_name classmethod

get_type_by_name(name)

Find a Component subclass (at any depth) by its class name.

Raises:

Type Description
ValueError

If no matching subclass exists or more than one subclass has the requested name.

from_dict classmethod

from_dict(data)

Create a component instance from a dictionary.

Call this method on a concrete subclass, or include a "type" key naming a registered subclass when calling Component.from_dict().

Raises:

Type Description
ValueError

If the concrete component type cannot be determined.

to_payload

to_payload()

Serialize this component for command and API payloads.

Unlike model_dump(), the result includes the concrete component type so Component.from_dict() can reconstruct it.

get_prefix classmethod

get_prefix()

Return the column prefix for this component type.

to_pyarrow_schema classmethod

to_pyarrow_schema()

Return the unprefixed PyArrow schema for this component type.

get_prefixed_schema classmethod

get_prefixed_schema()

Return a PyArrow schema whose fields use the component prefix.

to_row_dict

to_row_dict()

Return component values keyed by their prefixed column names.

AsyncProcessor

Base class for asynchronous dataframe processors.

Subclasses declare the component types they require and implement process(). Lower priority values run first. Processors return a new Daft DataFrame rather than mutating their input.

Examples:

>>> from daft import col
>>> class Health(Component):
...     value: int = 100
>>> class Decay(AsyncProcessor):
...     components = (Health,)
...     priority = 10
...
...     async def process(self, df, **kwargs):
...         return df.with_column(
...             "health__value", col("health__value") - 1
...         )

process async

process(df, **input_kwargs)

Transform one archetype dataframe for a simulation tick.

Override this method in subclasses and return a new dataframe.

Resources

Resources()

Type-safe container for world-level shared state.

Usage

world.resources.insert(SimConfig(gravity=9.8)) world.resources.insert(broker)

In processor

config = resources.require(SimConfig) broker = resources.get(CommandBroker) # Returns None if not present

insert

insert(resource)

Insert a resource, keyed by its type.

insert_as

insert_as(resource, key_type)

Insert a resource keyed by an explicit type rather than type(resource).

Useful in tests when a subclass should be looked up as its base type::

world.resources.insert_as(FakeSpec(client), PolicyClientSpec)

Production processors that call resources.require(PolicyClientSpec) will then find FakeSpec(client) transparently.

get

get(resource_type)

Get a resource, or None if not present.

require

require(resource_type)

Get a resource, raising KeyError if not present.

remove

remove(resource_type)

Remove and return a resource, or None if not present.

contains

contains(resource_type)

Check if a resource type is registered.

items

items()

Iterate over (type, instance) pairs.