rizer.misc.yaml_utils#

Shared YAML-loading helpers.

Classes#

ScientificFloatLoader

yaml.SafeLoader that also parses 1e5-style numbers as float.

Functions#

parse_convert_yaml(→ dict[str, Any])

Parse and convert YAML file. Also perform minimal validation.

to_snake_case(→ str)

Convert CamelCase or PascalCase to snake_case.

extract_typed_block(→ dict[str, Any])

Convert a STONE-style typed block to a flat dict with type key.

Module Contents#

class rizer.misc.yaml_utils.ScientificFloatLoader(stream)#

Bases: yaml.SafeLoader

yaml.SafeLoader that also parses 1e5-style numbers as float.

PyYAML’s default float resolver requires a decimal point in the mantissa (1.0e5) and treats 1e5 as a string. This loader extends the resolver pattern so values such as 1e18 or 2e-3 are returned as Python floats.

rizer.misc.yaml_utils.parse_convert_yaml(path: str | pathlib.Path, expected_sections: list[str] | None = None) → dict[str, Any]#

Parse and convert YAML file. Also perform minimal validation.

Parameters:
  • path (str or pathlib.Path) – Path to the YAML file containing the parameters.

  • expected_sections (list of str or None, optional) – List of expected top-level sections in the YAML file. If provided, the function will check that these sections are present in the YAML file. Default is None, which means no validation of sections will be performed.

Returns:

A dictionary containing the parsed parameters from the YAML file.

Return type:

dict of str to Any

rizer.misc.yaml_utils.to_snake_case(name: str) → str#

Convert CamelCase or PascalCase to snake_case.

Examples

>>> to_snake_case("RompeWeizel")
'rompe_weizel'
>>> to_snake_case("LTEDischarge")
'lte_discharge'
>>> to_snake_case("Constant")
'constant'
rizer.misc.yaml_utils.extract_typed_block(block: dict[str, Any]) → dict[str, Any]#

Convert a STONE-style typed block to a flat dict with type key.

A typed block has exactly one key whose value is a dict of properties:

{"Trapezoidal": {"R_g": 1.0, "U_on": 10000}}
->  {"type": "trapezoidal", "R_g": 1.0, "U_on": 10000}

{"Constant": {}}
->  {"type": "constant"}