hydromt_wflow.utils.get_config#

get_config(config: dict, key: str, root: Path | None = None, fallback: Any | None = None, abs_path: bool = False)[source]#

Retrieve a nested configuration value from a dictionary.

Keys can be specified using dot-separated paths (e.g. "a.b.c") to access nested dictionaries. Optionally, the function can resolve relative paths against a given model root.

Parameters:
  • config (dict) – The configuration dictionary to read from.

  • key (str) – Dotted key path indicating the value to retrieve, e.g. "a.b.c".

  • root (Path, optional) – Path to the model root. Used when abs_path=True to resolve relative paths. Default is None.

  • fallback (Any, optional) – Value to return if the key path does not exist. Default is None.

  • abs_path (bool, optional) – If True and the retrieved value is a relative path, return its absolute version relative to root. Default is False.

Returns:

The value corresponding to the given key path. Returns fallback if the key does not exist.

Return type:

Any

Examples

>>> from pathlib import Path
>>> from hydromt_wflow.utils import get_config
>>> cfg = {
...     "model": {
...         "params": {"scale": 2.5},
...         "paths": {"data": "input/data.csv"},
...     },
...     "verbose": True,
... }

# Retrieve a simple value
>>> get_config(cfg, "verbose")
True

# Retrieve a nested value
>>> get_config(cfg, "model.paths.data")
'input/data.csv'

# Retrieve as absolute path relative to a model root
>>> root = Path("/home/user/wflow_model")
>>> get_config(cfg, "model.paths.data", root=root, abs_path=True)
Path('/home/user/wflow_model/input/data.csv')

# Retrieve a non-existent key with fallback
>>> get_config(cfg, "model.output.folder", fallback="output")
'output'