Skip to content

cdm_data_loaders.utils.paths

Generic nested dictionary path operations.

NestedPathError

Bases: ValueError

Raised for an invalid nested path or a non-dictionary intermediate value.

Source code in src/cdm_data_loaders/utils/paths.py
class NestedPathError(ValueError):
    """Raised for an invalid nested path or a non-dictionary intermediate value."""

set_nested

set_nested(
    target: dict[str, Any],
    path: Sequence[str],
    value: object,
) -> None

Set a leaf through a nonempty sequence of string keys, creating missing dicts.

Existing leaves are replaced, including dicts and lists. Empty string keys are valid. Intermediate values must be dicts; collisions fail without mutation. The assigned value is not copied. A string alone is not a path sequence.

Parameters:

Name Type Description Default
target dict[str, Any]

dictionary to mutate in place

required
path Sequence[str]

nonempty sequence of string keys leading to the leaf

required
value object

value to assign at the leaf key

required

Raises:

Type Description
NestedPathError

if path is empty, not a sequence of strings, or collides with a non-dict intermediate

Source code in src/cdm_data_loaders/utils/paths.py
def set_nested(target: dict[str, Any], path: Sequence[str], value: object) -> None:
    """Set a leaf through a nonempty sequence of string keys, creating missing dicts.

    Existing leaves are replaced, including dicts and lists. Empty string keys are
    valid. Intermediate values must be dicts; collisions fail without mutation.
    The assigned value is not copied. A string alone is not a path sequence.

    :param target: dictionary to mutate in place
    :type target: dict[str, Any]
    :param path: nonempty sequence of string keys leading to the leaf
    :type path: Sequence[str]
    :param value: value to assign at the leaf key
    :type value: object
    :raises NestedPathError: if ``path`` is empty, not a sequence of strings, or collides with a non-dict intermediate
    """
    if not path or isinstance(path, (str, bytes)) or not all(isinstance(fragment, str) for fragment in path):
        message = "Path must be a nonempty sequence of string keys."
        raise NestedPathError(message)
    current = target
    for fragment in path[:-1]:
        if fragment not in current:
            current[fragment] = {}
        child = current[fragment]
        if not isinstance(child, dict):
            message = f"Path collision at {fragment!r}: intermediate value must be a dict."
            raise NestedPathError(message)
        current = child
    current[path[-1]] = value