Skip to content

Commit

Permalink
Merge pull request #128 from michaelmior/hashable
Browse files Browse the repository at this point in the history
Make path instances hashable
  • Loading branch information
michaelmior committed Jul 18, 2023
2 parents 8c1e1f5 + 089d7ee commit 39931f7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions jsonpath_ng/jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ def __repr__(self):
def __eq__(self, other):
return isinstance(other, Root)

def __hash__(self):
return hash('$')


class This(JSONPath):
"""
Expand All @@ -244,6 +247,9 @@ def __repr__(self):
def __eq__(self, other):
return isinstance(other, This)

def __hash__(self):
return hash('this')


class Child(JSONPath):
"""
Expand Down Expand Up @@ -302,6 +308,9 @@ def __str__(self):
def __repr__(self):
return '%s(%r, %r)' % (self.__class__.__name__, self.left, self.right)

def __hash__(self):
return hash((self.left, self.right))


class Parent(JSONPath):
"""
Expand All @@ -323,6 +332,9 @@ def __str__(self):
def __repr__(self):
return 'Parent()'

def __hash__(self):
return hash('parent')


class Where(JSONPath):
"""
Expand Down Expand Up @@ -357,6 +369,9 @@ def __str__(self):
def __eq__(self, other):
return isinstance(other, Where) and other.left == self.left and other.right == self.right

def __hash__(self):
return hash((self.left, self.right))

class Descendants(JSONPath):
"""
JSONPath that matches first the left expression then any descendant
Expand Down Expand Up @@ -469,6 +484,9 @@ def __eq__(self, other):
def __repr__(self):
return '%s(%r, %r)' % (self.__class__.__name__, self.left, self.right)

def __hash__(self):
return hash((self.left, self.right))


class Union(JSONPath):
"""
Expand All @@ -490,6 +508,12 @@ def is_singular(self):
def find(self, data):
return self.left.find(data) + self.right.find(data)

def __eq__(self, other):
return isinstance(other, Union) and self.left == other.left and self.right == other.right

def __hash__(self):
return hash((self.left, self.right))

class Intersect(JSONPath):
"""
JSONPath for bits that match *both* patterns.
Expand All @@ -511,6 +535,12 @@ def is_singular(self):
def find(self, data):
raise NotImplementedError()

def __eq__(self, other):
return isinstance(other, Intersect) and self.left == other.left and self.right == other.right

def __hash__(self):
return hash((self.left, self.right))


class Fields(JSONPath):
"""
Expand Down Expand Up @@ -596,6 +626,9 @@ def __repr__(self):
def __eq__(self, other):
return isinstance(other, Fields) and tuple(self.fields) == tuple(other.fields)

def __hash__(self):
return hash(tuple(self.fields))


class Index(JSONPath):
"""
Expand Down Expand Up @@ -662,6 +695,9 @@ def _pad_value(self, value):
pad = self.index - len(value) + 1
value += [{} for __ in range(pad)]

def __hash__(self):
return hash(self.index)


class Slice(JSONPath):
"""
Expand Down Expand Up @@ -741,6 +777,9 @@ def __repr__(self):
def __eq__(self, other):
return isinstance(other, Slice) and other.start == self.start and self.end == other.end and other.step == self.step

def __hash__(self):
return hash((self.start, self.end, self.step))


def _create_list_key(dict_):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def check_paths(self, test_cases):

for string, data, target in test_cases:
print('parse("%s").find(%s).paths =?= %s' % (string, data, target))
assert hash(parse(string)) == hash(parse(string))
result = parse(string).find(data)
if isinstance(target, list):
assert [str(r.full_path) for r in result] == target
Expand Down

0 comments on commit 39931f7

Please sign in to comment.