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:
- Returns:
New
Stateinstance containing the transformed key/value pairs.- Raises:
TypeError – If
stateis not aStateinstance.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}