Skip to content

Commit

Permalink
create generic equivalence check
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBodor committed Dec 11, 2023
1 parent 8103ccb commit df0620e
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions eitprocessing/mixins/equality.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations
from abc import ABC
from abc import abstractmethod
from dataclasses import astuple
from dataclasses import is_dataclass
import numpy as np
from typing_extensions import Self


class Equivalence(ABC):
Expand All @@ -28,3 +30,60 @@ def _array_safe_eq(a, b) -> bool:
return object.__eq__(a, b) # `a == b` could trigger an infinite loop
except TypeError:
return NotImplemented

@abstractmethod
def isequivalent(
self,
other: Self,
raise_: bool = False,
checks: dict[str, bool] = None,
) -> bool:
"""Test whether the data structure between two objects are equivalent.
Equivalence in this case means that objects are equal in all respects,
except for data content. Generally data loaded from the same source with
identical preprocessing will be equivalent.
Args:
other: object that will be compared to self.
raise_:
if False (default): return `False` if not equivalent;
if `True`: raise `EquivalenceError` if not equivalence.
checks: Dictionary of bools that will be checked. This dictionary can be
defined in each child class individually.
Defaults to None, meaning that only `type`s are compared.
Raises:
EquivalenceError: if `raise_ == True` and the objects are not equivalent.
Returns:
bool describing result of equivalence comparison.
"""
# TODO: find out what correct way is to send extra argument to parent class without pissing off the linter
if not checks:
checks = {}

if self == other:
return True
try:
if type(self) is not type(other):
raise EquivalenceError(
f"Classes don't match: {type(self)}, {type(other)}"
)
for msg, check in checks.items():
if not check:
raise EquivalenceError(msg)
except EquivalenceError as e:
if raise_:
raise e
return False
return True


class EquivalenceError(TypeError, ValueError):
"""Raised if objects are not equivalent.
Equivalence in this case means that objects are equal in all respects,
except for data content. Generally data loaded from the same source with
identical preprocessing will be equivalent.
"""

0 comments on commit df0620e

Please sign in to comment.