Skip to content

Commit 3f460a5

Browse files
committed
Simplify walk method. No need to look before we leap.
1 parent af04ec0 commit 3f460a5

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

jsonpointer.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,29 +223,14 @@ def walk(self, doc, part):
223223

224224
part = self.get_part(doc, part)
225225

226-
assert (type(doc) in (dict, list) or hasattr(doc, '__getitem__')), "invalid document type %s" % (type(doc),)
227-
228-
if isinstance(doc, Mapping):
229-
try:
230-
return doc[part]
231-
232-
except KeyError:
233-
raise JsonPointerException("member '%s' not found in %s" % (part, doc))
234-
235-
elif isinstance(doc, Sequence):
236-
237-
if part == '-':
238-
return EndOfList(doc)
239-
240-
try:
241-
return doc[part]
242-
243-
except IndexError:
244-
raise JsonPointerException("index '%s' is out of bounds" % (part, ))
245-
246-
else:
247-
# Object supports __getitem__, assume custom indexing
226+
if part == '-' and isinstance(doc, Sequence):
227+
return EndOfList(doc)
228+
try:
248229
return doc[part]
230+
except KeyError:
231+
raise JsonPointerException("member '%s' not found in %s" % (part, doc))
232+
except IndexError:
233+
raise JsonPointerException("index '%s' is out of bounds" % (part, ))
249234

250235
def contains(self, ptr):
251236
"""Returns True if self contains the given ptr"""

0 commit comments

Comments
 (0)