benedict.dicts.keypath.keypath_dict   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 48
dl 0
loc 63
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A KeypathDict.keypath_separator() 0 3 1
A KeypathDict.__delitem__() 0 2 1
A KeypathDict.__getitem__() 0 2 1
A KeypathDict.__contains__() 0 2 1
A KeypathDict.update() 0 3 1
A KeypathDict.get() 0 2 1
A KeypathDict.pop() 0 2 1
A KeypathDict._parse_key() 0 8 3
A KeypathDict.__setitem__() 0 3 1
A KeypathDict.__init__() 0 6 2
A KeypathDict.fromkeys() 0 6 2
1
from benedict.dicts import KeylistDict
2
from benedict.dicts.keypath import keypath_util
3
4
5
class KeypathDict(KeylistDict):
6
7
    _keypath_separator = None
8
9
    def __init__(self, *args, **kwargs):
10
        self._keypath_separator = kwargs.pop("keypath_separator", ".")
11
        check_keys = kwargs.pop("check_keys", True)
12
        super().__init__(*args, **kwargs)
13
        if check_keys:
14
            keypath_util.check_keys(self, self._keypath_separator)
15
16
    @property
17
    def keypath_separator(self):
18
        return self._keypath_separator
19
20
    @keypath_separator.setter
21
    def keypath_separator(self, value):
22
        keypath_util.check_keys(self, value)
23
        self._keypath_separator = value
24
25
    def __contains__(self, key):
26
        return super().__contains__(self._parse_key(key))
27
28
    def __delitem__(self, key):
29
        super().__delitem__(self._parse_key(key))
30
31
    def __getitem__(self, key):
32
        return super().__getitem__(self._parse_key(key))
33
34
    def __setitem__(self, key, value):
35
        keypath_util.check_keys(value, self._keypath_separator)
36
        super().__setitem__(self._parse_key(key), value)
37
38
    def _parse_key(self, key):
39
        keys = keypath_util.parse_keys(key, self._keypath_separator)
40
        keys_count = len(keys)
41
        if keys_count == 0:
42
            return None
43
        elif keys_count == 1:
44
            return keys[0]
45
        return keys
46
47
    @classmethod
48
    def fromkeys(cls, sequence, value=None):
49
        d = cls()
50
        for key in sequence:
51
            d[key] = value
52
        return d
53
54
    def get(self, key, default=None):
55
        return super().get(self._parse_key(key), default)
56
57
    def pop(self, key, *args):
58
        return super().pop(self._parse_key(key), *args)
59
60
    def update(self, other):
61
        keypath_util.check_keys(other, self._keypath_separator)
62
        super().update(other)
63