Skip to content

Equality

coola.equality.comparators

Contain the comparators to check if two objects are equal or not.

coola.equality.comparators.BaseEqualityComparator

Bases: ABC, Generic[T]

Define the base class to implement an equality operator.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import DefaultEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = DefaultEqualityComparator()
>>> comparator.equal(42, 42, config)
True
>>> comparator.equal("meow", "meov", config)
False

coola.equality.comparators.BaseEqualityComparator.clone abstractmethod

Return a copy of the equality operator.

Returns:

Type Description
BaseEqualityComparator

A copy of the equality operator.

Example usage:

>>> from coola.equality.comparators import DefaultEqualityComparator
>>> op = DefaultEqualityComparator()
>>> op_cloned = op.clone()
>>> op_cloned
DefaultEqualityComparator()
>>> op is op_cloned
False

coola.equality.comparators.BaseEqualityComparator.equal abstractmethod

equal(
    actual: T, expected: Any, config: EqualityConfig
) -> bool

Indicate if two objects are equal or not.

Parameters:

Name Type Description Default
actual T

The actual input.

required
expected Any

The expected input.

required
config EqualityConfig

The equality configuration.

required

Returns:

Type Description
bool

True if the two objects are equal, otherwise False.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import DefaultEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = DefaultEqualityComparator()
>>> comparator.equal(42, 42, config)
True
>>> comparator.equal("meow", "meov", config)
False

coola.equality.comparators.DefaultEqualityComparator

Bases: BaseEqualityComparator[Any]

Implement a default equality comparator.

The == operator is used to test the equality between the objects.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import DefaultEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = DefaultEqualityComparator()
>>> comparator.equal(42, 42, config)
True
>>> comparator.equal("meow", "meov", config)
False

coola.equality.comparators.JaxArrayEqualityComparator

Bases: BaseEqualityComparator[ndarray]

Implement an equality comparator for jax.numpy.ndarray.

Example usage:

>>> import jax.numpy as jnp
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import JaxArrayEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = JaxArrayEqualityComparator()
>>> comparator.equal(jnp.ones((2, 3)), jnp.ones((2, 3)), config)
True
>>> comparator.equal(jnp.ones((2, 3)), jnp.zeros((2, 3)), config)
False

coola.equality.comparators.MappingEqualityComparator

Bases: BaseEqualityComparator[Mapping]

Implement a sequence equality comparator.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import MappingEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = MappingEqualityComparator()
>>> comparator.equal({"a": 1, "b": 2}, {"a": 1, "b": 2}, config)
True
>>> comparator.equal({"a": 1, "b": 2}, {"a": 1, "b": 4}, config)
False

coola.equality.comparators.NumpyArrayEqualityComparator

Bases: BaseEqualityComparator[ndarray]

Implement an equality comparator for numpy.ndarray.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import NumpyArrayEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = NumpyArrayEqualityComparator()
>>> comparator.equal(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> comparator.equal(np.ones((2, 3)), np.zeros((2, 3)), config)
False

coola.equality.comparators.NumpyMaskedArrayEqualityComparator

Bases: BaseEqualityComparator[MaskedArray]

Implement an equality comparator for numpy.ndarray.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import NumpyMaskedArrayEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = NumpyMaskedArrayEqualityComparator()
>>> comparator.equal(
...     np.ma.array(data=[0.0, 1.0, 1.2], mask=[0, 1, 0]),
...     np.ma.array(data=[0.0, 1.0, 1.2], mask=[0, 1, 0]),
...     config,
... )
True
>>> comparator.equal(
...     np.ma.array(data=[0.0, 1.0, 1.2], mask=[0, 1, 0]),
...     np.ma.array(data=[0.0, 1.0, 2.0], mask=[0, 1, 0]),
...     config,
... )
False

coola.equality.comparators.PandasDataFrameEqualityComparator

Bases: BaseEqualityComparator[DataFrame]

Implement an equality comparator for pandas.DataFrame.

Example usage:

>>> import pandas as pd
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import PandasDataFrameEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = PandasDataFrameEqualityComparator()
>>> comparator.equal(
...     pd.DataFrame({"col": [1, 2, 3]}),
...     pd.DataFrame({"col": [1, 2, 3]}),
...     config,
... )
True
>>> comparator.equal(
...     pd.DataFrame({"col": [1, 2, 3]}),
...     pd.DataFrame({"col": [1, 2, 4]}),
...     config,
... )
False

coola.equality.comparators.PandasSeriesEqualityComparator

Bases: BaseEqualityComparator[Series]

Implement an equality comparator for pandas.Series.

Example usage:

>>> import pandas as pd
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import PandasSeriesEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = PandasSeriesEqualityComparator()
>>> comparator.equal(pd.Series([1, 2, 3]), pd.Series([1, 2, 3]), config)
True
>>> comparator.equal(pd.Series([1, 2, 3]), pd.Series([1, 2, 4]), config)
False

coola.equality.comparators.PolarsDataFrameEqualityComparator

Bases: BaseEqualityComparator[DataFrame]

Implement an equality comparator for polars.DataFrame.

Example usage:

>>> import polars as pl
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import PolarsDataFrameEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = PolarsDataFrameEqualityComparator()
>>> comparator.equal(
...     pl.DataFrame({"col": [1, 2, 3]}),
...     pl.DataFrame({"col": [1, 2, 3]}),
...     config,
... )
True
>>> comparator.equal(
...     pl.DataFrame({"col": [1, 2, 3]}),
...     pl.DataFrame({"col": [1, 2, 4]}),
...     config,
... )
False

coola.equality.comparators.PolarsSeriesEqualityComparator

Bases: BaseEqualityComparator[Series]

Implement an equality comparator for polars.Series.

Example usage:

>>> import polars as pl
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import PolarsSeriesEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = PolarsSeriesEqualityComparator()
>>> comparator.equal(pl.Series([1, 2, 3]), pl.Series([1, 2, 3]), config)
True
>>> comparator.equal(pl.Series([1, 2, 3]), pl.Series([1, 2, 4]), config)
False

coola.equality.comparators.PyarrowEqualityComparator

Bases: BaseEqualityComparator[Array]

Implement an equality comparator for `pyarrow.Arrays and pyarrow.Tables.

Note that config.equal_nan, config.atol and config.rtol arguments are ignored.

Example usage:

>>> import pyarrow as pa
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import PyarrowEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = PyarrowEqualityComparator()
>>> comparator.equal(pa.array([1, 2, 3]), pa.array([1, 2, 3]), config)
True
>>> comparator.equal(pa.array([1, 2, 3]), pa.array([1, 2, 4]), config)
False

coola.equality.comparators.ScalarEqualityComparator

Bases: BaseEqualityComparator[Any]

Implement a default equality comparator.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import ScalarEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = ScalarEqualityComparator()
>>> comparator.equal(42.0, 42.0, config)
True
>>> comparator.equal(42.0, 1.0, config)
False

coola.equality.comparators.SequenceEqualityComparator

Bases: BaseEqualityComparator[Sequence]

Implement a sequence equality comparator.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import SequenceEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = SequenceEqualityComparator()
>>> comparator.equal([1, 2, 3], [1, 2, 3], config)
True
>>> comparator.equal([1, 2, 3], [1, 2, 4], config)
False

coola.equality.comparators.TorchPackedSequenceEqualityComparator

Bases: BaseEqualityComparator[PackedSequence]

Implement an equality comparator for torch.Tensor.

Example usage:

>>> import torch
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import TorchPackedSequenceEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = TorchTensorEqualityComparator()
>>> comparator.equal(torch.ones(2, 3), torch.ones(2, 3), config)
True
>>> comparator.equal(torch.ones(2, 3), torch.zeros(2, 3), config)
False

coola.equality.comparators.TorchTensorEqualityComparator

Bases: BaseEqualityComparator[Tensor]

Implement an equality comparator for torch.Tensor.

Example usage:

>>> import torch
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import TorchTensorEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = TorchTensorEqualityComparator()
>>> comparator.equal(torch.ones(2, 3), torch.ones(2, 3), config)
True
>>> comparator.equal(torch.ones(2, 3), torch.zeros(2, 3), config)
False

coola.equality.comparators.XarrayDataArrayEqualityComparator

Bases: BaseEqualityComparator[DataArray]

Implement an equality comparator for xarray.DataArray.

Example usage:

>>> import numpy as np
>>> import xarray as xr
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import XarrayDataArrayEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = XarrayDataArrayEqualityComparator()
>>> comparator.equal(
...     xr.DataArray(np.arange(6), dims=["z"]),
...     xr.DataArray(np.arange(6), dims=["z"]),
...     config,
... )
True
>>> comparator.equal(
...     xr.DataArray(np.ones(6), dims=["z"]),
...     xr.DataArray(np.zeros(6), dims=["z"]),
...     config,
... )
False

coola.equality.comparators.XarrayDatasetEqualityComparator

Bases: BaseEqualityComparator[Dataset]

Implement an equality comparator for xarray.Dataset.

Example usage:

>>> import numpy as np
>>> import xarray as xr
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import XarrayDatasetEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = XarrayDatasetEqualityComparator()
>>> comparator.equal(
...     xr.Dataset({"x": xr.DataArray(np.arange(6), dims=["z"])}),
...     xr.Dataset({"x": xr.DataArray(np.arange(6), dims=["z"])}),
...     config,
... )
True
>>> comparator.equal(
...     xr.Dataset({"x": xr.DataArray(np.zeros(6), dims=["z"])}),
...     xr.Dataset({"x": xr.DataArray(np.ones(6), dims=["z"])}),
...     config,
... )
False

coola.equality.comparators.XarrayVariableEqualityComparator

Bases: BaseEqualityComparator[Variable]

Implement an equality comparator for xarray.Variable.

Example usage:

>>> import numpy as np
>>> import xarray as xr
>>> from coola.equality import EqualityConfig
>>> from coola.equality.comparators import XarrayVariableEqualityComparator
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> comparator = XarrayVariableEqualityComparator()
>>> comparator.equal(
...     xr.Variable(dims=["z"], data=np.arange(6)),
...     xr.Variable(dims=["z"], data=np.arange(6)),
...     config,
... )
True
>>> comparator.equal(
...     xr.Variable(dims=["z"], data=np.zeros(6)),
...     xr.Variable(dims=["z"], data=np.ones(6)),
...     config,
... )
False

coola.equality.comparators.get_type_comparator_mapping

get_type_comparator_mapping() -> (
    dict[type, BaseEqualityComparator]
)

Get a default mapping between the types and the equality comparators.

Returns:

Type Description
dict[type, BaseEqualityComparator]

The mapping between the types and the equality comparators.

>>> from coola.equality.comparators import get_type_comparator_mapping
>>> get_type_comparator_mapping()
{<class 'object'>: DefaultEqualityComparator(),
 <class 'collections.abc.Mapping'>: MappingEqualityComparator(),
 <class 'collections.abc.Sequence'>: SequenceEqualityComparator(), ...}

coola.equality.handlers

Contain the handlers to help to check if two objects are equal or not.

The handlers are designed to work with Chain of Responsibility pattern.

coola.equality.handlers.AbstractEqualityHandler

Bases: BaseEqualityHandler

Implement a base class with the default chaining behavior.

A child class needs to implement the following method: handle.

coola.equality.handlers.AbstractEqualityHandler.next_handler property

next_handler: BaseEqualityHandler | None

The next handler.

coola.equality.handlers.BaseEqualityHandler

Bases: ABC

Define the base class to implement an equality handler.

A child class needs to implement the following methods:

  • handle
  • set_next_handler

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameObjectHandler, FalseHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameObjectHandler()
>>> handler.set_next_handler(FalseHandler())
>>> handler.handle("abc", "abc", config)
True
>>> handler.handle("abc", "ABC", config)
False

coola.equality.handlers.BaseEqualityHandler.chain

Chain a handler to the current handler.

Parameters:

Name Type Description Default
handler BaseEqualityHandler

The handler to chain.

required

Returns:

Type Description
BaseEqualityHandler

The input handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import (
...     SameObjectHandler,
...     SameTypeHandler,
...     ObjectEqualHandler,
... )
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameObjectHandler()
>>> handler.chain(SameTypeHandler()).chain(ObjectEqualHandler())
>>> handler.handle([1, 2, 3], [1, 2, 3], config)
True

coola.equality.handlers.BaseEqualityHandler.handle abstractmethod

handle(
    actual: Any, expected: Any, config: EqualityConfig
) -> bool

Return the equality result between the two input objects.

Parameters:

Name Type Description Default
actual Any

The actual input.

required
expected Any

The expected input.

required
config EqualityConfig

The equality configuration.

required

Returns:

Type Description
bool

True if the input objects are equal, and False otherwise.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameObjectHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameObjectHandler()
>>> handler.handle("abc", "abc", config)
True

coola.equality.handlers.BaseEqualityHandler.set_next_handler abstractmethod

set_next_handler(handler: BaseEqualityHandler) -> None

Set the next handler.

Parameters:

Name Type Description Default
handler BaseEqualityHandler

The next handler.

required

Example usage:

>>> from coola.equality.handlers import SameObjectHandler, TrueHandler
>>> handler = SameObjectHandler()
>>> handler.set_next_handler(TrueHandler())

coola.equality.handlers.EqualHandler

Bases: BaseEqualityHandler

Check if the two objects have the same data.

This handler returns False if the two objects are different data, otherwise it returns True. The first object must have a equal attribute which indicates if the two objects are equal or not.

Example usage:

>>> import math
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import EqualHandler
>>> from coola.equality.testers import EqualityTester
>>> class MyFloat:
...     def __init__(self, value: float) -> None:
...         self._value = value
...     def equal(self, other: float) -> bool:
...         return self._value == other
...
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = EqualHandler()
>>> handler.handle(MyFloat(42), 42, config)
True
>>> handler.handle(MyFloat(42), float("nan"), config)
False

coola.equality.handlers.EqualNanHandler

Bases: BaseEqualityHandler

Check if the two objects have the same data.

This handler returns False if the two objects are different data, otherwise it returns True. The first object must have a equal attribute which indicates if the two objects are equal or not.

Example usage:

>>> import math
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import EqualNanHandler
>>> from coola.equality.testers import EqualityTester
>>> class MyFloat:
...     def __init__(self, value: float) -> None:
...         self._value = value
...     def equal(self, other: float, equal_nan: bool = False) -> bool:
...         if equal_nan and math.isnan(self._value) and math.isnan(other):
...             return True
...         return self._value == other
...
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = EqualNanHandler()
>>> handler.handle(MyFloat(42), 42, config)
True
>>> handler.handle(MyFloat(float("nan")), float("nan"), config)
False
>>> config.equal_nan = True
>>> handler.handle(MyFloat(float("nan")), float("nan"), config)
True

coola.equality.handlers.FalseHandler

Bases: BaseEqualityHandler

Implement a handler that always return False.

This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import FalseHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = FalseHandler()
>>> handler.handle("abc", "abc", config)
False
>>> handler.handle("abc", "ABC", config)
False

coola.equality.handlers.JaxArrayEqualHandler

Bases: BaseEqualityHandler

Check if the two JAX arrays are equal.

This handler returns True if the two arrays are equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import jax.numpy as jnp
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import JaxArrayEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = JaxArrayEqualHandler()
>>> handler.handle(jnp.ones((2, 3)), jnp.ones((2, 3)), config)
True
>>> handler.handle(jnp.ones((2, 3)), jnp.zeros((2, 3)), config)
False

coola.equality.handlers.MappingSameKeysHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same keys.

This handler returns False if the two objects have different keys, otherwise it passes the inputs to the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import MappingSameKeysHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = MappingSameKeysHandler()
>>> handler.handle({"a": 1, "b": 2}, {"a": 1, "b": 2, "c": 1}, config)
False

coola.equality.handlers.MappingSameValuesHandler

Bases: AbstractEqualityHandler

Check if the key-value pairs in the first mapping are in the second mapping.

This handler returns False if the one of the key-value pair in the first mapping is not in the second mapping, otherwise it passes the inputs to the next handler.

Notes

This handler assumes that all the keys in the first mapping are also in the second mapping. The second mapping can have more keys. To check if two mappings are equal, you can combine this handler with MappingSameKeysHandler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import MappingSameValuesHandler, TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = MappingSameValuesHandler(next_handler=TrueHandler())
>>> handler.handle({"a": 1, "b": 2}, {"a": 1, "b": 2}, config)
True
>>> handler.handle({"a": 1, "b": 2}, {"a": 1, "b": 3}, config)
False

coola.equality.handlers.NanEqualHandler

Bases: AbstractEqualityHandler

Check if the two NaNs are equal.

This handler returns True if the two numbers are NaNs, otherwise it passes the inputs to the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import NanEqualHandler, FalseHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = NanEqualHandler(next_handler=FalseHandler())
>>> handler.handle(float("nan"), float("nan"), config)
False
>>> config.equal_nan = True
>>> handler.handle(float("nan"), float("nan"), config)
True

coola.equality.handlers.NumpyArrayEqualHandler

Bases: BaseEqualityHandler

Check if the two NumPy arrays are equal.

This handler returns True if the two arrays are equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import NumpyArrayEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = NumpyArrayEqualHandler()
>>> handler.handle(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> handler.handle(np.ones((2, 3)), np.zeros((2, 3)), config)
False

coola.equality.handlers.ObjectEqualHandler

Bases: BaseEqualityHandler

Check if the two objects are equal using the default equality operator ==.

This handler returns True if the two objects are equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import ObjectEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = ObjectEqualHandler()
>>> handler.handle(1, 1, config)
True
>>> handler.handle(1, "abc", config)
False

coola.equality.handlers.PandasDataFrameEqualHandler

Bases: BaseEqualityHandler

Check if the two pandas.DataFrame are equal.

This handler returns True if the two pandas.DataFrames equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import pandas
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import PandasDataFrameEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = PandasDataFrameEqualHandler()
>>> handler.handle(
...     pandas.DataFrame({"col": [1, 2, 3]}),
...     pandas.DataFrame({"col": [1, 2, 3]}),
...     config,
... )
True
>>> handler.handle(
...     pandas.DataFrame({"col": [1, 2, 3]}),
...     pandas.DataFrame({"col": [1, 2, 4]}),
...     config,
... )
False

coola.equality.handlers.PandasSeriesEqualHandler

Bases: BaseEqualityHandler

Check if the two pandas.Series are equal.

This handler returns True if the two arrays pandas.Series equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import pandas
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import PandasSeriesEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = PandasSeriesEqualHandler()
>>> handler.handle(pandas.Series([1, 2, 3]), pandas.Series([1, 2, 3]), config)
True
>>> handler.handle(pandas.Series([1, 2, 3]), pandas.Series([1, 2, 4]), config)
False

coola.equality.handlers.PolarsDataFrameEqualHandler

Bases: BaseEqualityHandler

Check if the two polars.DataFrame are equal.

This handler returns True if the two polars.DataFrames equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import polars as pl
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import PolarsDataFrameEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = PolarsDataFrameEqualHandler()
>>> handler.handle(
...     pl.DataFrame({"col": [1, 2, 3]}),
...     pl.DataFrame({"col": [1, 2, 3]}),
...     config,
... )
True
>>> handler.handle(
...     pl.DataFrame({"col": [1, 2, 3]}),
...     pl.DataFrame({"col": [1, 2, 4]}),
...     config,
... )
False

coola.equality.handlers.PolarsSeriesEqualHandler

Bases: BaseEqualityHandler

Check if the two polars.Series are equal.

This handler returns True if the two arrays polars.Series equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import polars as pl
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import PolarsSeriesEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = PolarsSeriesEqualHandler()
>>> handler.handle(pl.Series([1, 2, 3]), pl.Series([1, 2, 3]), config)
True
>>> handler.handle(pl.Series([1, 2, 3]), pl.Series([1, 2, 4]), config)
False

coola.equality.handlers.PyarrowEqualHandler

Bases: BaseEqualityHandler

Check if the two pyarrow arrays or tables are equal.

This handler returns True if the two arrays or tables are equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Note that config.equal_nan, config.atol and config.rtol arguments are ignored.

Example usage:

>>> import pyarrow
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import PyarrowEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = PyarrowEqualHandler()
>>> handler.handle(pyarrow.array([1, 2, 3]), pyarrow.array([1, 2, 3]), config)
True
>>> handler.handle(pyarrow.array([1, 2, 3]), pyarrow.array([1, 2, 4]), config)
False

coola.equality.handlers.SameAttributeHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same attribute.

This handler returns False if the two objects have different attributes, otherwise it passes the inputs to the next handler. The objects must have the attribute.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameAttributeHandler, TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameAttributeHandler(name="shape", next_handler=TrueHandler())
>>> handler.handle(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> handler.handle(np.ones((2, 3)), np.ones((3, 2)), config)
False

coola.equality.handlers.SameDTypeHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same data type.

This handler returns False if the two objects have different data types, otherwise it passes the inputs to the next handler. The objects must have a dtype attribute (e.g. object.dtype) which returns the shape of the object. This handler works on numpy.ndarrays and torch.Tensors objects.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameDTypeHandler, TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameDTypeHandler(next_handler=TrueHandler())
>>> handler.handle(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> handler.handle(np.ones((2, 3), dtype=float), np.ones((2, 3), dtype=int), config)
False

coola.equality.handlers.SameDataHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same data.

This handler returns False if the two objects have different data, otherwise it passes the inputs to the next handler. The objects must have a data attribute (e.g. object.data) which returns the shape of the object.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameDataHandler, TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameDataHandler(next_handler=TrueHandler())
>>> handler.handle(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> handler.handle(np.ones((2, 3)), np.zeros((2, 3)), config)
False

coola.equality.handlers.SameLengthHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same length.

This handler returns False if the two objects have different lengths, otherwise it passes the inputs to the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameLengthHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameLengthHandler()
>>> handler.handle([1, 2, 3], [1, 2, 3, 4], config)
False

coola.equality.handlers.SameObjectHandler

Bases: AbstractEqualityHandler

Check if the two objects refer to the same object.

This handler returns True if the two objects refer to the same object, otherwise it passes the inputs to the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameObjectHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameObjectHandler()
>>> handler.handle("abc", "abc", config)
True

coola.equality.handlers.SameShapeHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same shape.

This handler returns False if the two objects have different shapes, otherwise it passes the inputs to the next handler. The objects must have a shape attribute (e.g. object.shape) which returns the shape of the object. This handler works on jax.numpy.ndarrays, numpy.ndarrays, pandas.DataFrame, polars.DataFrame and torch.Tensors objects.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameShapeHandler, TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameShapeHandler(next_handler=TrueHandler())
>>> handler.handle(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> handler.handle(np.ones((2, 3)), np.ones((3, 2)), config)
False

coola.equality.handlers.SameTypeHandler

Bases: AbstractEqualityHandler

Check if the two objects have the same type.

This handler returns False if the two objects have different types, otherwise it passes the inputs to the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameTypeHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameTypeHandler()
>>> handler.handle(1, "abc", config)
False

coola.equality.handlers.ScalarEqualHandler

Bases: BaseEqualityHandler

Check if the two numbers are equal or not.

This handler returns False if the two numbers are different, otherwise it returns True. It is possible to control the tolerance by using atol and rtol. By default, the tolerances are set to 0.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import ScalarEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = ScalarEqualHandler()
>>> handler.handle(42.0, 42.0, config)
True
>>> config.atol = 1e-3
>>> handler.handle(42.0, 42.0001, config)
True
>>> handler.handle(float("nan"), float("nan"), config)
False

coola.equality.handlers.SequenceSameValuesHandler

Bases: AbstractEqualityHandler

Check if the two sequences have the same values.

This handler returns False if the two sequences have at least one different value, otherwise it passes the inputs to the next handler. If the sequences have different length, this handler checks only the values of the shortest sequence.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SequenceSameValuesHandler, TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SequenceSameValuesHandler(next_handler=TrueHandler())
>>> handler.handle([1, 2, 3], [1, 2, 3], config)
True
>>> handler.handle([1, 2, 3], [1, 2, 4], config)
False

coola.equality.handlers.TorchTensorEqualHandler

Bases: BaseEqualityHandler

Check if the two tensors are equal.

This handler returns True if the two tensors are equal, otherwise False. This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> import torch
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers.torch_ import TorchTensorEqualHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = TorchTensorEqualHandler()
>>> handler.handle(torch.ones(2, 3), torch.ones(2, 3), config)
True
>>> handler.handle(torch.ones(2, 3), torch.zeros(2, 3), config)
False

coola.equality.handlers.TrueHandler

Bases: BaseEqualityHandler

Implement a handler that always return True.

This handler is designed to be used at the end of the chain of responsibility. This handler does not call the next handler.

Example usage:

>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import TrueHandler
>>> from coola.equality.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = TrueHandler()
>>> handler.handle("abc", "abc", config)
True
>>> handler.handle("abc", "ABC", config)
True

coola.equality.testers

Contain the testers to check if two objects are equal or not.

coola.equality.testers.BaseEqualityTester

Bases: ABC

Define the base class to implement an equality tester.

coola.equality.testers.BaseEqualityTester.equal abstractmethod

equal(
    actual: Any, expected: Any, config: EqualityConfig
) -> bool

Indicate if two objects are equal or not.

Parameters:

Name Type Description Default
actual Any

The actual input.

required
expected Any

The expected input.

required
config EqualityConfig

The equality configuration.

required

Returns:

Type Description
bool

True if the two objects are equal, otherwise False.

Example usage:

>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.testers import EqualityTester
>>> tester = EqualityTester()
>>> config = EqualityConfig(tester=tester)
>>> tester.equal([np.ones((2, 3)), np.zeros(2)], [np.ones((2, 3)), np.zeros(2)], config)
True
>>> tester.equal([np.ones((2, 3)), np.ones(2)], [np.ones((2, 3)), np.zeros(2)], config)
False

coola.equality.testers.EqualityTester

Bases: BaseEqualityTester

Implement the default equality tester.

coola.equality.testers.EqualityTester.add_comparator classmethod

add_comparator(
    data_type: type,
    comparator: BaseEqualityComparator,
    exist_ok: bool = False,
) -> None

Add an equality comparator for a given data type.

Parameters:

Name Type Description Default
data_type type

The data type for this test.

required
comparator BaseEqualityComparator

The comparator used to test the equality of the specified type.

required
exist_ok bool

If False, RuntimeError is raised if the data type already exists. This parameter should be set to True to overwrite the comparator for a type.

False

Raises:

Type Description
RuntimeError

if a comparator is already registered for the data type and exist_ok=False.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> from coola.equality.comparators import SequenceEqualityComparator
>>> EqualityTester.add_comparator(list, SequenceEqualityComparator(), exist_ok=True)

coola.equality.testers.EqualityTester.find_comparator classmethod

find_comparator(data_type: Any) -> BaseEqualityComparator

Find the equality comparator associated to an object.

Parameters:

Name Type Description Default
data_type Any

The data type to get.

required

Returns:

Type Description
BaseEqualityComparator

The equality comparator associated to the data type.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> EqualityTester.find_comparator(list)
SequenceEqualityComparator()
>>> EqualityTester.find_comparator(str)
DefaultEqualityComparator()

coola.equality.testers.EqualityTester.has_comparator classmethod

has_comparator(data_type: type) -> bool

Indicate if an equality comparator is registered for the given data type.

Parameters:

Name Type Description Default
data_type type

The data type to check.

required

Returns:

Type Description
bool

True if an equality comparator is registered, otherwise False.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> EqualityTester.has_comparator(list)
True
>>> EqualityTester.has_comparator(str)
False

coola.equality.testers.EqualityTester.local_copy classmethod

local_copy() -> LocalEqualityTester

Return a copy of EqualityTester that can easily be customized without changind EqualityTester.

Returns:

Type Description
LocalEqualityTester

A "local" copy of EqualityTester.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> tester = EqualityTester.local_copy()
>>> tester
LocalEqualityTester(...)

coola.equality.testers.LocalEqualityTester

Bases: BaseEqualityTester

Implement an equality tester that can be easily customized.

Parameters:

Name Type Description Default
registry dict[type, BaseEqualityComparator] | None

The initial registry with the equality comparators.

None

coola.equality.testers.LocalEqualityTester.add_comparator

add_comparator(
    data_type: type,
    comparator: BaseEqualityComparator,
    exist_ok: bool = False,
) -> None

Add an equality comparator for a given data type.

Parameters:

Name Type Description Default
data_type type

The data type for this test.

required
comparator BaseEqualityComparator

The comparator used to test the equality of the specified type.

required
exist_ok bool

If False, RuntimeError is raised if the data type already exists. This parameter should be set to True to overwrite the comparator for a type.

False

Raises:

Type Description
RuntimeError

if an comparator is already registered for the data type and exist_ok=False.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> from coola.equality.comparators import DefaultEqualityComparator
>>> tester = EqualityTester.local_copy()
>>> tester.add_comparator(str, DefaultEqualityComparator())
>>> tester.add_comparator(str, DefaultEqualityComparator(), exist_ok=True)

coola.equality.testers.LocalEqualityTester.clone

Clones the current tester.

Returns:

Type Description
LocalEqualityTester

A deep copy of the current tester.

Example usage:

```pycon

from coola.equality.testers import EqualityTester tester = EqualityTester.local_copy() tester_cloned = tester.clone()

```

coola.equality.testers.LocalEqualityTester.find_comparator

find_comparator(data_type: Any) -> BaseEqualityComparator

Find the equality comparator associated to an object.

Parameters:

Name Type Description Default
data_type Any

The data type to get.

required

Returns:

Type Description
BaseEqualityComparator

The equality comparator associated to the data type.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> tester = EqualityTester.local_copy()
>>> tester.find_comparator(list)
SequenceEqualityComparator()
>>> tester.find_comparator(str)
DefaultEqualityComparator()

coola.equality.testers.LocalEqualityTester.has_comparator

has_comparator(data_type: type) -> bool

Indicate if an equality comparator is registered for the given data type.

Parameters:

Name Type Description Default
data_type type

The data type to check.

required

Returns:

Type Description
bool

True if an equality comparator is registered, otherwise False.

Example usage:

>>> from coola.equality.testers import EqualityTester
>>> tester = EqualityTester.local_copy()
>>> tester.has_comparator(list)
True
>>> tester.has_comparator(str)
False