Passed
Push — master ( 3136b5...f980db )
by Fabio
01:24
created

benedict.dicts.benedict.move()   A

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 3
1
# -*- coding: utf-8 -*-
2
3
from six import string_types
4
5
from benedict.dicts.io import IODict
6
from benedict.dicts.keypath import KeypathDict
7
from benedict.dicts.parse import ParseDict
8
from benedict.utils import dict_util
9
10
11
def benediction(method):
12
    def wrapper(*args, **kwargs):
13
        value = method(*args, **kwargs)
14
        if isinstance(value, dict) and not isinstance(value, benedict):
15
            return benedict(value)
16
        return value
17
    return wrapper
18
19
20
class benedict(IODict, KeypathDict, ParseDict):
21
22
    def __init__(self, *args, **kwargs):
23
        super(benedict, self).__init__(*args, **kwargs)
24
25
    def clean(self, strings=True, dicts=True, lists=True):
26
        dict_util.clean(self, strings=strings, dicts=dicts, lists=lists)
27
28
    @benediction
29
    def clone(self):
30
        return dict_util.clone(self)
31
32
    @benediction
33
    def copy(self):
34
        return super(benedict, self).copy()
35
36
    def deepcopy(self):
37
        return self.clone()
38
39
    def deepupdate(self, other, *args):
40
        self.merge(other, *args)
41
42
    def dump(self, data=None):
43
        return dict_util.dump(data or self)
44
45
    @benediction
46
    def filter(self, predicate):
47
        return dict_util.filter(self, predicate)
48
49
    @benediction
50
    def flatten(self, separator='_'):
51
        return dict_util.flatten(self, separator)
52
53
    @classmethod
54
    @benediction
55
    def fromkeys(cls, sequence, value=None):
56
        return KeypathDict.fromkeys(sequence, value)
57
58
    @staticmethod
59
    @benediction
60
    def from_base64(s, **kwargs):
61
        return IODict.from_base64(s, **kwargs)
62
63
    @staticmethod
64
    @benediction
65
    def from_json(s, **kwargs):
66
        return IODict.from_json(s, **kwargs)
67
68
    @staticmethod
69
    @benediction
70
    def from_toml(s, **kwargs):
71
        return IODict.from_toml(s, **kwargs)
72
73
    @staticmethod
74
    @benediction
75
    def from_xml(s, **kwargs):
76
        return IODict.from_xml(s, **kwargs)
77
78
    @staticmethod
79
    @benediction
80
    def from_yaml(s, **kwargs):
81
        return IODict.from_yaml(s, **kwargs)
82
83
    @benediction
84
    def invert(self, flat=False):
85
        return dict_util.invert(self, flat)
86
87
    def items_sorted_by_keys(self, reverse=False):
88
        return dict_util.items_sorted_by_keys(self, reverse=reverse)
89
90
    def items_sorted_by_values(self, reverse=False):
91
        return dict_util.items_sorted_by_values(self, reverse=reverse)
92
93
    def merge(self, other, *args):
94
        dicts = [other] + list(args)
95
        for d in dicts:
96
            dict_util.merge(self, d)
97
98
    def move(self, key_src, key_dest):
99
        self[key_dest] = self.pop(key_src)
100
101
    def remove(self, keys, *args):
102
        if isinstance(keys, string_types):
103
            keys = [keys]
104
        keys += args
105
        for key in keys:
106
            try:
107
                del self[key]
108
            except KeyError:
109
                continue
110
111
    @benediction
112
    def subset(self, keys, *args):
113
        d = self.__class__()
114
        if isinstance(keys, string_types):
115
            keys = [keys]
116
        keys += args
117
        for key in keys:
118
            d[key] = self.get(key, None)
119
        return d
120
121
    def swap(self, key1, key2):
122
        self[key1], self[key2] = self[key2], self[key1]
123