ndtools package#
- class All(initlist=None)[source]#
Bases:
UserList[Any],Combinable,EquatableImplement logical conjunction between equatables.
It should contain equatables like
All([eqatable_0, equatable_1, ...]). Then the equality operation on the target array will perform like(array == equatable_0) & array == equatable_1) & ....Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 def __ne__(self, array): return ~(self == array) class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 def __ne__(self, array): return ~(self == array) 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 equatables.
It should contain equatables like
Any([eqatable_0, equatable_1, ...]). Then the equality operation on the target array will perform like(array == equatable_0) | array == equatable_1) & ....Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 def __ne__(self, array): return ~(self == array) class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 def __ne__(self, array): return ~(self == array) Even() | Odd() # -> Any([Even(), Odd()]) np.arange(3) == Even() | Odd() # -> array([True, True, True])
- class Combinable[source]#
Bases:
objectImplement logical operations between objects.
Classes that inherit from this mix-in class can perform logical operations between the class instance and other object. Then
instance & objectwill returnAll([instance, other])andinstance | objectwill returnAny[instance, other]), whereAllandAnyare the implementation of logical conjunction and logical disjunction, respectively. In general,Combinableshould be used with theEquatableabstract base class to implement combinable equatables.Examples
import numpy as np from ndtools import Combinable, Equatable class Even(Combinable, Equatable): def __eq__(self, array): return array % 2 == 0 def __ne__(self, array): return ~(self == array) class Odd(Combinable, Equatable): def __eq__(self, array): return array % 2 == 1 def __ne__(self, array): return ~(self == array) 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:
ABCImplement equality operations for multidimensional arrays.
Classes that inherit from this abstract base class and implement both
__eq__and__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.- Raises:
TypeError – Raised if both
__eq__and__ne__are not defined.
Examples
import numpy as np from ndtools import Equatable class Even(Equatable): def __eq__(self, array): return array % 2 == 0 def __ne__(self, array): return ~(self == array) 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 Orderable[source]#
Bases:
ABCImplement ordering operations for multidimensional arrays.
Classes that inherit from this abstract base class and implement all of
__eq__,__ge__,__gt__,__le__,__lt__, and__ne__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.- Raises:
TypeError – Raised if all of
__eq__,__ge__,__gt__,__le__,__lt__, and__ne__are not defined.
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 def __gt__(self, array): return array < self.lower def __le__(self, array): return ~(self > array) def __lt__(self, array): return ~(self >= array) def __ne__(self, array): return ~(self == array) 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])
- class TotalEquality[source]#
Bases:
EquatableImplement missing equality operations for multidimensional arrays.
- Raises:
ValueError – Raised if none of the equality operators (==, !=) is defined.
Examples
import numpy as np from ndtools import TotalEquality class Even(TotalEquality): 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 TotalOrdering[source]#
Bases:
OrderableImplement missing ordering operations for multidimensional arrays.
- Raises:
ValueError – Raise if none of the ordering operator (>=, >, <=, <) is defined.
Examples
import numpy as np from dataclasses import dataclass from ndtools import TotalOrdering @dataclass class Range(TotalOrdering): 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])