Skip to content

Dict utils

flatten(obj: dict[str, Any], sep: str, name: str | None = None) -> dict[str, Any]

Flatten dictionary by combining nested keys with given separator.

Source code in opskrift/dict_utils.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def flatten(obj: dict[str, Any], sep: str, name: str | None = None) -> dict[str, Any]:
    """Flatten dictionary by combining nested keys with given separator."""
    items: list[tuple[str, Any]] = []

    for k, v in obj.items():
        new_key = f"{name}{sep}{k}" if name else k
        if not isinstance(v, dict) or len(v) == 0:
            items.append((new_key, v))
        else:
            items.extend(flatten(v, sep=sep, name=new_key).items())

    return dict(items)

unflatten(obj: dict[str, Any], sep: str) -> dict[str, Any]

Construct a nested dictionary by splitting keys on given separator.

Source code in opskrift/dict_utils.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def unflatten(obj: dict[str, Any], sep: str) -> dict[str, Any]:
    """Construct a nested dictionary by splitting keys on given separator."""
    out: dict[str, Any] = {}

    for key, value in obj.items():
        *init, last = key.split(sep)
        d = out

        for k in init:
            if k not in d:
                d[k] = {}

            d = d[k]

        d[last] = value

    return out