Utilities

This module contains utility functions and classes for the vivarium.config_tree package.

vivarium.config_tree.utilities.load_yaml(data)[source]

Load a YAML filepath or string into a dictionary.

Return type:

dict[str, Any]

Parameters:

data (str | Path) – The YAML content to load. This can be a file path to a YAML file or a string containing YAML-formatted text.

Returns:

A dictionary representation of the loaded YAML content.

Raises:

ValueError – If the loaded YAML content is not a dictionary.

Notes

If data is a Path object or a string that ends with “.yaml” or “.yml”, it is treated as a filepath and this function loads the file. Otherwise, data is a string that does _not_ end in “.yaml” or “.yml” and it is treated as YAML-formatted text which is loaded directly into a dictionary.

class vivarium.config_tree.utilities.SafeLoader(stream)[source]

A yaml.SafeLoader that restricts duplicate keys.

construct_mapping(node, deep=False)[source]

Construct the standard mapping after checking for duplicates.

Return type:

dict[Hashable, Any]

Raises:

DuplicatedConfigurationError – If duplicate keys within the same level are detected in the YAML file being loaded.

Parameters:
  • node (MappingNode)

  • deep (bool)

Notes

A key is considered a duplicate only if it is the same as another key at the same level in the YAML.

This raises upon the first duplicate key found; other duplicates may exist (in which case a new error will be raised upon re-loading of the YAML file once the duplicate is resolved).