Skip to content

serialization

Functions#

fastforward.serialization.yamlable(cls, /, tag='!ff.obj', yaml_constructor=_ff_obj_yaml_constructor, yaml_representer=_ff_obj_yaml_representer) #

Decorator that makes a class serializable to/from YAML.

This decorator wraps the init and new methods of a class to store their arguments, enabling automatic YAML serialization and deserialization. It also registers custom YAML constructor and representer functions for the class.

When a class uses this decorator, instances will have the following attributes automatically added to store constructor arguments: - newargs_ex: tuple containing (args, kwargs) passed to new - initargs_ex: tuple containing (args, kwargs) passed to init

The decorator ensures that both the current class and all its subclasses have their init and new methods wrapped when they are overridden.

Parameters:

Name Type Description Default
cls type[_T]

The class to decorate.

required
tag str

YAML tag to use for this class (default: "!ff.obj").

'!ff.obj'
yaml_constructor Callable[[Loader | FullLoader | UnsafeLoader, str, Node], Any]

Function to construct objects from YAML (default: _ff_obj_yaml_constructor).

_ff_obj_yaml_constructor
yaml_representer Callable[[Dumper, Any], MappingNode]

Function to represent objects as YAML (default: _ff_obj_yaml_representer).

_ff_obj_yaml_representer

Returns:

Type Description
type[_T]

The decorated class with YAML serialization capabilities and wrapped methods.

Example

@yamlable class MyClass: def init(self, value: int): self.value = value

Objects can now be serialized to/from YAML automatically#

obj = MyClass(42) yaml_str = yaml.dump(obj) restored_obj = yaml.load(yaml_str, Loader=yaml.Loader)