Skip to content

Commit a91bed6

Browse files
authored
Merge pull request #8 from semiversus/master
remove numpy and pandas to be imported by default
2 parents 1b68d0a + 35ec4b6 commit a91bed6

File tree

7 files changed

+96
-25
lines changed

7 files changed

+96
-25
lines changed

docs/usage.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ For convenience, `mini_lambda` comes bundled with the following predefined input
2525
* boolean/int/float numbers: `b` / `i`, `j`, `n` / `x`, `y`
2626
* lists/mappings: `l` / `d`
2727
* callables: `f`
28-
* numpy arrays (if numpy is present): `X`, `Y`, `M`
29-
* pandas dataframes (if pandas is present): `df`
28+
* numpy arrays (if numpy is present): `X`, `Y`, `M` (from `mini_lambda.numpy`)
29+
* pandas dataframes (if pandas is present): `df` (from `mini_lambda.pandas`)
3030

3131

3232
## Lambda Expressions vs Lambda Functions
@@ -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

@@ -344,7 +345,8 @@ Note: although the above is valid, it is much more recommended to convert the wh
344345
Classes can be entirely made lambda-friendly at once. This will convert the constructor, as well as any other method that would be available.
345346

346347
```python
347-
from mini_lambda import X, _, make_lambda_friendly_class
348+
from mini_lambda import _, make_lambda_friendly_class
349+
from mini_lambda.numpy import X
348350
import numpy as np
349351
import pandas as pd
350352

@@ -360,7 +362,8 @@ str(expr) # 'DataFrame(X).max().values[0]'
360362
Actually the `Constant()` (alias `C()` or `make_lambda_friendly()`) function that we saw above to convert constants, is also able to convert methods ans classes. So if there is only a single conversion operator to remember, remember this one.
361363

362364
```python
363-
from mini_lambda import _, C, X
365+
from mini_lambda import _, C
366+
from mini_lambda.numpy import X
364367
import numpy as np
365368
import pandas as pd
366369

mini_lambda/goodies.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,3 @@
2020

2121
# callables
2222
f = InputVar('f', Callable)
23-
24-
try:
25-
import numpy as np
26-
# matrices/arrays
27-
X = InputVar('X', np.ndarray)
28-
Y = InputVar('Y', np.ndarray)
29-
M = InputVar('M', np.ndarray)
30-
except:
31-
pass
32-
33-
try:
34-
# data frames
35-
import pandas as pd
36-
df = InputVar('df', pd.DataFrame)
37-
except:
38-
pass

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: expression evaluating the and combination
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: expression evaluating the or combination
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/numpy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
3+
from mini_lambda.main import InputVar
4+
5+
6+
# matrices/arrays
7+
X = InputVar('X', np.ndarray)
8+
Y = InputVar('Y', np.ndarray)
9+
M = InputVar('M', np.ndarray)

mini_lambda/pandas.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pandas as pd
2+
3+
from mini_lambda.main import InputVar
4+
5+
6+
# data frames
7+
df = InputVar('df', pd.DataFrame)

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 """

mini_lambda/tests/test_readme.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ def bar2(cls, times, num, den):
303303

304304
def test_doc_usage_other_classes():
305305
""" Tests that the example in doc/usage in the others/classes section works """
306-
from mini_lambda import X, _, make_lambda_friendly_class
306+
from mini_lambda import _, make_lambda_friendly_class
307+
from mini_lambda.numpy import X
307308
import numpy as np
308309
import pandas as pd
309310

@@ -316,7 +317,8 @@ def test_doc_usage_other_classes():
316317

317318
def test_doc_usage_all_at_once():
318319
""" Tests that the example in doc/usage in the others/anything section works """
319-
from mini_lambda import _, C, X
320+
from mini_lambda import _, C
321+
from mini_lambda.numpy import X
320322
import numpy as np
321323
import pandas as pd
322324

0 commit comments

Comments
 (0)