https://github.com/likianta/lambda-ex
Python lambda expression in multiple lines.
https://github.com/likianta/lambda-ex
Last synced: 6 months ago
JSON representation
Python lambda expression in multiple lines.
- Host: GitHub
- URL: https://github.com/likianta/lambda-ex
- Owner: likianta
- License: mit
- Created: 2022-08-10T04:27:28.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-19T09:50:52.000Z (over 2 years ago)
- Last Synced: 2025-02-08T05:26:26.305Z (8 months ago)
- Language: Python
- Size: 289 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lambda-ex
python lambda expression in multiple lines.
## install
```shell
pip install git+https://github.com/likianta/lambda-ex
```*note: it requires python 3.8+.*
## usage
```python
from lambda_ex import xlambdaadd = xlambda('a, b', """
return a + b
""")print(add(1, 2)) # -> 3
```### kwargs
```python
from lambda_ex import xlambdaadd = xlambda('a, b, c=0', """
return a + b + c
""")print(add(1, 2)) # -> 3
print(add(1, 2, 3)) # -> 6
print(add(a=1, b=2, c=3)) # -> 6
```if you are passing a complex object, for example a long list, you can also use
"post" kwargs like below:```python
from lambda_ex import xlambdaprint_names = xlambda('title_case=False', """
for n in names:
if title_case:
print(n.title())
else:
print(n)
""", kwargs={
'names': (
'anne',
'bob',
'charlie',
'david',
'erin',
)
})print_names(title_case=True)
# -> Anne
# Bob
# Charlie
# David
# Erin
print_names(title_case=True, names=('fred', 'george', 'harry'))
# -> Fred
# George
# Harry
```### type annotations
```python
from lambda_ex import xlambdaadd = xlambda('a: int, b: int', """
return a + b
""")print(add(1, 2)) # -> 3
```### recursive call
use `__selfunc__` to call itself:
```python
from lambda_ex import xlambdafibonacci = xlambda(('n'), """
if n <= 0:
raise ValueError(n)
if n <= 2:
return 1
return __selfunc__(n - 1) + __selfunc__(n - 2)
""")fibonacci(10) # -> 55
```### context (locals and globals)
lambda-ex can directly access locals and globals in its occurrence:
```python
from lambda_ex import xlambdaa = 1
b = 2add = xlambda('', """
return a + b
""")add() # -> 3
```and modify "global" values:
```python
from lambda_ex import xlambdaa = 1
b = 2
c = 0add = xlambda('', """
global c
c = 3
return a + b + c
""")print(add()) # -> 6
print(a, b, c) # -> 1 2 3
```warning: there is some limitation in this case, see [here](#20220810124919).
## tips & tricks
- please check `examples` folder to get more usages.
- if you're using pycharm, you can add a code template to pycharm's live
template:```
xlambda('$END$', """""")
```
- by default lambda-ex inherits caller context, if you want to forbid this
(it would be little faster then), set `inherit_context` to False:```python
from lambda_ex import xlambda
hello_world = xlambda('', """
print('hello world')
""", inherit_context=False)
```## cautions & limitations
- use `\\n` instead of `\n` in your lambda expression. or you may use the
r-string (example below).```python
from lambda_ex import xlambda
foo = xlambda('', r"""
print('one\ntwo\nthree')
""")
foo()
```- you can only use `global` when xlambda in top module, otherwise it won't
affect outside variables:```python
from lambda_ex import xlambdadef foo():
a = 1
b = 2
c = 0add = xlambda('', """
global c # no effect
# btw do never use `nonlocals ...` in xlambda, it will raise an
# error at once.
c = 3
return a + b + c
""")print(add()) # -> 6
print(a, b, c) # -> 1 2 0
# ^ no changefoo()
```