dantro._yaml module

Takes care of all YAML-related imports and configuration

The ruamel.yaml.YAML object used here is imported from paramspace and specialized such that it can load and dump dantro classes.

dantro._yaml.load_yml(path: str, *, mode: str = 'r') → Union[dict, Any][source]

Deserializes a YAML file into an object.

Uses the dantro-internal ruamel.yaml.YAML object for loading and thus supports all registered constructors.

Parameters
  • path (str) – The path to the YAML file that should be loaded. A ~ in the path will be expanded to the current user’s directory.

  • mode (str, optional) – Read mode

Returns

The result of the data loading. Typically, this will

be a dict, but depending on the structure of the file, it may also be of another type.

Return type

Union[dict, Any]

dantro._yaml.write_yml(d: Union[dict, Any], *, path: str, mode: str = 'w')[source]

Serialize an object using YAML and store it in a file.

Uses the dantro-internal ruamel.yaml.YAML object for dumping and thus supports all registered representers.

Parameters
  • d (dict) – The object to serialize and write to file

  • path (str) – The path to write the YAML output to. A ~ in the path will be expanded to the current user’s directory.

  • mode (str, optional) – Write mode of the file

dantro._yaml.yaml_dumps(obj: Any, *, register_classes: tuple = (), **dump_params) → str[source]

Serializes the given object using a newly created YAML dumper.

The aim of this function is to provide YAML dumping that is not dependent on any package configuration; all parameters can be passed here.

In other words, his function does _not_ use the dantro._yaml.yaml object for dumping but each time creates a new dumper with fixed settings. This reduces the chance of interference from elsewhere. Compared to the time needed for serialization in itself, the extra time needed to create the new ruamel.yaml.YAML object and register the classes is negligible.

Parameters
  • obj (Any) – The object to dump

  • register_classes (tuple, optional) – Additional classes to register

  • **dump_params – Dumping parameters

Returns

The output of serialization

Return type

str

Raises

ValueError – On failure to serialize the given object