Skip to content

confit.utils.collections

join_path(path)

Join a path into a string and quotes subpaths that contain dots.

PARAMETER DESCRIPTION
path

RETURNS DESCRIPTION
str
Source code in confit/utils/collections.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def join_path(path):
    """
    Join a path into a string and quotes subpaths that contain dots.

    Parameters
    ----------
    path: Tuple[Union[int, str]]

    Returns
    -------
    str
    """
    return ".".join(
        repr(x) if not isinstance(x, str) or split_path(x.strip()) != (x,) else x
        for x in path
    )

split_path(path)

Split a path around "." into a tuple of strings and ints. If a sub-path is quoted, it will be returned as a full non-split string.

PARAMETER DESCRIPTION
path

TYPE: str

Source code in confit/utils/collections.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def split_path(path: str) -> Tuple[Union[int, str]]:
    """
    Split a path around "." into a tuple of strings and ints.
    If a sub-path is quoted, it will be returned as a full non-split string.

    Parameters
    ----------
    path: str

    Returns
    -------

    """
    offset = 0
    result = []
    for match in re.finditer(KEY_PART, str(path)):
        assert match.start() == offset, f"Malformed path: {path!r} in config"
        offset = match.end()
        part = next((g for g in match.groups() if g is not None))
        result.append(int(part) if part.isdigit() else part)
        if offset == len(path):
            break
    return tuple(result)

flatten_sections(root)

Flatten a nested dict of dicts into a "flat" dict of dict.

PARAMETER DESCRIPTION
root

The root dict to flatten

TYPE: Dict[str, Any]

RETURNS DESCRIPTION
Dict[str, Dict[str, Any]]
Source code in confit/utils/collections.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def flatten_sections(root: Dict[str, Any]) -> Dict[str, Any]:
    """
    Flatten a nested dict of dicts into a "flat" dict of dict.

    Parameters
    ----------
    root: Dict[str, Any]
        The root dict to flatten

    Returns
    -------
    Dict[str, Dict[str, Any]]
    """
    res = collections.defaultdict(lambda: {})

    def rec(d, path):
        res.setdefault(join_path(path), {})
        section = {}
        for k, v in d.items():
            if isinstance(v, dict):
                rec(v, (*path, k))
            else:
                section[k] = v
        res[join_path(path)].update(section)

    rec(root, ())
    root_level = res.pop("", None)
    if root_level is not None and len(root_level) > 0:
        raise Exception("Cannot dump root level config", root_level)
    return dict(res)