Skip to content

Commit 73cb9ef

Browse files
committed
Add method and add classmethod tag
Set get_part as a classmethod, and add method `get_parts` For: #36, #45
1 parent 7d146bd commit 73cb9ef

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

jsonpointer.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def to_last(self, doc):
193193
for part in self.parts[:-1]:
194194
doc = self.walk(doc, part)
195195

196-
return doc, self.get_part(doc, self.parts[-1])
196+
return doc, JsonPointer.get_part(doc, self.parts[-1])
197197

198198
def resolve(self, doc, default=_nothing):
199199
"""Resolves the pointer against doc and returns the referenced object"""
@@ -228,7 +228,8 @@ def set(self, doc, value, inplace=True):
228228
parent[part] = value
229229
return doc
230230

231-
def get_part(self, doc, part):
231+
@classmethod
232+
def get_part(cls, doc, part):
232233
"""Returns the next step in the correct type"""
233234

234235
if isinstance(doc, Mapping):
@@ -239,7 +240,7 @@ def get_part(self, doc, part):
239240
if part == '-':
240241
return part
241242

242-
if not self._RE_ARRAY_INDEX.match(str(part)):
243+
if not JsonPointer._RE_ARRAY_INDEX.match(str(part)):
243244
raise JsonPointerException("'%s' is not a valid sequence index" % part)
244245

245246
return int(part)
@@ -252,12 +253,17 @@ def get_part(self, doc, part):
252253
else:
253254
raise JsonPointerException("Document '%s' does not support indexing, "
254255
"must be mapping/sequence or support __getitem__" % type(doc))
256+
257+
def get_parts(self):
258+
"""Returns the list of the parts. For example, JsonPointer('/a/b').get_parts() == ['a', 'b']"""
259+
260+
return self.parts
255261

256262

257263
def walk(self, doc, part):
258264
""" Walks one step in doc and returns the referenced part """
259265

260-
part = self.get_part(doc, part)
266+
part = JsonPointer.get_part(doc, part)
261267

262268
assert hasattr(doc, '__getitem__'), "invalid document type %s" % (type(doc),)
263269

0 commit comments

Comments
 (0)