Passed
Push — master ( 68a360...546f3a )
by Fabio
03:54
created

benedict.dicts.utility   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 34
dl 0
loc 48
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A UtilityDict.dump() 0 2 1
A UtilityDict.deepcopy() 0 2 1
A UtilityDict.merge() 0 4 2
A UtilityDict.clean() 0 2 1
A UtilityDict.__init__() 0 2 1
A UtilityDict.subset() 0 5 2
A UtilityDict.deepupdate() 0 2 1
A UtilityDict.remove() 0 6 3
A UtilityDict.clone() 0 2 1
A UtilityDict.filter() 0 4 2
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 merge(self, other, *args):
32
        dicts = [other] + list(args)
33
        for d in dicts:
34
            utility_util.merge(self, d)
35
36
    def remove(self, keys):
37
        for key in keys:
38
            try:
39
                del self[key]
40
            except KeyError:
41
                continue
42
43
    def subset(self, keys):
44
        d = self.__class__()
45
        for key in keys:
46
            d[key] = self.get(key, None)
47
        return d
48