benedict.dicts.keylist.keylist_dict   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 32
eloc 78
dl 0
loc 96
rs 9.84
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A KeylistDict.set() 0 2 1
A KeylistDict.__delitem__() 0 5 2
A KeylistDict.pop() 0 4 2
A KeylistDict.__getitem__() 0 4 2
A KeylistDict.__setitem__() 0 5 2
A KeylistDict._getitem_by_keys() 0 5 2
A KeylistDict.setdefault() 0 5 2
A KeylistDict._pop_by_keys() 0 12 5
A KeylistDict.get() 0 4 2
A KeylistDict.__contains__() 0 4 2
A KeylistDict._delitem_by_keys() 0 9 3
A KeylistDict._contains_by_keys() 0 5 2
A KeylistDict.__init__() 0 2 1
A KeylistDict._get_by_keys() 0 7 3
A KeylistDict._setitem_by_keys() 0 2 1
1
from benedict.dicts.base import BaseDict
2
from benedict.dicts.keylist import keylist_util
3
from benedict.utils import type_util
4
5
6
class KeylistDict(BaseDict):
7
    def __init__(self, *args, **kwargs):
8
        super().__init__(*args, **kwargs)
9
10
    def __contains__(self, key):
11
        if type_util.is_list_or_tuple(key):
12
            return self._contains_by_keys(key)
13
        return super().__contains__(key)
14
15
    def _contains_by_keys(self, keys):
16
        parent, _, _ = keylist_util.get_item(self, keys)
17
        if type_util.is_dict_or_list_or_tuple(parent):
18
            return True
19
        return False
20
21
    def __delitem__(self, key):
22
        if type_util.is_list_or_tuple(key):
23
            self._delitem_by_keys(key)
24
            return
25
        super().__delitem__(key)
26
27
    def _delitem_by_keys(self, keys):
28
        parent, key, _ = keylist_util.get_item(self, keys)
29
        if type_util.is_dict_or_list(parent):
30
            del parent[key]
31
            return
32
        elif type_util.is_tuple(parent):
33
            # raise the standard TypeError
34
            del parent[key]
35
        raise KeyError(f"Invalid keys: '{keys}'")
36
37
    def __getitem__(self, key):
38
        if type_util.is_list_or_tuple(key):
39
            return self._getitem_by_keys(key)
40
        return super().__getitem__(key)
41
42
    def _getitem_by_keys(self, keys):
43
        parent, key, _ = keylist_util.get_item(self, keys)
44
        if type_util.is_dict_or_list_or_tuple(parent):
45
            return parent[key]
46
        raise KeyError(f"Invalid keys: '{keys}'")
47
48
    def __setitem__(self, key, value):
49
        if type_util.is_list_or_tuple(key):
50
            self._setitem_by_keys(key, value)
51
            return
52
        super().__setitem__(key, value)
53
54
    def _setitem_by_keys(self, keys, value):
55
        keylist_util.set_item(self, keys, value)
56
57
    def get(self, key, default=None):
58
        if type_util.is_list_or_tuple(key):
59
            return self._get_by_keys(key, default)
60
        return super().get(key, default)
61
62
    def _get_by_keys(self, keys, default=None):
63
        parent, key, _ = keylist_util.get_item(self, keys)
64
        if type_util.is_dict(parent):
65
            return parent.get(key, default)
66
        elif type_util.is_list_or_tuple(parent):
67
            return parent[key]
68
        return default
69
70
    def pop(self, key, *args):
71
        if type_util.is_list_or_tuple(key):
72
            return self._pop_by_keys(key, *args)
73
        return super().pop(key, *args)
74
75
    def _pop_by_keys(self, keys, *args):
76
        parent, key, _ = keylist_util.get_item(self, keys)
77
        if type_util.is_dict(parent):
78
            return parent.pop(key, *args)
79
        elif type_util.is_list(parent):
80
            return parent.pop(key)
81
        elif type_util.is_tuple(parent):
82
            # raise the standard TypeError
83
            del parent[key]
84
        if args:
85
            return args[0]
86
        raise KeyError(f"Invalid keys: '{keys}'")
87
88
    def set(self, key, value):
89
        self[key] = value
90
91
    def setdefault(self, key, default=None):
92
        if key not in self:
93
            self[key] = default
94
            return default
95
        return self[key]
96