Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make path instances hashable #128

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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