Completed
Push — master ( 07e208...06c740 )
by Fabio
03:33
created

benedict.dicts.benedict.flatten()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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