hydra_zen.to_yaml#

hydra_zen.to_yaml(cfg, *, resolve=False, sort_keys=False)[source]#

Serialize a config as a yaml-formatted string.

This is an alias of omegaconf.Omegaconf.to_yaml.

Parameters:
cfgAny

A valid configuration object, supported either by Hydra or hydra-zen

resolvebool, optional (default=False)

If True, interpolated fields in cfg will be resolved in the yaml.

sort_keysbool, optional (default=False)

If True, the yaml’s entries will alphabetically ordered.

Returns:
yamlstr

See also

save_as_yaml

Save a config to a yaml-format file.

load_from_yaml

Load a config from a yaml-format file.

Examples

>>> from hydra_zen import builds, make_config, to_yaml

Basic usage

The yaml of a config with both an un-configured field and a configured field:

>>> c1 = make_config("a", b=1)
>>> print(to_yaml(c1))
a: ???
b: 1

The yaml of a targeted config:

>>> c2 = builds(dict, y=10)
>>> print(to_yaml(c2))
_target_: builtins.dict
'y': 10

hydra-zen’s additional supported types can be specified as well

>>> print(to_yaml(1+2j))
real: 1.0
imag: 2.0
_target_: builtins.complex

Specifying resolve

The following is a config with interpolated fields.

>>> c3 = make_config(a=builds(dict, b="${c}"), c=1)
>>> print(to_yaml(c3, resolve=False))
a:
  _target_: builtins.dict
  b: ${c}
c: 1
>>> print(to_yaml(c3, resolve=True))
a:
  _target_: builtins.dict
  b: 1
c: 1

Specifying sort_keys

>>> c4 = make_config("b", "a")  # field order: b then a
>>> print(to_yaml(c4, sort_keys=False))
b: ???
a: ???
>>> print(to_yaml(c4, sort_keys=True))
a: ???
b: ???