Skip to content

Commit 86a4dc0

Browse files
committed
added And and Or function
1 parent 61c10ba commit 86a4dc0

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

docs/usage.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,15 @@ or through provided workarounds :
148148

149149
```python
150150
from mini_lambda import b, i, s, l, x
151-
from mini_lambda import Slice, Get, Not, In
151+
from mini_lambda import Slice, Get, Not, In, And
152152
from mini_lambda import Iter, Repr, Format, Len, Int, Any, Log, DDecimal
153153
from math import log
154154
from decimal import Decimal
155155

156156
# boolean logic
157157
expr = (x > 1) and (x < 5) # fails
158158
expr = (x > 1) & (x < 5) # OK
159+
expr = And(x > 1, x < 5) # OK
159160
# iterating
160161
expr = next(iter(s)) # fails
161162
expr = next(Iter(s)) # OK
@@ -207,7 +208,7 @@ As seen above, there are several types of defective behaviours:
207208

208209
* built-in behaviours with special syntax (`not b`, `{'a': 1}[s]`, `x in y`, `any_(x)`). In which case an equivalent explicit method is provided: `Not`, `Get`, `Slice`, `In`, `Any`, `All`. In addition, equivalent methods `<expr>.contains()`, `<expr>.is_in()`, `<expr>.not_()`, `<expr>.any_()`, and `<expr>.all_()` are provided.
209210

210-
* the shortcircuit boolean operators `and/or` can not be overriden and check the return type, so you should use `&` or `|` instead
211+
* the shortcircuit boolean operators `and/or` can not be overridden and check the return type, so you should use either bitwise combination (`&` or `|`) or logical (`And` or `Or`) instead.
211212

212213
* any other 'standard' methods, whether they are object constructors `Decimal()` or functions such as `log()`. We will see in the next section how you can convert any existing class or method to a lambda-friendly one. `mini_lambda` comes bundled with a few of them, namely all constants, functions and classes defined in `math` and `decimal` modules.
213214

mini_lambda/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,37 @@ def Not(expression: _LambdaExpression):
428428
return expression.not_()
429429

430430

431+
# Logical combinations 'and' and 'or'
432+
def _and(a, b):
433+
return a and b
434+
435+
436+
def And(a, b):
437+
"""
438+
Equivalent of 'a and b'.
439+
440+
:param a: left operand
441+
:param b: right operand
442+
:return:
443+
"""
444+
return _LambdaExpression._get_expression_for_method_with_args(_and, a, b)
445+
446+
447+
def _or(a, b):
448+
return a or b
449+
450+
451+
def Or(a, b):
452+
"""
453+
Equivalent of 'a or b'.
454+
455+
:param a: left operand
456+
:param b: right operand
457+
:return:
458+
"""
459+
return _LambdaExpression._get_expression_for_method_with_args(_or, a, b)
460+
461+
431462
# Special case: we do not want to use format() but type(value).format. So we override the generated method
432463
def Format(value, *args, **kwargs):
433464
"""

mini_lambda/tests/test_mini_lambda.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66

77
from mini_lambda import InputVar, Len, Str, Int, Repr, Bytes, Sizeof, Hash, Bool, Complex, Float, Oct, Iter, \
8-
Any, All, _, Slice, Get, Not, FunctionDefinitionError, Format, C
8+
Any, All, _, Slice, Get, Not, FunctionDefinitionError, Format, C, And, Or
99
from math import cos, isfinite
1010
from numbers import Real
1111

@@ -305,6 +305,41 @@ def test_evaluator_truth_testable_not():
305305
assert h(0)
306306
assert not h(5.2)
307307

308+
309+
def test_evaluator_logical_and():
310+
""" Object: Tests that And function works """
311+
312+
x = InputVar('x', int)
313+
314+
with pytest.raises(FunctionDefinitionError):
315+
x > 5 and x < 10
316+
317+
r = And(x > 5, x < 10)
318+
r = r.as_function()
319+
320+
assert not r(5)
321+
assert r(6)
322+
assert r(9)
323+
assert not r(10)
324+
325+
326+
def test_evaluator_logical_or():
327+
""" Object: Tests that Or function works """
328+
329+
x = InputVar('x', int)
330+
331+
with pytest.raises(FunctionDefinitionError):
332+
x < 5 or x > 10
333+
334+
r = Or(x < 5, x > 10)
335+
r = r.as_function()
336+
337+
assert r(4)
338+
assert not r(5)
339+
assert not r(10)
340+
assert r(11)
341+
342+
308343
# Object: .__getattr__
309344
def test_evaluator_attribute():
310345
""" Object: Tests that obj.foo_field works """

0 commit comments

Comments
 (0)