From 5ccca5a68c27c77fc292fbd9b3f18447f76504d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20Courtney?= Date: Thu, 6 Mar 2025 16:22:58 +0000 Subject: [PATCH 1/3] 15-improve-performance-of-baseclass__eq__-and-baseclass__hash__-functions: bump version --- python_wrapper/setup.py | 2 +- python_wrapper/twa/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python_wrapper/setup.py b/python_wrapper/setup.py index 21de3ab89..563bd8833 100644 --- a/python_wrapper/setup.py +++ b/python_wrapper/setup.py @@ -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', diff --git a/python_wrapper/twa/__init__.py b/python_wrapper/twa/__init__.py index 549b8a8b0..237c52d13 100644 --- a/python_wrapper/twa/__init__.py +++ b/python_wrapper/twa/__init__.py @@ -1,3 +1,3 @@ from twa.JPSGateway import JPSGateway -__version__ = "0.0.7" +__version__ = "0.0.8-15-SNAPSHOT" From 98ba412723ad8b18a362736f635b548d683a7e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20Courtney?= Date: Thu, 6 Mar 2025 16:24:10 +0000 Subject: [PATCH 2/3] 15-improve-performance-of-baseclass__eq__-and-baseclass__hash__-functions: string compare is faster than hashing first --- python_wrapper/twa/data_model/base_ontology.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python_wrapper/twa/data_model/base_ontology.py b/python_wrapper/twa/data_model/base_ontology.py index d80e5b486..3f84bad4c 100644 --- a/python_wrapper/twa/data_model/base_ontology.py +++ b/python_wrapper/twa/data_model/base_ontology.py @@ -1529,7 +1529,11 @@ 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 def __hash__(self): # using instance_iri for hash so that iri and object itself are treated the same in set operations From 186e259df184de94a49971464a83564b3c3faf6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20Courtney?= Date: Thu, 6 Mar 2025 16:25:27 +0000 Subject: [PATCH 3/3] 15-improve-performance-of-baseclass__eq__-and-baseclass__hash__-functions: cache the hash calculation --- python_wrapper/twa/data_model/base_ontology.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python_wrapper/twa/data_model/base_ontology.py b/python_wrapper/twa/data_model/base_ontology.py index 3f84bad4c..b2897fc96 100644 --- a/python_wrapper/twa/data_model/base_ontology.py +++ b/python_wrapper/twa/data_model/base_ontology.py @@ -1535,9 +1535,12 @@ def __eq__(self, other: Any) -> bool: # 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_()))