summaryrefslogtreecommitdiff
path: root/jsonpointer.py
diff options
context:
space:
mode:
authorBen Kehoe <ben@kehoe.io>2022-02-20 14:10:10 -0700
committerBen Kehoe <ben@kehoe.io>2022-02-20 14:10:10 -0700
commitd811454192adb18fc3a9aa333fc27e4e48ddd723 (patch)
tree8dc471a90a596320a5338638622f6e734f295364 /jsonpointer.py
parent04e4ac28b935f7538fa54d8ffd385549015040b5 (diff)
downloadpython-json-pointer-d811454192adb18fc3a9aa333fc27e4e48ddd723.tar.gz
Add join method and / operator
Diffstat (limited to 'jsonpointer.py')
-rw-r--r--jsonpointer.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/jsonpointer.py b/jsonpointer.py
index 3eaea19..d9fe0c0 100644
--- a/jsonpointer.py
+++ b/jsonpointer.py
@@ -54,7 +54,7 @@ try:
except ImportError: # Python 3
from collections import Mapping, Sequence
-from itertools import tee
+from itertools import tee, chain
import re
import copy
@@ -299,6 +299,23 @@ class JsonPointer(object):
""" Returns True if self contains the given ptr """
return self.contains(item)
+ def join(self, suffix):
+ """ Returns a new JsonPointer with the given suffix append to this ptr """
+ if isinstance(suffix, JsonPointer):
+ suffix_parts = suffix.parts
+ elif isinstance(suffix, str):
+ suffix_parts = JsonPointer(suffix).parts
+ else:
+ suffix_parts = suffix
+ try:
+ return JsonPointer.from_parts(chain(self.parts, suffix_parts))
+ except:
+ raise JsonPointerException("Invalid suffix")
+
+ def __truediv__(self, suffix): # Python 3
+ return self.join(suffix)
+ __div__ = __truediv__ # Python 2
+
@property
def path(self):
"""Returns the string representation of the pointer