|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from benedict.dicts.io import IODict |
|
4
|
|
|
from benedict.dicts.keypath import KeypathDict |
|
5
|
|
|
from benedict.dicts.parse import ParseDict |
|
6
|
|
|
from benedict.utils import dict_util |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class benedict(IODict, KeypathDict, ParseDict): |
|
10
|
|
|
|
|
11
|
|
|
def __init__(self, *args, **kwargs): |
|
12
|
|
|
""" |
|
13
|
|
|
Constructs a new instance. |
|
14
|
|
|
""" |
|
15
|
|
|
super(benedict, self).__init__(*args, **kwargs) |
|
16
|
|
|
|
|
17
|
|
|
def clean(self, strings=True, dicts=True, lists=True): |
|
18
|
|
|
""" |
|
19
|
|
|
Clean the current dict instance removing all empty values: None, '', {}, [], (). |
|
20
|
|
|
If strings, dicts or lists flags are False, related empty values will not be deleted. |
|
21
|
|
|
""" |
|
22
|
|
|
dict_util.clean(self, strings=strings, dicts=dicts, lists=lists) |
|
23
|
|
|
|
|
24
|
|
|
def clone(self): |
|
25
|
|
|
""" |
|
26
|
|
|
Creates and return a clone of the current dict instance (deep copy). |
|
27
|
|
|
""" |
|
28
|
|
|
return benedict(dict_util.clone(self), |
|
29
|
|
|
keypath_separator=self._keypath_separator) |
|
30
|
|
|
|
|
31
|
|
|
def copy(self): |
|
32
|
|
|
""" |
|
33
|
|
|
Creates and return a copy of the current instance (shallow copy). |
|
34
|
|
|
""" |
|
35
|
|
|
return benedict(super(benedict, self).copy(), |
|
36
|
|
|
keypath_separator=self._keypath_separator) |
|
37
|
|
|
|
|
38
|
|
|
def deepcopy(self): |
|
39
|
|
|
""" |
|
40
|
|
|
Alias of 'clone' method. |
|
41
|
|
|
""" |
|
42
|
|
|
return self.clone() |
|
43
|
|
|
|
|
44
|
|
|
def deepupdate(self, other, *args): |
|
45
|
|
|
""" |
|
46
|
|
|
Alias of 'merge' method. |
|
47
|
|
|
""" |
|
48
|
|
|
self.merge(other, *args) |
|
49
|
|
|
|
|
50
|
|
|
def dump(self, data=None): |
|
51
|
|
|
""" |
|
52
|
|
|
Return a readable string representation of any dict/list. |
|
53
|
|
|
This method can be used both as static method or instance method. |
|
54
|
|
|
""" |
|
55
|
|
|
return dict_util.dump(data or self) |
|
56
|
|
|
|
|
57
|
|
|
def filter(self, predicate): |
|
58
|
|
|
""" |
|
59
|
|
|
Return a new filtered dict using the given predicate function. |
|
60
|
|
|
Predicate function receives key, value arguments and should return a bool value. |
|
61
|
|
|
""" |
|
62
|
|
|
return dict_util.filter(self, predicate) |
|
63
|
|
|
|
|
64
|
|
|
def flatten(self, separator='_'): |
|
65
|
|
|
""" |
|
66
|
|
|
Return a new flattened dict using the given separator |
|
67
|
|
|
to join nested dict keys to flatten keypaths. |
|
68
|
|
|
""" |
|
69
|
|
|
return dict_util.flatten(self, separator) |
|
70
|
|
|
|
|
71
|
|
|
def invert(self, flat=False): |
|
72
|
|
|
""" |
|
73
|
|
|
Return a new inverted dict, where values become keys and keys become values. |
|
74
|
|
|
Since multiple keys could have the same value, each value will be a list of keys. |
|
75
|
|
|
If flat is True each value will be a single value (use this only if values are unique). |
|
76
|
|
|
""" |
|
77
|
|
|
return dict_util.invert(self, flat) |
|
78
|
|
|
|
|
79
|
|
|
def items_sorted_by_keys(self, reverse=False): |
|
80
|
|
|
""" |
|
81
|
|
|
Return items (key/value list) sorted by keys. |
|
82
|
|
|
If reverse is True, the list will be reversed. |
|
83
|
|
|
""" |
|
84
|
|
|
return dict_util.items_sorted_by_keys(self, reverse=reverse) |
|
85
|
|
|
|
|
86
|
|
|
def items_sorted_by_values(self, reverse=False): |
|
87
|
|
|
""" |
|
88
|
|
|
Return items (key/value list) sorted by values. |
|
89
|
|
|
If reverse is True, the list will be reversed. |
|
90
|
|
|
""" |
|
91
|
|
|
return dict_util.items_sorted_by_values(self, reverse=reverse) |
|
92
|
|
|
|
|
93
|
|
|
def keypaths(self): |
|
94
|
|
|
""" |
|
95
|
|
|
Return a list of all keypaths in the dict. |
|
96
|
|
|
""" |
|
97
|
|
|
sep = self._keypath_separator or '.' |
|
98
|
|
|
return dict_util.keypaths(self, separator=sep) |
|
99
|
|
|
|
|
100
|
|
|
def merge(self, other, *args): |
|
101
|
|
|
""" |
|
102
|
|
|
Merge one or more dict objects into current instance (deepupdate). |
|
103
|
|
|
Sub-dictionaries will be merged toghether. |
|
104
|
|
|
""" |
|
105
|
|
|
dict_util.merge(self, other, *args) |
|
106
|
|
|
|
|
107
|
|
|
def move(self, key_src, key_dest): |
|
108
|
|
|
""" |
|
109
|
|
|
Move a dict instance value item from 'key_src' to 'key_dst'. |
|
110
|
|
|
If key_dst exists, its value will be overwritten. |
|
111
|
|
|
""" |
|
112
|
|
|
dict_util.move(self, key_src, key_dest) |
|
113
|
|
|
|
|
114
|
|
|
def remove(self, keys, *args): |
|
115
|
|
|
""" |
|
116
|
|
|
Remove multiple keys from the current dict instance. |
|
117
|
|
|
It is possible to pass a single key or more keys (as list or *args). |
|
118
|
|
|
""" |
|
119
|
|
|
dict_util.remove(self, keys, *args) |
|
120
|
|
|
|
|
121
|
|
|
def rename(self, key, key_new): |
|
122
|
|
|
""" |
|
123
|
|
|
Rename a dict item key from 'key' to 'key_new'. |
|
124
|
|
|
If key_new exists, a KeyError will be raised. |
|
125
|
|
|
""" |
|
126
|
|
|
dict_util.rename(self, key, key_new) |
|
127
|
|
|
|
|
128
|
|
|
def search(self, query, in_keys=True, in_values=True, exact=False, case_sensitive=False): |
|
129
|
|
|
""" |
|
130
|
|
|
Search and return a list of items (dict, key, value, ) matching the given query. |
|
131
|
|
|
""" |
|
132
|
|
|
return dict_util.search(self, query, in_keys, in_values, exact, case_sensitive) |
|
133
|
|
|
|
|
134
|
|
|
def standardize(self): |
|
135
|
|
|
""" |
|
136
|
|
|
Standardize all dict keys (e.g. 'Location Latitude' -> 'location_latitude'). |
|
137
|
|
|
""" |
|
138
|
|
|
dict_util.standardize(self) |
|
139
|
|
|
|
|
140
|
|
|
def subset(self, keys, *args): |
|
141
|
|
|
""" |
|
142
|
|
|
Return a new dict subset for the given keys. |
|
143
|
|
|
It is possible to pass a single key or multiple keys (as list or *args). |
|
144
|
|
|
""" |
|
145
|
|
|
return dict_util.subset(self, keys, *args) |
|
146
|
|
|
|
|
147
|
|
|
def swap(self, key1, key2): |
|
148
|
|
|
""" |
|
149
|
|
|
Swap items values at the given keys. |
|
150
|
|
|
""" |
|
151
|
|
|
dict_util.swap(self, key1, key2) |
|
152
|
|
|
|
|
153
|
|
|
def traverse(self, callback): |
|
154
|
|
|
""" |
|
155
|
|
|
Traverse the current dict instance (including nested dicts), |
|
156
|
|
|
and pass each item (dict, key, value) to the callback function. |
|
157
|
|
|
""" |
|
158
|
|
|
dict_util.traverse(self, callback) |
|
159
|
|
|
|
|
160
|
|
|
def unflatten(self, separator='_'): |
|
161
|
|
|
""" |
|
162
|
|
|
Return a new unflattened dict using the given separator |
|
163
|
|
|
to split dict keys to nested keypaths. |
|
164
|
|
|
""" |
|
165
|
|
|
return dict_util.unflatten(self, separator) |
|
166
|
|
|
|
|
167
|
|
|
def unique(self): |
|
168
|
|
|
""" |
|
169
|
|
|
Remove duplicated values from the current dict instance. |
|
170
|
|
|
""" |
|
171
|
|
|
dict_util.unique(self) |
|
172
|
|
|
|