6262
6363
6464def 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.
6766
6867 With inplace set to true, doc is modified as long as pointer is not the
6968 root.
@@ -77,16 +76,14 @@ def set_pointer(doc, pointer, value, inplace=True):
7776 >>> set_pointer(obj, '/foo/yet%20another%20prop', 'added prop') == \
7877 {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}}
7978 True
80-
8179 """
8280
8381 pointer = JsonPointer (pointer )
8482 return pointer .set (doc , value , inplace )
8583
8684
8785def 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
9087
9188 >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}}
9289
@@ -107,15 +104,15 @@ def resolve_pointer(doc, pointer, default=_nothing):
107104
108105 >>> resolve_pointer(obj, '/some/path', None) == None
109106 True
110-
111107 """
112108
113109 pointer = JsonPointer (pointer )
114110 return pointer .resolve (doc , default )
115111
116112
117113def pairwise (iterable ):
118- """
114+ """ Transforms a list to a list of tuples of adjacent items
115+
119116 s -> (s0,s1), (s1,s2), (s2, s3), ...
120117
121118 >>> list(pairwise([]))
@@ -138,9 +135,7 @@ class JsonPointerException(Exception):
138135
139136
140137class EndOfList (object ):
141- """
142- Result of accessing element "-" of a list
143- """
138+ """Result of accessing element "-" of a list"""
144139
145140 def __init__ (self , list_ ):
146141 self .list_ = list_
@@ -151,9 +146,8 @@ def __repr__(self):
151146
152147
153148class 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+
157151 _GETITEM_SUPPORT_ERROR = """document '%s' does not support indexing,
158152 must be mapping/sequence
159153 or support __getitem__"""
@@ -173,9 +167,7 @@ def __init__(self, pointer):
173167 self .parts = parts
174168
175169 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)"""
179171
180172 if not self .parts :
181173 return doc , None
@@ -186,9 +178,7 @@ def to_last(self, doc):
186178 return doc , self .get_part (doc , self .parts [- 1 ])
187179
188180 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"""
192182
193183 for part in self .parts :
194184
@@ -205,9 +195,7 @@ def resolve(self, doc, default=_nothing):
205195 get = resolve
206196
207197 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."""
211199
212200 if len (self .parts ) == 0 :
213201 if inplace :
@@ -223,9 +211,7 @@ def set(self, doc, value, inplace=True):
223211 return doc
224212
225213 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"""
229215
230216 if isinstance (doc , Mapping ):
231217 return part
@@ -249,9 +235,7 @@ def get_part(self, doc, part):
249235 raise JsonPointerException (self ._GETITEM_SUPPORT_ERROR % type (doc ))
250236
251237 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"""
255239
256240 part = self .get_part (doc , part )
257241
@@ -281,16 +265,14 @@ def walk(self, doc, part):
281265 return doc [part ]
282266
283267 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 \
288271 self .parts [:len (ptr .parts )] == ptr .parts
289272
290273 @property
291274 def path (self ):
292- """
293- Returns the string representation of the pointer
275+ """Returns the string representation of the pointer
294276
295277 >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
296278 """
@@ -299,8 +281,7 @@ def path(self):
299281 return '' .join ('/' + part for part in parts )
300282
301283 def __eq__ (self , other ):
302- """
303- Compares a pointer to another object
284+ """Compares a pointer to another object
304285
305286 Pointers can be compared by comparing their strings (or splitted
306287 strings), because no two different parts can point to the same
@@ -317,8 +298,7 @@ def __hash__(self):
317298
318299 @classmethod
319300 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
322302
323303 >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0'
324304 True
0 commit comments