yamlpars package

class yamlpars.Parameters(yaml_folder: Union[str, Path], auto_load: bool = True)[source]

The Parameters object loads the yaml files from the folder specified in the constructor, and allows easy dot-notation access to the parameters, as well as an useful serialization methods.

yaml_folder

the folder containing the yaml files.

Type

str | pathlib.Path

auto_load

if True, the yaml files are loaded automatically when the object is created. The default is True.

Type

bool

Example

>>> pars = Parameters("path/to/yaml/folder")
>>> pars.group_a.a_int
1
>>> pars.group_a.a_float
1.0
load() None[source]

Load parameters into the object by parsing the yaml files.

Raises

FileNotFoundError – the folder containing the yaml files does not exist.

replace_entry(key: str, value: Any, type_check: bool = True) None[source]

Replace the value of a parameter.

Parameters
  • key (str) – the key of the parameter to replace.

  • value (Any) – the new value of the parameter.

  • type_check (bool) – if True, the type of the value is checked against the type of the parameter, and an error is raised if the types do not match. The default is True.

Example

>>> pars.replace_entry("group_a.a_dictionary.first_key", 'some_new_value')
Raises
  • KeyError – the key does not exist in the parameters.

  • TypeError – the type of the value does not match the type of the parameter.

serialize() Dict[str, Any][source]

Return the parameters as a dictionary of depth one.

Example

>>> pars.serialize()
{ 'group_a.a_int': 1,
  'group_a.a_float': 1.0,
  'group_a.a_bool': True,
  'group_a.a_dictionary.first_key': 'first',
  'group_a.a_dictionary.second_key': 'second',
...
yamlpars.update_yaml_file(yaml_folder: Union[str, Path], file_name: str, update_dict: Dict[str, Any], record_file_name: Optional[str] = None, check_type: bool = True, lint_yaml_file_after_update: bool = True) None[source]

Update the entries in a yaml file according to the update_dict.

Parameters
  • yaml_folder (str | pathlib.Path) – the folder containing the yaml files.

  • file_name (str) – the name of the file to update, without the extension.

  • update_dict (Dict[str, Any]) – the dictionary containing the updated values.

  • record_file_name (str, optional) – the name of the file used to keep track of the updates. If None, the record file is not updated.

  • check_type (bool) – if True, the type of the updated values is checked against the type of the original values. If the type is different, a TypeError is raised. The default is True.

  • lint_yaml_file_after_update (bool) – if True, the yaml file is linted after the update. The default is True.

Raises
  • FileNotFoundError – the file to update does not exist.

  • KeyError – a key in the update_dict does not exist in the original yaml file.

  • TypeError – the type of the updated values is different from the original.

Example

>>> update_yaml_file("path/to/yaml/folder", "group_a", {"a_int": 2, "a_float": 2.0})