madgui.util.yaml module

Utilities for loading and writing YAML documents. We currently use YAML for config files, session data and various data exports.

The functions here are merely thin wrappers on top of the PyYAML API. They mainly add these features:

  • the load functions are order preserving, i.e. they return dicts that when iterated yield elements in the same order as written in the YAML document

  • the save family of functions handle serialization of OrderedDict and several numpy number types that would otherwise raise an error.

  • load_file() and save_file() allow to directly provide a filename rather than having to pass a stream.

  • the save_file() function ensures that an existing file at the same location will only be overwritten if the data can be serialized without error.

  • save_file() creates directories as needed

Functions

load_file(filename)

Load yaml document from filename.

safe_load(stream[, Loader])

Load YAML document from stream, returns dictionaries in the written order within the YAML document.

safe_dump(data[, stream, Dumper])

Saves YAML document to stream (or returns as string).

Exceptions

YAMLError

ParserError([context, context_mark, …])

ScannerError([context, context_mark, …])

exception madgui.util.yaml.ParserError(context=None, context_mark=None, problem=None, problem_mark=None, note=None)[source]

Bases: yaml.error.MarkedYAMLError

exception madgui.util.yaml.ScannerError(context=None, context_mark=None, problem=None, problem_mark=None, note=None)[source]

Bases: yaml.error.MarkedYAMLError

exception madgui.util.yaml.YAMLError[source]

Bases: Exception

madgui.util.yaml.load_file(filename)[source]

Load yaml document from filename.

madgui.util.yaml.safe_dump(data, stream=None, Dumper=<class 'yaml.cyaml.CSafeDumper'>, **kwds)[source]

Saves YAML document to stream (or returns as string). This function takes care to correctly serialize OrderedDict, as well as several numpy number types, which would otherwise lead to errors.

Note that it is easy to accidentally have some of these types in your data if not taking extreme care. For example, if you retrieve an array element fron numpy using array[i], you will not get a python float or int, but a numpy specific datatype.

madgui.util.yaml.safe_load(stream, Loader=<class 'yaml.cyaml.CSafeLoader'>)[source]

Load YAML document from stream, returns dictionaries in the written order within the YAML document.