Transform#

State transformation helpers.

class everwillow.statelib.transform.Transform(new_key, value_fn=<function _identity>)[source]#

Bases: Generic[V]

Describe how a single key/value pair should be rewritten.

Examples

>>> transform = Transform(new_key="scale", value_fn=lambda _k, v: 2 * v)
>>> transform.new_key
'scale'
value_fn(value)#

Callable applied to derive the transformed value.

Return type:

V

everwillow.statelib.transform.apply_transformations(state, transformations)[source]#

Rewrite selected entries in a State.

Parameters:
  • state (State[V]) – Original state whose segments will be updated.

  • transformations (Mapping[str | tuple[str | int, ...], Transform[V]]) – Mapping from existing keys to Transform objects.

Returns:

New State instance containing the transformed key/value pairs.

Raises:
  • TypeError – If state is not a State instance.

  • KeyError – If transformations reference keys not present in state.

  • ValueError – If multiple transformations target the same destination key.

Return type:

State[V]

Examples

>>> base = State.from_pytree({"a": 1, "b": 2})
>>> transform = {"a": Transform(new_key="alpha")}
>>> apply_transformations(base, transform).to_dict()
{'alpha': 1, 'b': 2}