bridge.utils.yaml_utils#

Module Contents#

Functions#

safe_yaml_representers

Context manager for safely adding and removing custom YAML representers.

dump_dataclass_to_yaml

Dump a dataclass object or other Python object to a YAML file or string.

_function_representer

Represent functions in YAML.

_torch_dtype_representer

Represent torch dtypes in YAML.

_safe_object_representer

General object representer for YAML.

_partial_representer

Represent functools.partial objects in YAML.

_enum_representer

Represent enums in YAML.

_generation_config_representer

Represent transformers GenerationConfig objects in YAML.

API#

bridge.utils.yaml_utils.safe_yaml_representers() Generator[None, None, None]#

Context manager for safely adding and removing custom YAML representers.

Temporarily adds custom representers for functions, classes, and other objects to the YAML SafeDumper, and restores the original representers when exiting the context.

Usage: with safe_yaml_representers(): yaml_str = yaml.safe_dump(my_complex_object)

bridge.utils.yaml_utils.dump_dataclass_to_yaml(
obj: Any,
filename: Optional[str] = None,
) Optional[str]#

Dump a dataclass object or other Python object to a YAML file or string.

Uses safe representers to handle common types.

Parameters:
  • obj – The object to dump.

  • filename – If provided, the path to the file where YAML should be written. If None, returns the YAML string directly.

Returns:

If filename is None, returns the YAML string representation of the object. Otherwise, returns None.

bridge.utils.yaml_utils._function_representer(dumper, data)#

Represent functions in YAML.

bridge.utils.yaml_utils._torch_dtype_representer(dumper, data)#

Represent torch dtypes in YAML.

bridge.utils.yaml_utils._safe_object_representer(dumper, data)#

General object representer for YAML.

This function is a fallback for objects that don’t have specific representers. If the object has qualname attr, the target is set to f”{inspect.getmodule(obj).name}.{obj.qualname}”. If the object does not have a qualname attr, the target is set from its class attr. The call key is used to indicate whether the target should be called to create an instance.

Parameters:
  • dumper (yaml.Dumper) – The YAML dumper to use for serialization.

  • data (Any) – The data to serialize.

Returns:

The YAML representation of the data.

bridge.utils.yaml_utils._partial_representer(dumper, data)#

Represent functools.partial objects in YAML.

bridge.utils.yaml_utils._enum_representer(dumper, data)#

Represent enums in YAML.

bridge.utils.yaml_utils._generation_config_representer(dumper, data)#

Represent transformers GenerationConfig objects in YAML.