KeypathDict.__delitem__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 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