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

benedict.dicts.keypath.keypath_dict   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 52
dl 0
loc 69
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A KeypathDict.__delitem__() 0 3 1
A KeypathDict.__getitem__() 0 3 1
A KeypathDict.__contains__() 0 3 1
A KeypathDict.keypath_separator() 0 3 1
A KeypathDict.update() 0 3 1
A KeypathDict.get() 0 3 1
A KeypathDict.pop() 0 3 1
A KeypathDict._parse_key() 0 8 3
A KeypathDict.__setitem__() 0 4 1
A KeypathDict.__init__() 0 4 1
A KeypathDict.fromkeys() 0 6 2
1
# -*- coding: utf-8 -*-
2
3
from benedict.dicts import KeylistDict
4
from benedict.dicts.keypath import keypath_util
5
6
7
class KeypathDict(KeylistDict):
8
9
    _keypath_separator = None
10
11
    def __init__(self, *args, **kwargs):
12
        self._keypath_separator = kwargs.pop('keypath_separator', '.')
13
        super(KeypathDict, self).__init__(*args, **kwargs)
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(KeypathDict, self).__contains__(
27
            self._parse_key(key))
28
29
    def __delitem__(self, key):
30
        super(KeypathDict, self).__delitem__(
31
            self._parse_key(key))
32
33
    def __getitem__(self, key):
34
        return super(KeypathDict, self).__getitem__(
35
            self._parse_key(key))
36
37
    def __setitem__(self, key, value):
38
        keypath_util.check_keys(value, self._keypath_separator)
39
        super(KeypathDict, self).__setitem__(
40
            self._parse_key(key), value)
41
42
    def _parse_key(self, key):
43
        keys = keypath_util.parse_keys(key, self._keypath_separator)
44
        keys_count = len(keys)
45
        if keys_count == 0:
46
            return None
47
        elif keys_count == 1:
48
            return keys[0]
49
        return keys
50
51
    @classmethod
52
    def fromkeys(cls, sequence, value=None):
53
        d = cls()
54
        for key in sequence:
55
            d[key] = value
56
        return d
57
58
    def get(self, key, default=None):
59
        return super(KeypathDict, self).get(
60
            self._parse_key(key), default)
61
62
    def pop(self, key, *args):
63
        return super(KeypathDict, self).pop(
64
            self._parse_key(key), *args)
65
66
    def update(self, other):
67
        keypath_util.check_keys(other, self._keypath_separator)
68
        super(KeypathDict, self).update(other)
69
70