Skip to content

Commit 2f6ee74

Browse files
committed
Simplified & and | operators and fixed ^ operator
1 parent 6c6e048 commit 2f6ee74

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

mini_lambda/main.py

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,15 @@ def __and__(self, other):
134134
'expression')
135135

136136
# create a new _InputEvaluator able to evaluate both sides with short-circuit capability
137-
def evaluate_both_inner_functions_and_combine(*args, **kwargs):
137+
def evaluate_both_inner_functions_and_combine(input):
138138
# first evaluate self
139-
left = self.evaluate(*args, **kwargs)
139+
left = self.evaluate(input)
140140
if not left:
141141
# short-circuit: the left part is False, no need to evaluate the right part
142142
return False
143143
else:
144144
# evaluate the right part
145-
if isinstance(other, _InputEvaluator):
146-
right = other.evaluate(*args, **kwargs)
147-
else:
148-
# standard callable
149-
right = other(*args, **kwargs)
150-
return bool(right)
145+
return bool(evaluate(other, input))
151146

152147
string_expr = get_repr(self, PRECEDENCE_BITWISE_AND) + ' & ' + get_repr(other, PRECEDENCE_BITWISE_AND)
153148
return _InputEvaluator(fun=evaluate_both_inner_functions_and_combine,
@@ -191,20 +186,15 @@ def __or__(self, other):
191186
'expression')
192187

193188
# create a new _InputEvaluator able to evaluate both sides with short-circuit capability
194-
def evaluate_both_inner_functions_and_combine(*args, **kwargs):
189+
def evaluate_both_inner_functions_and_combine(input):
195190
# first evaluate self
196-
left = self.evaluate(*args, **kwargs)
191+
left = self.evaluate(input)
197192
if left:
198193
# short-circuit: the left part is True, no need to evaluate the right part
199194
return True
200195
else:
201196
# evaluate the right part
202-
if isinstance(other, _InputEvaluator):
203-
right = other.evaluate(*args, **kwargs)
204-
else:
205-
# standard callable
206-
right = other(*args, **kwargs)
207-
return bool(right)
197+
return bool(evaluate(other, input))
208198

209199
string_expr = get_repr(self, PRECEDENCE_BITWISE_OR) + ' | ' + get_repr(other, PRECEDENCE_BITWISE_OR)
210200
return _InputEvaluator(fun=evaluate_both_inner_functions_and_combine,
@@ -240,18 +230,16 @@ def __xor__(self, other):
240230
'expression')
241231

242232
# create a new _InputEvaluator able to evaluate both sides
243-
def evaluate_both_inner_functions_and_combine(*args, **kwargs):
233+
def evaluate_both_inner_functions_and_combine(input):
244234
# first evaluate self
245-
left = self.evaluate(*args, **kwargs)
235+
left = self.evaluate(input)
236+
246237
# evaluate the right part
247-
if isinstance(other, _InputEvaluator):
248-
right = other.evaluate(*args, **kwargs)
249-
else:
250-
# standard callable
251-
right = other(*args, **kwargs)
252-
return left ^ right
238+
right = evaluate(other, input)
253239

254-
string_expr = get_repr(self, PRECEDENCE_BITWISE_XOR) + ' | ' + get_repr(other, PRECEDENCE_BITWISE_XOR)
240+
return (left and not right) or (not left and right)
241+
242+
string_expr = get_repr(self, PRECEDENCE_BITWISE_XOR) + ' ^ ' + get_repr(other, PRECEDENCE_BITWISE_XOR)
255243
return _InputEvaluator(fun=evaluate_both_inner_functions_and_combine,
256244
precedence_level=PRECEDENCE_BITWISE_XOR,
257245
str_expr=string_expr,
@@ -276,15 +264,15 @@ def all(self):
276264
def __index__(self):
277265
""" This magic method is forbidden because python casts the result before returning """
278266
raise FunctionDefinitionError('It is not currently possible to use an _InputEvaluator as an index, e.g.'
279-
'o[x], where x is the input variable. Instead, please use the equivalent operator '
280-
'provided in mini_lambda : Get(o, x)')
267+
'o[x], where x is the input variable. Instead, please use the equivalent operator'
268+
' provided in mini_lambda : Get(o, x)')
281269

282270
# Special case: membership testing
283271
def __contains__(self, item):
284272
""" This magic method is forbidden because python casts the result before returning """
285273
raise FunctionDefinitionError('membership operators in/ not in cannot be used with an _InputEvaluator because '
286-
'python casts the result as a bool. Therefore this __contains__ method is forbidden. '
287-
'An alternate x.contains() method is provided to replace it')
274+
'python casts the result as a bool. Therefore this __contains__ method is '
275+
'forbidden. An alternate x.contains() method is provided to replace it')
288276

289277
def contains(self, item):
290278
""" Returns a new _InputEvaluator performing '__contains__' on the result of this evaluator's evaluation """
@@ -319,7 +307,8 @@ def ___call__(input):
319307

320308
# return a new InputEvaluator of the same type than self, with the new function as inner function
321309
# Note: we use precedence=None for coma-separated items inside the parenthesis
322-
string_expr = get_repr(self, PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF) + '(' + ', '.join([get_repr(arg, None) for arg in args]) + ')'
310+
string_expr = get_repr(self, PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF) \
311+
+ '(' + ', '.join([get_repr(arg, None) for arg in args]) + ')'
323312
return type(self)(fun=___call__, precedence_level=PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF,
324313
str_expr=string_expr, root_var=self._root_var)
325314

@@ -335,7 +324,8 @@ def ___getitem__(input):
335324

336325
# return a new InputEvaluator of the same type than self, with the new function as inner function
337326
# Note: we use precedence=None for coma-separated items inside the parenthesis
338-
string_expr = get_repr(self, PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF) + '[' + ', '.join([get_repr(arg, None) for arg in args]) + ']'
327+
string_expr = get_repr(self, PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF) \
328+
+ '[' + ', '.join([get_repr(arg, None) for arg in args]) + ']'
339329
return type(self)(fun=___getitem__, precedence_level=PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF,
340330
str_expr=string_expr, root_var=self._root_var)
341331

@@ -349,7 +339,8 @@ def ___pow__(input):
349339
return r ** evaluate(other, input)
350340

351341
# return a new InputEvaluator of the same type than self, with the new function as inner function
352-
string_expr = get_repr(self, PRECEDENCE_EXPONENTIATION) + ' ** ' + get_repr(other, PRECEDENCE_POS_NEG_BITWISE_NOT)
342+
string_expr = get_repr(self, PRECEDENCE_EXPONENTIATION) + ' ** ' \
343+
+ get_repr(other, PRECEDENCE_POS_NEG_BITWISE_NOT)
353344
return type(self)(fun=___pow__, precedence_level=13, str_expr=string_expr, root_var=self._root_var)
354345

355346
# Special case for string representation because pow is asymetric in precedence
@@ -362,7 +353,8 @@ def ___rpow__(input):
362353
return evaluate(other, input) ** r
363354

364355
# return a new InputEvaluator of the same type than self, with the new function as inner function
365-
string_expr = get_repr(other, PRECEDENCE_EXPONENTIATION) + ' ** ' + get_repr(self, PRECEDENCE_POS_NEG_BITWISE_NOT)
356+
string_expr = get_repr(other, PRECEDENCE_EXPONENTIATION) + ' ** ' \
357+
+ get_repr(self, PRECEDENCE_POS_NEG_BITWISE_NOT)
366358
return type(self)(fun=___rpow__, precedence_level=13, str_expr=string_expr, root_var=self._root_var)
367359

368360
# Special case : unbound function call but with left/right
@@ -567,4 +559,3 @@ def InputVar(var_name: str = None, typ: Type[T] = None) -> Union[T, _InputEvalua
567559
s = InputVar('s', str)
568560
x = InputVar('x', int)
569561
l = InputVar('l', list)
570-

0 commit comments

Comments
 (0)