YAML utilities

Read and write YAML files with numpy and Path support.

This module is a convenience for plugins, not core code – nothing in ember itself depends on it. A plugin that wants to load or save its own configuration as YAML can use read_yaml() and write_yaml() in place of yaml.safe_load and yaml.safe_dump.

The following fixes are applied:

  • Dump numpy arrays and scalars as plain YAML lists and scalars

  • Path as an expanded string

  • Parse scientific notation without decimal points e.g. 1e-5

  • Use PyYAML’s CSafeLoader/CSafeDumper for speed where available, falling back to the pure-Python versions otherwise

  • Always read and write files as UTF-8 encoded

yaml_util.read_yaml(fname)[source]

Read a dictionary from a YAML file.

Parameters:

fname (str or Path) – Path to the YAML file to read.

Returns:

Parsed contents of the file.

Return type:

dict

yaml_util.write_yaml(d, fname, mode='w')[source]

Write a dictionary to a YAML file.

Parameters:
  • d (dict) – Dictionary to write.

  • fname (str or Path) – Path to the YAML file to write.

  • mode (str, optional) – Mode to open fname with. Defaults to "w"; pass "a" to append a further document to an existing file.