You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://smarie.github.io/python-mini-lambda/)[](https://pypi.python.org/pypi/mini-lambda/)[](https://pepy.tech/project/mini-lambda)[](https://pepy.tech/project/mini-lambda)[](https://github.com/smarie/python-mini-lambda/stargazers)
8
8
9
-
!!! success "`repr` is now enabled by default for expressions! More details [here](#new-repr-now-enabled-by-default)"
9
+
!!! success "`repr` is now enabled by default for expressions and functions! More details [here](#new-repr-now-enabled-by-default)"
10
10
11
-
This idea initially comes from the [valid8](https://smarie.github.io/python-valid8/) validation library. I ended up understanding that there were two complementary ways to provide users with easy-to-use validation functions:
11
+
`mini_lambda` allows developers to write simple expressions with a subset of standard python syntax, without the `lambda x:` prefix. These expressions can easily be transformed into functions. It is possible to get a string representation of both.
12
12
13
-
* either to provide a very exhaustive catalog of functions to cover most use cases (is greater than, is between, etc.). *Drawback*: we need to reinvent all functions that exist already.
14
-
* or to provide users with the capability to write custom validation functions, in particular using lambdas. *Drawback*: the `lambda x:` prefix has to be present everywhere, and users still have to write explicit exception messages for validation failures.
15
-
16
-
17
-
The `mini_lambda` library provides an answer to the second item: it allows developers to write simple functions with a subset of standard python syntax, without the `lambda x:` prefix. It is possible to get a string representation of these functions in order for example to automatically generate validation exception messages as done in [valid8](https://smarie.github.io/python-valid8/). But it can also be used for other use cases: indeed, although initially developed in the context of validation, this library is now fully independent.
13
+
Among many potential use cases, the original motivation came from [valid8](https://smarie.github.io/python-valid8/) where we want to let users provide their own validation functions, while still being able to raise user-friendly exceptions "showing" the formula that failed.
18
14
19
15
20
16
## Installing
@@ -25,11 +21,35 @@ The `mini_lambda` library provides an answer to the second item: it allows devel
25
21
26
22
## Usage
27
23
24
+
### a- Principles
25
+
28
26
Three basic steps:
29
27
30
-
* import or create a 'magic variable' such as `x`, `s`, `l`, `df`...
31
-
* write an expression using it.
32
-
* transform the expression into a function by wrapping it with `_()` or its aliases `L()` and`F()`.
28
+
* import or create a 'magic variable' (an `InputVar`) such as `x`, `s`, `l`, `df`...
29
+
* write an *expression* using it.
30
+
* transform the *expression* into a *function* by wrapping it with `_()`, `L()`, or `F()` (3 aliases), or by calling `as_function()` on it.
31
+
32
+
For example with a numeric variable:
33
+
34
+
```python
35
+
# -- expressions --
36
+
from mini_lambda import x
37
+
my_expr = x **2
38
+
my_expr # <LambdaExpression: x ** 2>
39
+
40
+
my_expr(12) # beware: calling an expression is still an expression !
41
+
# <LambdaExpression: (x ** 2)(12)>
42
+
43
+
# -- functions --
44
+
from mini_lambda import _
45
+
my_func = _(x **2)
46
+
my_func # <LambdaFunction: x ** 2>
47
+
48
+
assert my_func(12) ==144# calling a function executes it as expected
49
+
```
50
+
51
+
52
+
Or with a string variable:
33
53
34
54
```python
35
55
# create or import a magic variable, here we import 's'
@@ -46,6 +66,20 @@ say_hello_function('world') # Returns "Hello, world !"
46
66
print(say_hello_function) # "'Hello, ' + s + ' !'"
47
67
```
48
68
69
+
### b- Capabilities
70
+
71
+
The variable can represent anything, not necessarily a primitive. If you wish to use another symbol just define it using `InputVar`:
72
+
73
+
```python
74
+
from mini_lambda import InputVar
75
+
z = InputVar('z')
76
+
77
+
from logging import Logger
78
+
l = InputVar('l', Logger)
79
+
```
80
+
81
+
Note that the type information is optional, it is just for your IDE's autocompletion capabilities.
82
+
49
83
Most of python syntax can be used in an expression:
50
84
51
85
```python
@@ -89,16 +123,18 @@ There are of course a few limitations to `mini_lambda` as compared to full-flavo
89
123
90
124
Check the [Usage](./usage/) page for more details.
91
125
92
-
## New: `repr` now enabled by default
126
+
###New: `repr` now enabled by default
93
127
94
-
Starting in version 2.0.0, the representation of lambda expressions does not raise exceptions anymore by default. This behaviour was a pain for developers, and was only like this for the very rare occasions where `repr` was used in the expression.
128
+
Starting in version 2.0.0, the representation of lambda expressions does not raise exceptions anymore by default. This behaviour was a pain for developers, and was only like this for the very rare occasions where `repr` was needed in the expression itself.
95
129
96
130
So now
97
131
98
132
```python
99
-
>>>from mini_lambda import x
133
+
>>>from mini_lambda import x, F
100
134
>>> x **2
101
135
<LambdaExpression: x **2>
136
+
>>> F(x **2)
137
+
<LambdaFunction: x **2>
102
138
```
103
139
104
140
If you wish to bring back the old exception-raising behaviour, simply set the `repr_on` attribute of your expressions to `False`:
@@ -111,6 +147,57 @@ If you wish to bring back the old exception-raising behaviour, simply set the `r
111
147
mini_lambda.base.FunctionDefinitionError: __repr__isnot supported by this Lambda Expression. (...)
112
148
```
113
149
150
+
### c- How to support mini-lambda expressions in your libraries.
151
+
152
+
You may wish to support mini-lambda *expressions* (not *functions*) directly into your code. That way, your users will not even have to convert their expressions into functions - this will bring more readability and ease of use for them.
153
+
154
+
You can do this with `as_function`: this will convert expressions to functions if needed, but otherwise silently return its input.
155
+
156
+
```python
157
+
from mini_lambda import _, s, as_function
158
+
159
+
defcall_with_hello(f):
160
+
"""An example custom method that is lambda_friendy"""
161
+
162
+
# transform mini-lambda expression to function if needed.
163
+
f = as_function(f)
164
+
165
+
return f('hello')
166
+
167
+
# it works with a normal function
168
+
deffoo(s):
169
+
return s[0]
170
+
assert call_with_hello(foo) =='h'
171
+
172
+
# with a mini-lambda *Function* (normal: this is a function)
173
+
assert call_with_hello(_(s[0])) =='h'
174
+
175
+
# and with a mini-lambda *Expression* too (this is new and easier to read)
176
+
assert call_with_hello(s[0]) =='h'
177
+
```
178
+
179
+
In addition a `is_mini_lambda_expr` helper is also provided, if you wish to perform some reasoning:
180
+
181
+
```python
182
+
from mini_lambda import x, is_mini_lambda_expr, as_function
183
+
184
+
# mini lambda: true
185
+
assert is_mini_lambda_expr(x **2)
186
+
187
+
# standard lambda: false
188
+
assertnot is_mini_lambda_expr(lambdax: x)
189
+
190
+
# standard function: false
191
+
deffoo():
192
+
pass
193
+
assertnot is_mini_lambda_expr(foo)
194
+
195
+
# mini lambda as function: false
196
+
f = as_function(x **2)
197
+
assertnot is_mini_lambda_expr(f)
198
+
```
199
+
200
+
114
201
## Main features
115
202
116
203
* More compact lambda expressions for single-variable functions
0 commit comments