62
62
63
63
64
64
def set_pointer (doc , pointer , value , inplace = True ):
65
- """
66
- Resolves pointer against doc and sets the value of the target within doc.
65
+ """Resolves pointer against doc and sets the value of the target within doc.
67
66
68
67
With inplace set to true, doc is modified as long as pointer is not the
69
68
root.
@@ -77,16 +76,14 @@ def set_pointer(doc, pointer, value, inplace=True):
77
76
>>> set_pointer(obj, '/foo/yet%20another%20prop', 'added prop') == \
78
77
{'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}}
79
78
True
80
-
81
79
"""
82
80
83
81
pointer = JsonPointer (pointer )
84
82
return pointer .set (doc , value , inplace )
85
83
86
84
87
85
def resolve_pointer (doc , pointer , default = _nothing ):
88
- """
89
- Resolves pointer against doc and returns the referenced object
86
+ """Resolves pointer against doc and returns the referenced object
90
87
91
88
>>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}}
92
89
@@ -107,15 +104,15 @@ def resolve_pointer(doc, pointer, default=_nothing):
107
104
108
105
>>> resolve_pointer(obj, '/some/path', None) == None
109
106
True
110
-
111
107
"""
112
108
113
109
pointer = JsonPointer (pointer )
114
110
return pointer .resolve (doc , default )
115
111
116
112
117
113
def pairwise (iterable ):
118
- """
114
+ """ Transforms a list to a list of tuples of adjacent items
115
+
119
116
s -> (s0,s1), (s1,s2), (s2, s3), ...
120
117
121
118
>>> list(pairwise([]))
@@ -138,9 +135,7 @@ class JsonPointerException(Exception):
138
135
139
136
140
137
class EndOfList (object ):
141
- """
142
- Result of accessing element "-" of a list
143
- """
138
+ """Result of accessing element "-" of a list"""
144
139
145
140
def __init__ (self , list_ ):
146
141
self .list_ = list_
@@ -151,9 +146,8 @@ def __repr__(self):
151
146
152
147
153
148
class JsonPointer (object ):
154
- """
155
- A JSON Pointer that can reference parts of an JSON document
156
- """
149
+ """A JSON Pointer that can reference parts of an JSON document"""
150
+
157
151
_GETITEM_SUPPORT_ERROR = """document '%s' does not support indexing,
158
152
must be mapping/sequence
159
153
or support __getitem__"""
@@ -173,9 +167,7 @@ def __init__(self, pointer):
173
167
self .parts = parts
174
168
175
169
def to_last (self , doc ):
176
- """
177
- Resolves ptr until the last step, returns (sub-doc, last-step)
178
- """
170
+ """Resolves ptr until the last step, returns (sub-doc, last-step)"""
179
171
180
172
if not self .parts :
181
173
return doc , None
@@ -186,9 +178,7 @@ def to_last(self, doc):
186
178
return doc , self .get_part (doc , self .parts [- 1 ])
187
179
188
180
def resolve (self , doc , default = _nothing ):
189
- """
190
- Resolves the pointer against doc and returns the referenced object
191
- """
181
+ """Resolves the pointer against doc and returns the referenced object"""
192
182
193
183
for part in self .parts :
194
184
@@ -205,9 +195,7 @@ def resolve(self, doc, default=_nothing):
205
195
get = resolve
206
196
207
197
def set (self , doc , value , inplace = True ):
208
- """
209
- Resolve the pointer against the doc and replace the target with value.
210
- """
198
+ """Resolve the pointer against the doc and replace the target with value."""
211
199
212
200
if len (self .parts ) == 0 :
213
201
if inplace :
@@ -223,9 +211,7 @@ def set(self, doc, value, inplace=True):
223
211
return doc
224
212
225
213
def get_part (self , doc , part ):
226
- """
227
- Returns the next step in the correct type
228
- """
214
+ """Returns the next step in the correct type"""
229
215
230
216
if isinstance (doc , Mapping ):
231
217
return part
@@ -249,9 +235,7 @@ def get_part(self, doc, part):
249
235
raise JsonPointerException (self ._GETITEM_SUPPORT_ERROR % type (doc ))
250
236
251
237
def walk (self , doc , part ):
252
- """
253
- Walks one step in doc and returns the referenced part
254
- """
238
+ """Walks one step in doc and returns the referenced part"""
255
239
256
240
part = self .get_part (doc , part )
257
241
@@ -281,16 +265,14 @@ def walk(self, doc, part):
281
265
return doc [part ]
282
266
283
267
def contains (self , ptr ):
284
- """
285
- Returns True if self contains the given ptr
286
- """
287
- return len (self .parts ) >= len (ptr .parts ) and \
268
+ """Returns True if self contains the given ptr"""
269
+
270
+ return len (self .parts ) > len (ptr .parts ) and \
288
271
self .parts [:len (ptr .parts )] == ptr .parts
289
272
290
273
@property
291
274
def path (self ):
292
- """
293
- Returns the string representation of the pointer
275
+ """Returns the string representation of the pointer
294
276
295
277
>>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
296
278
"""
@@ -299,8 +281,7 @@ def path(self):
299
281
return '' .join ('/' + part for part in parts )
300
282
301
283
def __eq__ (self , other ):
302
- """
303
- Compares a pointer to another object
284
+ """Compares a pointer to another object
304
285
305
286
Pointers can be compared by comparing their strings (or splitted
306
287
strings), because no two different parts can point to the same
@@ -317,8 +298,7 @@ def __hash__(self):
317
298
318
299
@classmethod
319
300
def from_parts (cls , parts ):
320
- """
321
- Constructs a JsonPointer from a list of (unescaped) paths
301
+ """Constructs a JsonPointer from a list of (unescaped) paths
322
302
323
303
>>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0'
324
304
True
0 commit comments