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

Improve performance of Baseclass.__eq__ and Baseclass.__hash__ functions #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion python_wrapper/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='twa',
version='0.0.7',
version='0.0.8-15-SNAPSHOT',
author='Jiaru Bai; Daniel Nurkowski',
author_email='jb2197@cam.ac.uk; danieln@cmclinnovations.com',
license='MIT',
Expand Down
2 changes: 1 addition & 1 deletion python_wrapper/twa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from twa.JPSGateway import JPSGateway

__version__ = "0.0.7"
__version__ = "0.0.8-15-SNAPSHOT"
11 changes: 9 additions & 2 deletions python_wrapper/twa/data_model/base_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,11 +1529,18 @@ def _exclude_keys_for_compare_(self, *keys_to_exclude):
return set(tuple(list_keys_to_exclude))

def __eq__(self, other: Any) -> bool:
return self.__hash__() == other.__hash__()
try:
return self.instance_iri == other.instance_iri
except TypeError:
# if other doesn't have instance_iri, then it's not comparable
return False

__hash_cache__ = None
def __hash__(self):
# using instance_iri for hash so that iri and object itself are treated the same in set operations
return self.instance_iri.__hash__()
if self.__hash_cache__ is None:
self.__hash_cache__ = self.instance_iri.__hash__()
return self.__hash_cache__
# TODO [future] do we want to provide the method to compare if the content of two instances are the same?
# a use case would be to compare if the chemicals in the two bottles are the same concentration
# return self._make_hash_sha256_(self.dict(exclude=self._exclude_keys_for_compare_()))
Expand Down
Loading