Passed
Push — master ( db2481...3bd527 )
by Fabio
03:28
created

benedict.core.keypaths.keypaths()   A

Complexity

Conditions 5

Size

Total Lines 15
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 15
rs 9.2333
c 0
b 0
f 0
cc 5
nop 2
1
# -*- coding: utf-8 -*-
2
3
from benedict.utils import type_util
4
5
6
def keypaths(d, separator='.'):
7
    if not separator or not type_util.is_string(separator):
8
        raise ValueError('separator argument must be a (non-empty) string.')
9
10
    def f(parent, parent_keys):
11
        kp = []
12
        for key, value in parent.items():
13
            keys = parent_keys + [key]
14
            kp += [separator.join('{}'.format(k) for k in keys)]
15
            if type_util.is_dict(value):
16
                kp += f(value, keys)
17
        return kp
18
    kp = f(d, [])
19
    kp.sort()
20
    return kp
21