4 Example Operators for comparison
6 ... def __init__(self, x):
9 >>> x, y, z = C(1), C(1), C(2)
10 >>> x == y, hash(x) == hash(y)
19 class KeyedEqualityOperators(object):
21 Mixin for auto-implementing comparison operators
22 @note Requires inheriting class to implement a '__key__' function
24 >>> class C(KeyedEqualityOperators):
25 ... def __init__(self, x):
27 ... def __key__(self):
30 >>> x, y, z = C(1), C(1), C(2)
31 >>> x == y, hash(x) == hash(y)
38 def __eq__(self, other):
39 return self.__key__() == other.__key__()
41 def __ne__(self, other):
42 return self.__key__() != other.__key__()
45 class KeyedComparisonOperators(KeyedEqualityOperators):
47 Mixin for auto-implementing comparison operators
48 @note Requires inheriting class to implement a '__key__' function
50 >>> class C(KeyedComparisonOperators):
51 ... def __init__(self, x):
53 ... def __key__(self):
56 >>> x, y, z = C(1), C(1), C(2)
57 >>> x == y, y < z, hash(x) == hash(y)
64 def __cmp__(self, other):
65 return cmp(self.__key__(), other.__key__())
67 def __lt__(self, other):
68 return self.__key__() < other.__key__()
70 def __le__(self, other):
71 return self.__key__() <= other.__key__()
73 def __gt__(self, other):
74 return self.__key__() > other.__key__()
76 def __ge__(self, other):
77 return self.__key__() >= other.__key__()
80 class KeyedHashing(object):
82 Mixin for auto-implementing comparison operators
83 @note Requires inheriting class to implement a '__key__' function
85 >>> class C(KeyedHashing):
86 ... def __init__(self, x):
88 ... def __key__(self):
91 >>> x, y, z = C(1), C(1), C(2)
92 >>> x == y, hash(x) == hash(y)
100 return hash(self.__key__())
103 class NotEqualOperator(object):
105 Mixin for auto-implementing comparison operators
106 @note Requires inheriting class to implement '__eq__' function
109 def __ne__(self, other):
110 return not (self == other)
113 class ComparisonOperators(NotEqualOperator):
115 Mixin for auto-implementing comparison operators
116 @note Requires inheriting class to implement '__lt__' function
119 def __le__(self, other):
120 return(self < other) or(self == other)
122 def __gt__(self, other):
123 return not(self <= other)
125 def __ge__(self, other):
126 return not(self < other)
132 http://code.activestate.com/recipes/384122/
135 >>> x = infix(operator.mul)
140 def __init__(self, func):
141 self.__name__ = func.__name__
142 self.__doc__ = func.__doc__
144 self.__dict__.update(func.__dict__)
145 except AttributeError:
149 def __ror__(self, other):
150 return infix(lambda x: self.function(other, x))
152 def __or__(self, other):
153 return self.function(other)
155 def __call__(self, lhs, rhs):
156 return self.function(lhs, rhs)
164 def __init__(self, value):
169 def mbind(maybe, func):
176 return func(maybe.value)
181 >>> class Sheep(object):
182 ... def __init__(self, name):
184 ... self.mother = None
185 ... self.father = None
187 >>> def father(sheep):
188 ... if sheep.father is None:
191 ... return Just(sheep.father)
193 >>> def mother(sheep):
194 ... if sheep.mother is None:
197 ... return Just(sheep.mother)
199 >>> def mothersFather(sheep):
200 ... return mreturn(sheep) |mbind| mother |mbind| father
202 >>> def mothersPaternalGrandfather(sheep):
203 ... return mreturn(sheep) |mbind| mother |mbind| father |mbind| father
205 >>> shawn = Sheep("Shawn")
206 >>> gertrude = Sheep("Gertrude")
207 >>> ernie = Sheep("Ernie")
208 >>> frank = Sheep("Frank")
210 >>> shawn.mother = gertrude
211 >>> gertrude.father = ernie
212 >>> ernie.father = frank
214 >>> print mothersFather(shawn).value.name
216 >>> print mothersPaternalGrandfather(shawn).value.name
218 >>> print mothersPaternalGrandfather(ernie)
225 truth = itertools.imap(operator.truth, args)
226 return reduce(operator.xor, truth)
230 truth = itertools.imap(operator.truth, args)
231 return reduce(lambda a, b: not operator.xor(a, b), truth)