ndtools.combination module#
- 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])