Passed
Push — master ( df1182...461010 )
by Fabio
01:14
created

benedict.dicts.utility   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 36
dl 0
loc 51
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A UtilityDict.dump() 0 2 1
A UtilityDict.deepcopy() 0 2 1
A UtilityDict.clean() 0 2 1
A UtilityDict.__init__() 0 2 1
A UtilityDict.deepupdate() 0 2 1
A UtilityDict.clone() 0 2 1
A UtilityDict.filter() 0 4 2
A UtilityDict.merge() 0 4 2
A UtilityDict.flatten() 0 2 1
A UtilityDict.subset() 0 5 2
A UtilityDict.remove() 0 6 3
1
# -*- coding: utf-8 -*-
2
3
from benedict.utils import utility_util
4
5
6
class UtilityDict(dict):
7
8
    def __init__(self, *args, **kwargs):
9
        super(UtilityDict, self).__init__(*args, **kwargs)
10
11
    def clean(self, strings=True, dicts=True, lists=True):
12
        utility_util.clean(self, strings=strings, dicts=dicts, lists=lists)
13
14
    def clone(self):
15
        return utility_util.clone(self)
16
17
    def deepcopy(self):
18
        return self.clone()
19
20
    def deepupdate(self, other, *args):
21
        self.merge(other, *args)
22
23
    def dump(self, data=None):
24
        return utility_util.dump(data or self)
25
26
    def filter(self, predicate):
27
        if not callable(predicate):
28
            raise ValueError('predicate argument must be a callable.')
29
        return utility_util.filter(self, predicate)
30
31
    def flatten(self, separator='_'):
32
        return utility_util.flatten(self, separator)
33
34
    def merge(self, other, *args):
35
        dicts = [other] + list(args)
36
        for d in dicts:
37
            utility_util.merge(self, d)
38
39
    def remove(self, keys):
40
        for key in keys:
41
            try:
42
                del self[key]
43
            except KeyError:
44
                continue
45
46
    def subset(self, keys):
47
        d = self.__class__()
48
        for key in keys:
49
            d[key] = self.get(key, None)
50
        return d
51