Moved derive unit API to DLUnit

Derived units now use a DLUnit class similar to a DLPak to be easily
stored and loaded, the public API has been updated to either take a
DLUnit object or a Path where it reads the DLUnit from a *.dlunit file

The .dlunit file is a ZIP compressed archive containing two files, a
manifest.json which stores the units uuid, its input signals and its
output signal, and unit.py which is the python code for the unit, the
same rules apply (one python function with a specifically structured
signature) as when a string was passed in the old API

Errors have been added to have a DLUnitError similar to the DLPakError
as the DLUnit class is heavily inspired from the DLPak architecutre

Logging may still be primitive, this will be covered in the next AI
logging sweep to homogenize and clean up the logging aspect of things
This commit is contained in:
h3cx committed 2026-09-15 17:13:27 +02:00
1 parent 9407bfeb79
commit 207a91811e
10 files changed
+378 -44

No files matched your search

+39 -12
View File
@@ -310,14 +310,23 @@ while it is paused, has not started, or has reached the end of the recording.
## Derived Signals
A derive unit calculates a new value whenever one of its input signals receives
an update and all inputs have non-stale live values. Define the calculation as a
Python source string and bind it with `bind_derive_unit()`.
an update and all inputs have non-stale live values. Package the calculation,
its input descriptors, and its output descriptor in a `DLUnit`, then bind it
with `bind_derive_unit()`.
### `bind_derive_unit(function, input_signals, output_signal) -> UUID`
### `bind_derive_unit(dlunit: DLUnit | None = None, file: Path | None = None) -> UUID`
Provide exactly one of `dlunit` or `file`:
- `dlunit` binds an in-memory `DLUnit`.
- `file` loads and binds a `.dlunit` archive.
To create and bind an in-memory unit:
```python
from uuid import uuid4
from dynalab_core.dlunit import DLUnit
from dynalab_core.protocols.packets.handshake import SignalDescriptor
processing_function = """
@@ -332,25 +341,42 @@ def add(left: ValueDescriptor, right: ValueDescriptor, output: SignalDescriptor)
)
"""
output_signal = SignalDescriptor(
return_signal = SignalDescriptor(
id=uuid4(),
name="Total",
type="number",
origin="derived",
)
derive_unit = DLUnit()
derive_unit.set_manifest(
uuid4(),
[left_signal, right_signal],
return_signal,
)
derive_unit.set_data(processing_function)
unit_id = core.bind_derive_unit(dlunit=derive_unit, file=None)
```
To bind a previously saved archive instead, pass `None` for `dlunit` and its
path for `file`:
```python
from pathlib import Path
unit_id = core.bind_derive_unit(
processing_function,
input_signals=[left_signal, right_signal],
output_signal=output_signal,
dlunit=None,
file=Path("units/total.dlunit"),
)
```
`function` must define exactly one Python function. Its parameters must be:
The unit's Python source must define exactly one function. Its parameters must
be:
1. Zero or more `ValueDescriptor` parameters, in the same order as
`input_signals`.
2. One final `SignalDescriptor` parameter for `output_signal`.
the manifest's `input_signals`.
2. One final `SignalDescriptor` parameter for the manifest's `return_signal`.
3. A return annotation of `ValueDescriptor`.
The parameter annotations must use the imported `ValueDescriptor` and
@@ -378,8 +404,9 @@ individual failure reason:
| Exception | Area | Codes |
| --- | --- | --- |
| `CoreError` | Core lifecycle, mode, and recording operations. | `invalid_state`, `invalid_mode`, `incorrect_mode`, `recording_active`, `not_recording`, `no_recording`, `no_playback_data` |
| `DLPakError` | Archive preparation, reading, and writing. | `no_data`, `no_manifest`, `read_failed`, `write_failed` |
| `CoreError` | Core lifecycle, mode, recording, and derive-unit binding operations. | `invalid_state`, `invalid_mode`, `incorrect_mode`, `recording_active`, `not_recording`, `no_recording`, `no_playback_data`, `bind_derive_failed` |
| `DLPakError` | Recording archive preparation, reading, and writing. | `no_data`, `no_manifest`, `read_failed`, `write_failed` |
| `DLUnitError` | Derive-unit archive preparation, reading, and writing. | `no_data`, `no_manifest`, `read_failed`, `write_failed` |
| `DeriveError` | Derive source, signatures, arguments, and registration. | `invalid_source`, `invalid_signature`, `input_signal_mismatch`, `argument_mismatch`, `already_registered` |
| `PlaybackError` | Playback state and data. | `no_data`, `incomplete_data`, `already_playing`, `invalid_timestamp`, `stopped` |
| `ConnectorError` | Connector queues and registration. | `queue_full`, `already_registered` |