ndtools.comparison.comparables module#
- class All(initlist=None)[source]#
Bases:
UserList[Any],Combinable,EquatableImplement logical conjunction between comparables.
It should contain comparables like
All([comparable_0, comparable_1, ...]). Then the equality operation on the target array will perform like(array == comparable_0) & array == comparable_1) & ....Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 Even() & Odd() # -> All([Even(), Odd()]) np.arange(3) == Even() & Odd() # -> array([False, False, False])
- class Any(initlist=None)[source]#
Bases:
UserList[Any],Combinable,EquatableImplement logical disjunction between comparables.
It should contain comparables like
Any([comparable_0, comparable_1, ...]). Then the equality operation on the target array will perform like(array == comparable_0) | array == comparable_1) & ....Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 Even() | Odd() # -> Any([Even(), Odd()]) np.arange(3) == Even() | Odd() # -> array([True, True, True])
- class Combinable[source]#
Bases:
objectImplement logical operations between comparables.
Classes that inherit from this mixin class can perform logical operations between comparables. Then
comparable_0 & comparable_1 & ...will returnAll([comparable_0, comparable_1, ...])andcomparable_0 | comparable_1 | ...will returnAny([comparable_0, comparable_1, ...]). whereAllandAnyare the implementation of logical conjunction and logical disjunction, respectively.Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 Even() & Odd() # -> All([Even(), Odd()]) Even() | Odd() # -> Any([Even(), Odd()]) np.arange(3) == Even() & Odd() # -> array([False, False, False]) np.arange(3) == Even() | Odd() # -> array([True, True, True])
- class Equatable[source]#
Bases:
objectImplement equality operations for multidimensional arrays.
Classes that inherit from this mixin class and implement
__eq__or__ne__special methods can perform their own equality operations on multidimensional arrays. These special methods should be implemented for the target array likedef __eq__(self, array). Then the class instance and the array can performinstance == arrayandarray == instance.Examples
import numpy as np from ndtools import Equatable class Even(Equatable): def __eq__(self, array): return array % 2 == 0 Even() == np.arange(3) # -> array([True, False, True]) np.arange(3) == Even() # -> array([True, False, True]) Even() != np.arange(3) # -> array([False, True, False]) np.arange(3) != Even() # -> array([False, True, False])
- class Not(comparable: Any, /)[source]#
Bases:
Combinable,EquatableImplement logical negation for comparables.
It should wrap a comparable like
Not(comparable). Then the equality operation on the target array will perform likearray != comparable.Examples
import numpy as np from ndtools import Not np.arange(3) == Not(1) # -> array([True, False, True])
- Parameters:
comparable (Any)
- class Orderable[source]#
Bases:
objectImplement ordering operations for multidimensional arrays.
Classes that inherit from this mixin base class and implement both (1)
__eq__or__ne__special methods and (2)__ge__,__gt__,__le__, or__lt__special methods can perform their own ordering operations on multidimensional arrays. These special methods should be implemented for the target array likedef __ge__(self, array). Then the class instance and the array can performinstance >= arrayandarray <= instance.Examples
import numpy as np from dataclasses import dataclass from ndtools import Orderable @dataclass class Range(Orderable): lower: float upper: float def __eq__(self, array): return (array >= self.lower) & (array < self.upper) def __ge__(self, array): return array < self.upper Range(1, 2) == np.arange(3) # -> array([False, True, False]) np.arange(3) == Range(1, 2) # -> array([False, True, False]) Range(1, 2) >= np.arange(3) # -> array([True, True, False]) np.arange(3) <= Range(1, 2) # -> array([True, True, False])