@@ -31,7 +31,7 @@ Using Dpath
31
31
32
32
.. code-block :: python
33
33
34
- import dpath.util
34
+ import dpath
35
35
36
36
Separators
37
37
==========
@@ -63,8 +63,8 @@ key '43' in the 'b' hash which is in the 'a' hash". That's easy.
63
63
64
64
.. code-block :: pycon
65
65
66
- >>> help(dpath.util. get)
67
- Help on function get in module dpath.util :
66
+ >>> help(dpath.get)
67
+ Help on function get in module dpath:
68
68
69
69
get(obj, glob, separator='/')
70
70
Given an object which contains only one possible match for the given glob,
@@ -73,16 +73,16 @@ key '43' in the 'b' hash which is in the 'a' hash". That's easy.
73
73
If more than one leaf matches the glob, ValueError is raised. If the glob is
74
74
not found, KeyError is raised.
75
75
76
- >>> dpath.util. get(x, '/a/b/43')
76
+ >>> dpath.get(x, '/a/b/43')
77
77
30
78
78
79
79
Or you could say "Give me a new dictionary with the values of all
80
80
elements in ``x['a']['b'] `` where the key is equal to the glob ``'[cd]' ``. Okay.
81
81
82
82
.. code-block :: pycon
83
83
84
- >>> help(dpath.util. search)
85
- Help on function search in module dpath.util :
84
+ >>> help(dpath.search)
85
+ Help on function search in module dpath:
86
86
87
87
search(obj, glob, yielded=False)
88
88
Given a path glob, return a dictionary containing all keys
@@ -96,7 +96,7 @@ elements in ``x['a']['b']`` where the key is equal to the glob ``'[cd]'``. Okay.
96
96
97
97
.. code-block :: pycon
98
98
99
- >>> result = dpath.util. search(x, "a/b/[cd]")
99
+ >>> result = dpath.search(x, "a/b/[cd]")
100
100
>>> print(json.dumps(result, indent=4, sort_keys=True))
101
101
{
102
102
"a": {
@@ -116,7 +116,7 @@ not get a merged view?
116
116
117
117
.. code-block :: pycon
118
118
119
- >>> for x in dpath.util. search(x, "a/b/[cd]", yielded=True): print(x)
119
+ >>> for x in dpath.search(x, "a/b/[cd]", yielded=True): print(x)
120
120
...
121
121
('a/b/c', [])
122
122
('a/b/d', ['red', 'buggy', 'bumpers'])
@@ -126,16 +126,16 @@ don't care about the paths they were found at:
126
126
127
127
.. code-block :: pycon
128
128
129
- >>> help(dpath.util. values)
130
- Help on function values in module dpath.util :
129
+ >>> help(dpath.values)
130
+ Help on function values in module dpath:
131
131
132
132
values(obj, glob, separator='/', afilter=None, dirs=True)
133
133
Given an object and a path glob, return an array of all values which match
134
134
the glob. The arguments to this function are identical to those of search(),
135
135
and it is primarily a shorthand for a list comprehension over a yielded
136
136
search call.
137
137
138
- >>> dpath.util. values(x, '/a/b/d/*')
138
+ >>> dpath.values(x, '/a/b/d/*')
139
139
['red', 'buggy', 'bumpers']
140
140
141
141
Example: Setting existing keys
@@ -146,14 +146,14 @@ value 'Waffles'.
146
146
147
147
.. code-block :: pycon
148
148
149
- >>> help(dpath.util. set)
150
- Help on function set in module dpath.util :
149
+ >>> help(dpath.set)
150
+ Help on function set in module dpath:
151
151
152
152
set(obj, glob, value)
153
153
Given a path glob, set all existing elements in the document
154
154
to the given value. Returns the number of elements changed.
155
155
156
- >>> dpath.util. set(x, 'a/b/[cd]', 'Waffles')
156
+ >>> dpath.set(x, 'a/b/[cd]', 'Waffles')
157
157
2
158
158
>>> print(json.dumps(x, indent=4, sort_keys=True))
159
159
{
@@ -176,8 +176,8 @@ necessary to get to the terminus.
176
176
177
177
.. code-block :: pycon
178
178
179
- >>> help(dpath.util. new)
180
- Help on function new in module dpath.util :
179
+ >>> help(dpath.new)
180
+ Help on function new in module dpath:
181
181
182
182
new(obj, path, value)
183
183
Set the element at the terminus of path to value, and create
@@ -188,7 +188,7 @@ necessary to get to the terminus.
188
188
characters in it, they will become part of the resulting
189
189
keys
190
190
191
- >>> dpath.util. new(x, 'a/b/e/f/g', "Roffle")
191
+ >>> dpath.new(x, 'a/b/e/f/g', "Roffle")
192
192
>>> print(json.dumps(x, indent=4, sort_keys=True))
193
193
{
194
194
"a": {
@@ -212,8 +212,8 @@ object with None entries in order to make it big enough:
212
212
213
213
.. code-block :: pycon
214
214
215
- >>> dpath.util. new(x, 'a/b/e/f/h', [])
216
- >>> dpath.util. new(x, 'a/b/e/f/h/13', 'Wow this is a big array, it sure is lonely in here by myself')
215
+ >>> dpath.new(x, 'a/b/e/f/h', [])
216
+ >>> dpath.new(x, 'a/b/e/f/h/13', 'Wow this is a big array, it sure is lonely in here by myself')
217
217
>>> print(json.dumps(x, indent=4, sort_keys=True))
218
218
{
219
219
"a": {
@@ -252,11 +252,11 @@ Handy!
252
252
Example: Deleting Existing Keys
253
253
===============================
254
254
255
- To delete keys in an object, use dpath.util. delete, which accepts the same globbing syntax as the other methods.
255
+ To delete keys in an object, use dpath.delete, which accepts the same globbing syntax as the other methods.
256
256
257
257
.. code-block :: pycon
258
258
259
- >>> help(dpath.util. delete)
259
+ >>> help(dpath.delete)
260
260
261
261
delete(obj, glob, separator='/', afilter=None):
262
262
Given a path glob, delete all elements that match the glob.
@@ -267,27 +267,26 @@ To delete keys in an object, use dpath.util.delete, which accepts the same globb
267
267
Example: Merging
268
268
================
269
269
270
- Also, check out dpath.util. merge. The python dict update() method is
270
+ Also, check out dpath.merge. The python dict update() method is
271
271
great and all but doesn't handle merging dictionaries deeply. This one
272
272
does.
273
273
274
274
.. code-block :: pycon
275
275
276
- >>> help(dpath.util. merge)
277
- Help on function merge in module dpath.util :
276
+ >>> help(dpath.merge)
277
+ Help on function merge in module dpath:
278
278
279
279
merge(dst, src, afilter=None, flags=4, _path='')
280
280
Merge source into destination. Like dict.update() but performs
281
281
deep merging.
282
282
283
- flags is an OR'ed combination of MERGE_ADDITIVE, MERGE_REPLACE
284
- MERGE_TYPESAFE.
285
- * MERGE_ADDITIVE : List objects are combined onto one long
283
+ flags is an OR'ed combination of MergeType enum members.
284
+ * ADDITIVE : List objects are combined onto one long
286
285
list (NOT a set). This is the default flag.
287
- * MERGE_REPLACE : Instead of combining list objects, when
286
+ * REPLACE : Instead of combining list objects, when
288
287
2 list objects are at an equal depth of merge, replace
289
288
the destination with the source.
290
- * MERGE_TYPESAFE : When 2 keys at equal levels are of different
289
+ * TYPESAFE : When 2 keys at equal levels are of different
291
290
types, raise a TypeError exception. By default, the source
292
291
replaces the destination in this situation.
293
292
@@ -312,7 +311,7 @@ does.
312
311
"c": "RoffleWaffles"
313
312
}
314
313
}
315
- >>> dpath.util. merge(x, y)
314
+ >>> dpath.merge(x, y)
316
315
>>> print(json.dumps(x, indent=4, sort_keys=True))
317
316
{
318
317
"a": {
@@ -392,7 +391,7 @@ them:
392
391
... return True
393
392
... return False
394
393
...
395
- >>> result = dpath.util. search(x, '**', afilter=afilter)
394
+ >>> result = dpath.search(x, '**', afilter=afilter)
396
395
>>> print(json.dumps(result, indent=4, sort_keys=True))
397
396
{
398
397
"a": {
@@ -431,18 +430,18 @@ Separator got you down? Use lists as paths
431
430
432
431
The default behavior in dpath is to assume that the path given is a string, which must be tokenized by splitting at the separator to yield a distinct set of path components against which dictionary keys can be individually glob tested. However, this presents a problem when you want to use paths that have a separator in their name; the tokenizer cannot properly understand what you mean by '/a/b/c' if it is possible for '/' to exist as a valid character in a key name.
433
432
434
- To get around this, you can sidestep the whole "filesystem path" style, and abandon the separator entirely, by using lists as paths. All of the methods in dpath.util. * support the use of a list instead of a string as a path. So for example:
433
+ To get around this, you can sidestep the whole "filesystem path" style, and abandon the separator entirely, by using lists as paths. All of the methods in dpath.* support the use of a list instead of a string as a path. So for example:
435
434
436
435
.. code-block :: python
437
436
438
437
>> > x = { ' a' : {' b/c' : 0 }}
439
- >> > dpath.util. get([' a' , ' b/c' ])
438
+ >> > dpath.get([' a' , ' b/c' ])
440
439
0
441
440
442
441
dpath.segments : The Low-Level Backend
443
442
======================================
444
443
445
- dpath.util is where you want to spend your time: this library has the friendly
444
+ dpath is where you want to spend your time: this library has the friendly
446
445
functions that will understand simple string globs, afilter functions, etc.
447
446
448
447
dpath.segments is the backend pathing library. It passes around tuples of path
0 commit comments