Passed
Push — master ( b3caf6...57589b )
by Fabio
02:22
created

benedict.dicts   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 87
dl 0
loc 212
rs 10
c 0
b 0
f 0

26 Methods

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