Passed
Push — master ( b49b7e...60d050 )
by Fabio
59s
created

benedict.dicts.benedict.pop()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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