Skip to content
Snippets Groups Projects
Commit 2b293727 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

improve OmniHashableMixin

parent ddd42940
No related branches found
No related tags found
No related merge requests found
import operator
import typing as tp import typing as tp
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
...@@ -180,40 +181,27 @@ class OmniHashableMixin(metaclass=ABCMeta): ...@@ -180,40 +181,27 @@ class OmniHashableMixin(metaclass=ABCMeta):
""" """
Note that this will only compare _HASH_FIELDS_TO_USE Note that this will only compare _HASH_FIELDS_TO_USE
""" """
if not isinstance(other, type(self)): return _generic_eq(self, other, False, operator.eq, 'eq', )
return False
if not isinstance(other, OmniHashableMixin):
return super().__eq__(other)
cmpr_by = self._HASH_FIELDS_TO_USE
try:
if isinstance(cmpr_by, str):
return getattr(self, cmpr_by) == getattr(other, cmpr_by)
for field_name in self._HASH_FIELDS_TO_USE:
if getattr(self, field_name) != getattr(other, field_name):
return False
return True
except AttributeError:
return False
def __ne__(self, other) -> bool: def __ne__(self, other) -> bool:
if not isinstance(other, type(self)): return _generic_eq(self, other, True, operator.ne, 'ne')
return True
if not isinstance(other, OmniHashableMixin):
return super().__ne__(other)
cmpr_by = self._HASH_FIELDS_TO_USE def _generic_eq(self, other, truth, comparator, name):
if not isinstance(other, type(self)):
return truth
try: if not isinstance(other, OmniHashableMixin):
if isinstance(cmpr_by, str): return getattr(super(), name)(other)
return getattr(self, cmpr_by) != getattr(other, cmpr_by)
for field_name in cmpr_by: cmpr_by = self._HASH_FIELDS_TO_USE
if getattr(self, field_name) != getattr(other, field_name): try:
return True if isinstance(cmpr_by, str):
return False return comparator(getattr(self, cmpr_by), getattr(other, cmpr_by))
except AttributeError:
return True for field_name in self._HASH_FIELDS_TO_USE:
if getattr(self, field_name) != getattr(other, field_name):
return truth
return not truth
except AttributeError:
return truth
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment