You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
python-benedict is a dict subclass with keypath support, I/O shortcuts (json, toml, xml, yaml) and many utilities... for humans, obviously.
12
+
python-benedict is a dict subclass with **keypath* support, **I/O** shortcuts (BASE64, JSON, TOML, XML, YAML) and many **utilities**... for humans, obviously.
13
13
14
14
## Index
15
15
-[Features](#features)
@@ -22,11 +22,25 @@ python-benedict is a dict subclass with keypath support, I/O shortcuts (json, to
@@ -51,15 +65,7 @@ python-benedict is a dict subclass with keypath support, I/O shortcuts (json, to
51
65
-[`get_slug_list`](#get_slug_list)
52
66
-[`get_str`](#get_str)
53
67
-[`get_str_list`](#get_str_list)
54
-
-[Utility](#utility)
55
-
-[`clean`](#clean)
56
-
-[`clone`](#clone)
57
-
-[`dump`](#dump)
58
-
-[`filter`](#filter)
59
-
-[`flatten`](#flatten)
60
-
-[`merge`](#merge)
61
-
-[`remove`](#remove)
62
-
-[`subset`](#subset)
68
+
63
69
-[Testing](#testing)
64
70
-[License](#license)
65
71
@@ -91,7 +97,7 @@ d = benedict()
91
97
# or cast an existing dict
92
98
d = benedict(existing_dict)
93
99
94
-
# or create from data source (filepath, url or data-string) in a supported format (json, toml, xml, yaml)
100
+
# or create from data source (filepath, url or data-string) in a supported format (base64, json, toml, xml, yaml)
95
101
d = benedict('https://localhost:8000/data.json')
96
102
97
103
# or in a Django view
@@ -147,6 +153,97 @@ d = benedict(existing_dict, keypath_separator=None)
147
153
148
154
## API
149
155
156
+
#### Utility
157
+
These methods are common utilities that will speed up your everyday work.
158
+
159
+
-##### clean
160
+
161
+
```python
162
+
# Clean the current dict removing all empty values: None, '', {}, [], ().
163
+
# If strings, dicts or lists flags are False, related empty values will not be deleted.
164
+
d.clean(strings=True, dicts=True, lists=True)
165
+
```
166
+
167
+
-##### clone
168
+
169
+
```python
170
+
# Return a clone (deepcopy) of the dict.
171
+
c = d.clone()
172
+
```
173
+
174
+
-##### dump
175
+
176
+
```python
177
+
# Return a readable representation of any dict/list.
178
+
# This method can be used both as static method or instance method.
179
+
s = benedict.dump(d.keypaths())
180
+
print(s)
181
+
# or
182
+
d = benedict()
183
+
print(d.dump())
184
+
```
185
+
186
+
-##### filter
187
+
188
+
```python
189
+
# Return a filtered dict using the given predicate function.
190
+
# Predicate function receives key, value arguments and should return a bool value.
191
+
predicate =lambdak, v: v isnotNone
192
+
f = d.filter(predicate)
193
+
```
194
+
195
+
-##### flatten
196
+
197
+
```python
198
+
# Return a flatten dict using the given separator to concat nested dict keys.
199
+
f = d.flatten(separator='_')
200
+
```
201
+
202
+
-##### invert
203
+
204
+
```python
205
+
# Return an inverted dict swapping values with keys.
206
+
# Since multiple keys could have the same value, each value will be a list.
207
+
# If flat is True each value will be a single value.
208
+
i = d.invert(flat=False)
209
+
```
210
+
211
+
-##### items_sorted_by_keys
212
+
213
+
```python
214
+
# Return items ((key, value, ) list) sorted by keys, if reverse is True, the list will be reversed.
215
+
items = d.items_sorted_by_keys(reverse=False)
216
+
```
217
+
218
+
-##### items_sorted_by_values
219
+
220
+
```python
221
+
# Return items ((key, value, ) list) sorted by values, if reverse is True, the list will be reversed.
222
+
items = d.items_sorted_by_values(reverse=False)
223
+
```
224
+
225
+
-##### merge
226
+
227
+
```python
228
+
# Merge one or more dictionary objects into current instance (deepupdate).
229
+
# Sub-dictionaries keys will be merged toghether.
230
+
d.merge(a, b, c)
231
+
```
232
+
233
+
-##### remove
234
+
235
+
```python
236
+
# Remove multiple keys from the dict.
237
+
d.remove(['firstname', 'lastname', 'email'])
238
+
```
239
+
240
+
-##### subset
241
+
242
+
```python
243
+
# Return a dict subset for the given keys.
244
+
s = d.subset(['firstname', 'lastname', 'email'])
245
+
```
246
+
150
247
### I/O
151
248
152
249
It is possible to create a `benedict` instance directly from data source (filepath, url or data-string) by passing the data source as first argument in the constructor.
@@ -162,7 +259,16 @@ d = benedict('https://localhost:8000/data.xml')
0 commit comments