qudi.util.yaml

This file extends the ruamel.yaml package functionality to load and dump more data types needed by qudi (mostly numpy array and number types). Provides easy to use yaml_load and yaml_dump functions to read and write qudi YAML files.

Functions

yaml_load(file_path[, ignore_missing])

Loads a qudi style YAML file.

yaml_dump(file_path, data)

Saves data to file_path in qudi style YAML format.

Classes

SafeRepresenter(*args, **kwargs)

Custom YAML representer for qudi config files

SafeConstructor([preserve_quotes, loader])

Custom YAML constructor for qudi config files

YAML(**kwargs)

ruamel.yaml.YAML subclass to be used by qudi for all loading/dumping purposes.

Exceptions

ParserError([context, context_mark, ...])

YAMLError

MarkedYAMLError([context, context_mark, ...])

YAMLStreamError

ScannerError([context, context_mark, ...])

ConstructorError([context, context_mark, ...])

DuplicateKeyError([context, context_mark, ...])

exception qudi.util.yaml.ConstructorError(context: Any = None, context_mark: Any = None, problem: Any = None, problem_mark: Any = None, note: Any = None, warn: Any = None)[source]

Bases: ruamel.yaml.error.MarkedYAMLError

exception qudi.util.yaml.DuplicateKeyError(context: Any = None, context_mark: Any = None, problem: Any = None, problem_mark: Any = None, note: Any = None, warn: Any = None)[source]

Bases: ruamel.yaml.error.MarkedYAMLError

exception qudi.util.yaml.MarkedYAMLError(context: Any = None, context_mark: Any = None, problem: Any = None, problem_mark: Any = None, note: Any = None, warn: Any = None)[source]

Bases: ruamel.yaml.error.YAMLError

check_append(lines: list[str], val: str | None) None[source]
exception qudi.util.yaml.ParserError(context: Any = None, context_mark: Any = None, problem: Any = None, problem_mark: Any = None, note: Any = None, warn: Any = None)[source]

Bases: ruamel.yaml.error.MarkedYAMLError

class qudi.util.yaml.SafeConstructor(preserve_quotes: bool | None = None, loader: Any = None)[source]

Bases: ruamel.yaml.constructor.SafeConstructor

Custom YAML constructor for qudi config files

construct_complex(node)[source]

The complex constructor.

construct_enum(node)[source]

The Enum constructor.

construct_extndarray(node)[source]

The constructor for a numpy array that is saved in a separate file.

construct_flag(node)[source]

The Flag constructor.

construct_frozenset(node)[source]

The frozenset constructor.

construct_ndarray(node)[source]

The constructor for a numpy array that is saved as binary string with ASCII-encoding

yaml_constructors: Dict[Any, Any] = {'tag:yaml.org,2002:binary': <function SafeConstructor.construct_yaml_binary>, 'tag:yaml.org,2002:bool': <function SafeConstructor.construct_yaml_bool>, 'tag:yaml.org,2002:complex': <function SafeConstructor.construct_complex>, 'tag:yaml.org,2002:enum': <function SafeConstructor.construct_enum>, 'tag:yaml.org,2002:extndarray': <function SafeConstructor.construct_extndarray>, 'tag:yaml.org,2002:flag': <function SafeConstructor.construct_flag>, 'tag:yaml.org,2002:float': <function SafeConstructor.construct_yaml_float>, 'tag:yaml.org,2002:frozenset': <function SafeConstructor.construct_frozenset>, 'tag:yaml.org,2002:int': <function SafeConstructor.construct_yaml_int>, 'tag:yaml.org,2002:map': <function SafeConstructor.construct_yaml_map>, 'tag:yaml.org,2002:ndarray': <function SafeConstructor.construct_ndarray>, 'tag:yaml.org,2002:null': <function SafeConstructor.construct_yaml_null>, 'tag:yaml.org,2002:omap': <function SafeConstructor.construct_yaml_omap>, 'tag:yaml.org,2002:pairs': <function SafeConstructor.construct_yaml_pairs>, 'tag:yaml.org,2002:seq': <function SafeConstructor.construct_yaml_seq>, 'tag:yaml.org,2002:set': <function SafeConstructor.construct_yaml_set>, 'tag:yaml.org,2002:str': <function SafeConstructor.construct_yaml_str>, 'tag:yaml.org,2002:timestamp': <function SafeConstructor.construct_yaml_timestamp>, None: <function SafeConstructor.construct_undefined>}
class qudi.util.yaml.SafeRepresenter(*args, **kwargs)[source]

Bases: ruamel.yaml.representer.SafeRepresenter

Custom YAML representer for qudi config files

ignore_aliases(ignore_data)[source]

Ignore aliases and anchors. Overwrites base class implementation.

ndarray_max_size = 20
represent_complex(data)[source]

Representer for builtin complex type

represent_dict_no_sort(data)[source]

Representer for dict and OrderedDict to prevent ruamel.yaml from sorting keys

represent_enum(data)[source]

Representer for enum types with base class enum.

represent_flag(data)[source]

Representer for enum types with base class enum.

represent_frozenset(data)[source]

Representer for builtin frozenset type

represent_ndarray(data)[source]

Representer for numpy.ndarrays. Will represent the array in binary representation as ASCII-encoded string by default. If the output stream to dump to is a “regular” open text file handle (io.TextIOWrapper) and the array size exceeds the specified maximum ndarray size, it is dumped into a separate binary .npy file and is represented in YAML as file path string.

represent_numpy_complex(data)[source]

Representer for numpy complex scalars

represent_numpy_float(data)[source]

Representer for numpy float scalars

represent_numpy_int(data)[source]

Representer for numpy int scalars

yaml_multi_representers: Dict[Any, Any] = {<class 'numpy.complexfloating'>: <function SafeRepresenter.represent_numpy_complex>, <class 'numpy.floating'>: <function SafeRepresenter.represent_numpy_float>, <class 'numpy.integer'>: <function SafeRepresenter.represent_numpy_int>, <enum 'Enum'>: <function SafeRepresenter.represent_enum>, <enum 'Flag'>: <function SafeRepresenter.represent_flag>, <enum 'IntEnum'>: <function SafeRepresenter.represent_enum>, <enum 'IntFlag'>: <function SafeRepresenter.represent_flag>}
yaml_representers: Dict[Any, Any] = {<class 'NoneType'>: <function SafeRepresenter.represent_none>, <class 'bool'>: <function SafeRepresenter.represent_bool>, <class 'bytes'>: <function SafeRepresenter.represent_binary>, <class 'collections.OrderedDict'>: <function SafeRepresenter.represent_dict_no_sort>, <class 'complex'>: <function SafeRepresenter.represent_complex>, <class 'datetime.date'>: <function SafeRepresenter.represent_date>, <class 'datetime.datetime'>: <function SafeRepresenter.represent_datetime>, <class 'dict'>: <function SafeRepresenter.represent_dict_no_sort>, <class 'float'>: <function SafeRepresenter.represent_float>, <class 'frozenset'>: <function SafeRepresenter.represent_frozenset>, <class 'int'>: <function SafeRepresenter.represent_int>, <class 'list'>: <function SafeRepresenter.represent_list>, <class 'numpy.ndarray'>: <function SafeRepresenter.represent_ndarray>, <class 'ruamel.yaml.compat.ordereddict'>: <function SafeRepresenter.represent_ordereddict>, <class 'set'>: <function SafeRepresenter.represent_set>, <class 'str'>: <function SafeRepresenter.represent_str>, <class 'tuple'>: <function SafeRepresenter.represent_list>, None: <function SafeRepresenter.represent_undefined>}
exception qudi.util.yaml.ScannerError(context: Any = None, context_mark: Any = None, problem: Any = None, problem_mark: Any = None, note: Any = None, warn: Any = None)[source]

Bases: ruamel.yaml.error.MarkedYAMLError

class qudi.util.yaml.YAML(**kwargs)[source]

Bases: ruamel.yaml.main.YAML

ruamel.yaml.YAML subclass to be used by qudi for all loading/dumping purposes. Will always use the ‘safe’ option without round-trip functionality.

__init__(**kwargs)[source]

@param kwargs: Keyword arguments accepted by ruamel.yaml.YAML(), excluding “typ”

exception qudi.util.yaml.YAMLError[source]

Bases: builtins.Exception

exception qudi.util.yaml.YAMLStreamError[source]

Bases: builtins.Exception

qudi.util.yaml.yaml_dump(file_path: str | bytes | PathLike, data: Mapping[str, Any]) None[source]

Saves data to file_path in qudi style YAML format. Creates subdirectories if needed.

@param str file_path: path to YAML file to save data into @param dict data: Dict containing the data to save to file

qudi.util.yaml.yaml_load(file_path: str | bytes | PathLike, ignore_missing: bool | None = False) Dict[str, Any][source]

Loads a qudi style YAML file. Raises OSError if the file does not exist or can not be accessed.

@param str file_path: path to config file @param bool ignore_missing: optional, flag to suppress FileNotFoundError

@return dict: The data as python/numpy objects in a dict