3232
3333""" Identify specific nodes in a JSON document (RFC 6901) """
3434
35- from __future__ import unicode_literals
36-
3735# Will be parsed by setup.py to determine package metadata
3836__author__ = 'Stefan Kögl <[email protected] >' 39- __version__ = '2.4 '
37+ __version__ = '3.0.0 '
4038__website__ = 'https://github.com/stefankoegl/python-json-pointer'
4139__license__ = 'Modified BSD License'
4240
43-
44- try :
45- from itertools import izip
46- str = unicode
47- encode_str = lambda u : u .encode ("raw_unicode_escape" )
48- except ImportError : # Python 3
49- izip = zip
50- encode_str = lambda u : u
51-
52- try :
53- from collections .abc import Mapping , Sequence
54- except ImportError : # Python 3
55- from collections import Mapping , Sequence
56-
57- from itertools import tee , chain
58- import re
5941import copy
60-
42+ import re
43+ from collections .abc import Mapping , Sequence
44+ from itertools import tee , chain
6145
6246_nothing = object ()
6347
6448
6549def set_pointer (doc , pointer , value , inplace = True ):
66- """Resolves pointer against doc and sets the value of the target within doc.
50+ """Resolves a pointer against doc and sets the value of the target within doc.
6751
6852 With inplace set to true, doc is modified as long as pointer is not the
6953 root.
@@ -145,7 +129,7 @@ def pairwise(iterable):
145129 a , b = tee (iterable )
146130 for _ in b :
147131 break
148- return izip (a , b )
132+ return zip (a , b )
149133
150134
151135class JsonPointerException (Exception ):
@@ -259,12 +243,11 @@ def get_part(cls, doc, part):
259243 else :
260244 raise JsonPointerException ("Document '%s' does not support indexing, "
261245 "must be mapping/sequence or support __getitem__" % type (doc ))
262-
246+
263247 def get_parts (self ):
264248 """Returns the list of the parts. For example, JsonPointer('/a/b').get_parts() == ['a', 'b']"""
265-
266- return self .parts
267249
250+ return self .parts
268251
269252 def walk (self , doc , part ):
270253 """ Walks one step in doc and returns the referenced part """
@@ -281,7 +264,7 @@ def walk(self, doc, part):
281264 return doc [part ]
282265
283266 except IndexError :
284- raise JsonPointerException ("index '%s' is out of bounds" % (part , ))
267+ raise JsonPointerException ("index '%s' is out of bounds" % (part ,))
285268
286269 # Else the object is a mapping or supports __getitem__(so assume custom indexing)
287270 try :
@@ -290,7 +273,6 @@ def walk(self, doc, part):
290273 except KeyError :
291274 raise JsonPointerException ("member '%s' not found in %s" % (part , doc ))
292275
293-
294276 def contains (self , ptr ):
295277 """ Returns True if self contains the given ptr """
296278 return self .parts [:len (ptr .parts )] == ptr .parts
@@ -309,12 +291,11 @@ def join(self, suffix):
309291 suffix_parts = suffix
310292 try :
311293 return JsonPointer .from_parts (chain (self .parts , suffix_parts ))
312- except :
294+ except : # noqa E722
313295 raise JsonPointerException ("Invalid suffix" )
314296
315- def __truediv__ (self , suffix ): # Python 3
297+ def __truediv__ (self , suffix ): # Python 3
316298 return self .join (suffix )
317- __div__ = __truediv__ # Python 2
318299
319300 @property
320301 def path (self ):
@@ -342,10 +323,10 @@ def __hash__(self):
342323 return hash (tuple (self .parts ))
343324
344325 def __str__ (self ):
345- return encode_str ( self .path )
326+ return self .path
346327
347328 def __repr__ (self ):
348- return "JsonPointer (" + repr (self .path ) + ")"
329+ return type ( self ). __name__ + " (" + repr (self .path ) + ")"
349330
350331 @classmethod
351332 def from_parts (cls , parts ):
@@ -362,5 +343,6 @@ def from_parts(cls, parts):
362343def escape (s ):
363344 return s .replace ('~' , '~0' ).replace ('/' , '~1' )
364345
346+
365347def unescape (s ):
366348 return s .replace ('~1' , '/' ).replace ('~0' , '~' )
0 commit comments