diff options
author | Alexander Dutton <alexander.dutton@it.ox.ac.uk> | 2015-02-15 20:00:41 +0000 |
---|---|---|
committer | Alexander Dutton <alexander.dutton@it.ox.ac.uk> | 2015-02-15 20:00:41 +0000 |
commit | 76b542b3fbfb3ef2a1f9fd289e8a74765c5cb53f (patch) | |
tree | e88ad3003923b97c01519d5dc7ead420a8e506d0 /jsonpointer.py | |
parent | 3f460a54c2004802cc96b1c619b5edc011ec555b (diff) | |
download | python-json-pointer-76b542b3fbfb3ef2a1f9fd289e8a74765c5cb53f.tar.gz |
Reduce number of isinstance() calls in get_part
Diffstat (limited to 'jsonpointer.py')
-rw-r--r-- | jsonpointer.py | 21 |
1 files changed, 6 insertions, 15 deletions
diff --git a/jsonpointer.py b/jsonpointer.py index 85afe00..29a71f5 100644 --- a/jsonpointer.py +++ b/jsonpointer.py @@ -196,27 +196,14 @@ class JsonPointer(object): def get_part(self, doc, part): """ Returns the next step in the correct type """ - if isinstance(doc, Mapping): - return part - - elif isinstance(doc, Sequence): - + if isinstance(doc, Sequence): if part == '-': return part - if not RE_ARRAY_INDEX.match(str(part)): raise JsonPointerException("'%s' is not a valid list index" % (part, )) - return int(part) - - elif hasattr(doc, '__getitem__'): - # Allow indexing via ducktyping if the target has defined __getitem__ - return part - else: - raise JsonPointerException("Document '%s' does not support indexing, " - "must be dict/list or support __getitem__" % type(doc)) - + return part def walk(self, doc, part): """ Walks one step in doc and returns the referenced part """ @@ -231,6 +218,10 @@ class JsonPointer(object): raise JsonPointerException("member '%s' not found in %s" % (part, doc)) except IndexError: raise JsonPointerException("index '%s' is out of bounds" % (part, )) + except TypeError: + raise JsonPointerException("Document '%s' does not support indexing, " + "must be dict/list or support __getitem__" % type(doc)) + def contains(self, ptr): """Returns True if self contains the given ptr""" |