Passed
Push — master ( eae58f...1f7285 )
by Fabio
03:34
created

benedict.dicts.benedict.__deepcopy__()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
# -*- coding: utf-8 -*-
2
3
from benedict.core import clean as _clean
4
from benedict.core import clone as _clone
5
from benedict.core import dump as _dump
6
from benedict.core import filter as _filter
7
from benedict.core import find as _find
8
from benedict.core import flatten as _flatten
9
from benedict.core import groupby as _groupby
10
from benedict.core import invert as _invert
11
from benedict.core import items_sorted_by_keys as _items_sorted_by_keys
12
from benedict.core import items_sorted_by_values as _items_sorted_by_values
13
from benedict.core import keypaths as _keypaths
14
from benedict.core import match as _match
15
from benedict.core import merge as _merge
16
from benedict.core import move as _move
17
from benedict.core import nest as _nest
18
from benedict.core import remove as _remove
19
from benedict.core import rename as _rename
20
from benedict.core import search as _search
21
from benedict.core import standardize as _standardize
22
from benedict.core import subset as _subset
23
from benedict.core import swap as _swap
24
from benedict.core import traverse as _traverse
25
from benedict.core import unflatten as _unflatten
26
from benedict.core import unique as _unique
27
from benedict.dicts.io import IODict
28
from benedict.dicts.keylist import KeylistDict
29
from benedict.dicts.keypath import KeypathDict
30
from benedict.dicts.parse import ParseDict
31
32
33
class benedict(KeypathDict, IODict, ParseDict):
34
35
    def __init__(self, *args, **kwargs):
36
        """
37
        Constructs a new instance.
38
        """
39
        if len(args) == 1 and isinstance(args[0], benedict):
40
            obj = args[0]
41
            kwargs.setdefault('keypath_separator', obj.keypath_separator)
42
            super(benedict, self).__init__(obj.dict(), **kwargs)
43
            return
44
        super(benedict, self).__init__(*args, **kwargs)
45
46
    def __deepcopy__(self, memo):
47
        obj = benedict(keypath_separator=self._keypath_separator)
48
        for key, value in self.items():
49
            obj[key] = _clone(value, memo=memo)
50
        return obj
51
52
    def __getitem__(self, key):
53
        return self._cast(
54
            super(benedict, self).__getitem__(key))
55
56
    def _cast(self, value):
57
        """
58
        Cast a dict instance to a benedict instance
59
        keeping the pointer to the original dict.
60
        """
61
        if isinstance(value, dict) and not isinstance(value, benedict):
62
            return benedict(value,
63
                            keypath_separator=self._keypath_separator,
64
                            check_keys=False)
65
        return value
66
67
    def clean(self, strings=True, collections=True):
68
        """
69
        Clean the current dict instance removing all empty values: None, '', {}, [], ().
70
        If strings or collections (dict, list, set, tuple) flags are False,
71
        related empty values will not be deleted.
72
        """
73
        _clean(self, strings=strings, collections=collections)
74
75
    def clone(self):
76
        """
77
        Creates and return a clone of the current dict instance (deep copy).
78
        """
79
        return self._cast(
80
            _clone(self))
81
82
    def copy(self):
83
        """
84
        Creates and return a copy of the current instance (shallow copy).
85
        """
86
        return self._cast(
87
            super(benedict, self).copy())
88
89
    def deepcopy(self):
90
        """
91
        Alias of 'clone' method.
92
        """
93
        return self.clone()
94
95
    def deepupdate(self, other, *args):
96
        """
97
        Alias of 'merge' method.
98
        """
99
        self.merge(other, *args)
100
101
    def dump(self, data=None):
102
        """
103
        Return a readable string representation of any dict/list.
104
        This method can be used both as static method or instance method.
105
        """
106
        return _dump(data or self)
107
108
    def filter(self, predicate):
109
        """
110
        Return a new filtered dict using the given predicate function.
111
        Predicate function receives key, value arguments and should return a bool value.
112
        """
113
        return _filter(self, predicate)
114
115
    def find(self, keys, default=None):
116
        """
117
        Return the first match searching for the given keys.
118
        If no result found, default value is returned.
119
        """
120
        return _find(self, keys, default)
121
122
    def flatten(self, separator='_'):
123
        """
124
        Return a new flattened dict using the given separator
125
        to join nested dict keys to flatten keypaths.
126
        """
127
        return _flatten(self, separator)
128
129
    def get(self, key, default=None):
130
        return self._cast(
131
            super(benedict, self).get(key, default))
132
133
    def get_dict(self, key, default=None):
134
        return self._cast(
135
            super(benedict, self).get_dict(key, default))
136
137
    def get_list_item(self, key, index=0, default=None, separator=','):
138
        return self._cast(
139
            super(benedict, self).get_list_item(
140
                key, index, default, separator))
141
142
    def groupby(self, key, by_key):
143
        """
144
        Group a list of dicts at key by the value of the given by_key and return a new dict.
145
        """
146
        return self._cast(
147
            _groupby(self[key], by_key))
148
149
    def invert(self, flat=False):
150
        """
151
        Return a new inverted dict, where values become keys and keys become values.
152
        Since multiple keys could have the same value, each value will be a list of keys.
153
        If flat is True each value will be a single value (use this only if values are unique).
154
        """
155
        return _invert(self, flat)
156
157
    def items_sorted_by_keys(self, reverse=False):
158
        """
159
        Return items (key/value list) sorted by keys.
160
        If reverse is True, the list will be reversed.
161
        """
162
        return _items_sorted_by_keys(self, reverse=reverse)
163
164
    def items_sorted_by_values(self, reverse=False):
165
        """
166
        Return items (key/value list) sorted by values.
167
        If reverse is True, the list will be reversed.
168
        """
169
        return _items_sorted_by_values(self, reverse=reverse)
170
171
    def keypaths(self, indexes=False):
172
        """
173
        Return a list of all keypaths in the dict.
174
        If indexes is True, the output will include list values indexes.
175
        """
176
        return _keypaths(
177
            self, separator=self._keypath_separator, indexes=indexes)
178
179
    def match(self, pattern, indexes=True):
180
        """
181
        Return a list of all values whose keypath matches the given pattern (a regex or string).
182
        If pattern is string, wildcard can be used (eg. [*] can be used to match all list indexes).
183
        If indexes is True, the pattern will be matched also against list values.
184
        """
185
        return _match(
186
            self, pattern, separator=self._keypath_separator, indexes=indexes)
187
188
    def merge(self, other, *args, **kwargs):
189
        """
190
        Merge one or more dict objects into current instance (deepupdate).
191
        Sub-dictionaries will be merged toghether.
192
        If overwrite is False, existing values will not be overwritten.
193
        If concat is True, list values will be concatenated toghether.
194
        """
195
        _merge(self, other, *args, **kwargs)
196
197
    def move(self, key_src, key_dest):
198
        """
199
        Move a dict instance value item from 'key_src' to 'key_dst'.
200
        If key_dst exists, its value will be overwritten.
201
        """
202
        _move(self, key_src, key_dest)
203
204
    def nest(self, key,
205
             id_key='id', parent_id_key='parent_id', children_key='children'):
206
        """
207
        Nest a list of dicts at the given key and return a new nested list
208
        using the specified keys to establish the correct items hierarchy.
209
        """
210
        return _nest(self[key], id_key, parent_id_key, children_key)
211
212
    def pop(self, key, *args):
213
        return self._cast(
214
            super(benedict, self).pop(key, *args))
215
216
    def remove(self, keys, *args):
217
        """
218
        Remove multiple keys from the current dict instance.
219
        It is possible to pass a single key or more keys (as list or *args).
220
        """
221
        _remove(self, keys, *args)
222
223
    def setdefault(self, key, default=None):
224
        return self._cast(
225
            super(benedict, self).setdefault(key, default))
226
227
    def rename(self, key, key_new):
228
        """
229
        Rename a dict item key from 'key' to 'key_new'.
230
        If key_new exists, a KeyError will be raised.
231
        """
232
        _rename(self, key, key_new)
233
234
    def search(self, query,
235
               in_keys=True, in_values=True,
236
               exact=False, case_sensitive=False):
237
        """
238
        Search and return a list of items (dict, key, value, ) matching the given query.
239
        """
240
        return _search(self, query, in_keys, in_values, exact, case_sensitive)
241
242
    def standardize(self):
243
        """
244
        Standardize all dict keys (e.g. 'Location Latitude' -> 'location_latitude').
245
        """
246
        _standardize(self)
247
248
    def subset(self, keys, *args):
249
        """
250
        Return a new dict subset for the given keys.
251
        It is possible to pass a single key or multiple keys (as list or *args).
252
        """
253
        return _subset(self, keys, *args)
254
255
    def swap(self, key1, key2):
256
        """
257
        Swap items values at the given keys.
258
        """
259
        _swap(self, key1, key2)
260
261
    def traverse(self, callback):
262
        """
263
        Traverse the current dict instance (including nested dicts),
264
        and pass each item (dict, key, value) to the callback function.
265
        """
266
        _traverse(self, callback)
267
268
    def unflatten(self, separator='_'):
269
        """
270
        Return a new unflattened dict using the given separator
271
        to split dict keys to nested keypaths.
272
        """
273
        return _unflatten(self, separator)
274
275
    def unique(self):
276
        """
277
        Remove duplicated values from the current dict instance.
278
        """
279
        _unique(self)
280
281
282
# fix benedict yaml representer - #43
283
from yaml import SafeDumper
284
from yaml.representer import SafeRepresenter
285
286
SafeDumper.yaml_representers[benedict] = SafeRepresenter.represent_dict
287