envision.data_formatter module

class envision.data_formatter.EnvisionDataFormatter(formatter_params: EnvisionDataFormatterArgs)[source]

A formatter to put envision state into a reduced format.

class DataFormatterLayer(data_formatter: EnvisionDataFormatter, iterable: Iterable | None, op: Operation)[source]

A formatting layer that maps into the above layer of the current data formatter.

add(value: Any, op: Operation = Operation.NONE)[source]

Format the given object to the current layer. Specified operations are performed.

add_any(obj: Any)[source]

Format the given object to the current layer.

add_primitive(obj: Any)[source]

Add the given object as is to the given layer. Will decompose known primitives.

layer(iterable: Iterable | None = None, op: Operation = Operation.NONE)[source]

Create a new layer which maps into a section of the above layer.

reset(reset_reduction_context: bool = True)[source]

Reset the current context in preparation for new serialization.

resolve() List[source]

Resolve all layers and mappings into the final data object.

class envision.data_formatter.EnvisionDataFormatterArgs(id: str | None, serializer: ~typing.Callable[[list], ~typing.Any] = <function _passthrough_method_1_arg>, float_decimals: int = 2, bool_as_int: bool = True, enable_reduction: bool = True)[source]

Data formatter configurations.

bool_as_int: bool

Alias for field number 3

enable_reduction: bool

Alias for field number 4

float_decimals: int

Alias for field number 2

id: str | None

Alias for field number 0

serializer: Callable[[list], Any]

Alias for field number 1

class envision.data_formatter.Operation(value)[source]

Formatting operations that should be performed on an object or layer.

FLATTEN = 4

Convert value from a sequential container or dataclass to a flat hierarchy.

e.g. [(1, 2), (13,)] -> [1, 2, 13]

NONE = 0

No special operation. Value will be sent as is.

REDUCE = 1

Send value only if it has changed.

class envision.data_formatter.ReductionContext(mapping: Dict[Hashable, int] | None = None, removed: List[int] | None = None, enabled: bool = True)[source]

Mappings between an object and its reduction to an ID.

property enabled

If this reduction context is enabled(else it is pass-through.)

property has_ids

If this reducing tool has currently reduced objects.

reset()[source]

Returns this context back to blank.

resolve_mapping()[source]

The mappings that the context contains

resolve_value(value: Hashable) int | Hashable[source]

Map the value to an ID.