Skip to content

Commit 85906c5

Browse files
committed
Minor improvements: "Constant" is now the pivot point to convert anything to lambda-friendly. It has a new alias make_lambda_friendly.
* Fixed #2 that was a bug happening when using lambda-friendly methods with non-lambda arguments * This actually required to refactor a bit the methods: a few methods in base.py are now classmethods of _LambdaExpressionBase. This also required the code generation mechanism to generate a new file 'generated2.py' because the generated methods were now dependent upon main.py
1 parent 22ff9ff commit 85906c5

File tree

12 files changed

+354
-280
lines changed

12 files changed

+354
-280
lines changed

code_generation/goodies_template.mako

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# ----
22
# This file is generated by mini_lambda_methods_generation.py - do not modify it !
33
# ----
4-
from mini_lambda.base import make_lambda_friendly_method
5-
from mini_lambda.main import C, make_lambda_friendly_class
4+
from mini_lambda.main import C, make_lambda_friendly_class, make_lambda_friendly_method
65

76
% for o in import_lines:
87
${o}
@@ -12,7 +11,9 @@ ${o}
1211
% if o.constant_name:
1312
${o.item_name} = C(${o.constant_name}, '${o.constant_name}')
1413
% elif o.function_name:
14+
% if o.function_name != 'Format':
1515
${o.item_name} = make_lambda_friendly_method(${o.function_name}, '${o.function_name}')
16+
% endif
1617
% else:
1718
## CLASS
1819
${o.item_name} = make_lambda_friendly_class(${o.class_name})

code_generation/mini_lambda_methods_generation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def generate_code():
128128
# generate
129129
generate_from_template('mini_lambda_template.mako', 'generated.py',
130130
dict(to_override=to_override, to_override_with_exception=to_override_with_exception))
131+
generate_from_template('mini_lambda_template_2.mako', 'generated2.py',
132+
dict(to_override=to_override, to_override_with_exception=to_override_with_exception))
131133

132134
# (2) to-do list for the second template
133135
import_lines, to_create = define_goodies()

code_generation/mini_lambda_template.mako

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
from typing import Any
55

66
from mini_lambda.base import _LambdaExpressionBase, evaluate, get_repr, FunctionDefinitionError, \
7-
_get_expr_or_result_for_method, _get_root_var
7+
_get_root_var
88
from mini_lambda.base import _PRECEDENCE_ADD_SUB, _PRECEDENCE_MUL_DIV_ETC, _PRECEDENCE_COMPARISON, \
99
_PRECEDENCE_EXPONENTIATION, _PRECEDENCE_SHIFTS, _PRECEDENCE_POS_NEG_BITWISE_NOT, \
1010
_PRECEDENCE_SUBSCRIPTION_SLICING_CALL_ATTRREF
11-
from sys import getsizeof
1211

1312

1413
class _LambdaExpressionGenerated(_LambdaExpressionBase):
@@ -158,19 +157,3 @@ class _LambdaExpressionGenerated(_LambdaExpressionBase):
158157
'details.')
159158

160159
% endfor
161-
162-
# ******* All replacement methods for the magic methods throwing exceptions ********
163-
% for o in to_override_with_exception:
164-
% if o.unbound_method:
165-
def ${o.module_method_name}(*args, **kwargs):
166-
""" This is a replacement method for _LambdaExpression '${o.method_name}' magic method """
167-
## return evaluator.add_unbound_method_to_stack(${o.unbound_method.__name__})
168-
return _get_expr_or_result_for_method(${o.unbound_method.__name__}, *args, **kwargs)
169-
% else:
170-
def ${o.module_method_name}(expr: _LambdaExpressionGenerated, *args, **kwargs):
171-
""" This is a replacement method for _LambdaExpression '${o.method_name}' magic method """
172-
return expr.add_bound_method_to_stack('${o.method_name}', *args, **kwargs)
173-
% endif
174-
175-
176-
% endfor
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ----
2+
# This file is generated by mini_lambda_methods_generation.py - do not modify it !
3+
# ----
4+
from mini_lambda.main import _LambdaExpression
5+
from sys import getsizeof
6+
7+
8+
# ******* All replacement methods for the magic methods throwing exceptions ********
9+
% for o in to_override_with_exception:
10+
% if o.module_method_name == 'Format':
11+
## skip this one
12+
13+
% elif o.unbound_method:
14+
def ${o.module_method_name}(*args, **kwargs):
15+
""" This is a replacement method for _LambdaExpression '${o.method_name}' magic method """
16+
## return evaluator.add_unbound_method_to_stack(${o.unbound_method.__name__})
17+
return _LambdaExpression._get_expression_for_method_with_args(${o.unbound_method.__name__}, *args, **kwargs)
18+
% else:
19+
def ${o.module_method_name}(expr: _LambdaExpression, *args, **kwargs):
20+
""" This is a replacement method for _LambdaExpression '${o.method_name}' magic method """
21+
return expr.add_bound_method_to_stack('${o.method_name}', *args, **kwargs)
22+
% endif
23+
24+
25+
% endfor

docs/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### 1.2.0 - New alias and bugfix for constant functions
2+
3+
* added alias `make_lambda_friendly` for `Constant`, since it is able to convert anything (constants, functions and classes) to lambda-friendly objects usable in expressions.
4+
* Fixed [#2](https://github.com/smarie/python-mini-lambda/issues/2) that was a bug happening when using lambda-friendly methods with non-lambda arguments
5+
16
### 1.1.0 - Compatibility with standard functions
27

38
* It is now possible to use any function in a lambda expression, through use of the `make_lambda_friendly_...` methods (see [documentation](./usage#supporting-any-other-methods-and-classes))

docs/usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ from math import e
236236
str(_(x + e)) # 'x + 2.718281828459045'
237237
```
238238

239-
For this reason `mini_lambda` provides a `Constant()` method with alias `C()` to define a constant and assign it with a symbol.
239+
For this reason `mini_lambda` provides a `Constant()` method with aliases `C()` and `make_lambda_friendly()` to define a constant and assign it with a symbol.
240240

241241
```python
242242
from mini_lambda import x, _, C
@@ -357,7 +357,7 @@ str(expr) # 'DataFrame(X).max().values[0]'
357357

358358
### Anything
359359

360-
Actually the `Constant()` (alias `C()`) 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.
360+
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.
361361

362362
```python
363363
from mini_lambda import _, C, X

mini_lambda/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from mini_lambda.goodies_generated import *
66
from mini_lambda.main import *
77
from mini_lambda.main import _
8+
from mini_lambda.generated2 import *
89
from mini_lambda.goodies import *
910

1011
# allow users to do
1112
# import mini_lambda as v
12-
__all__ = ['base', 'generated', 'goodies_generated', 'main', 'goodies']
13+
__all__ = ['base', 'generated', 'goodies_generated', 'main', 'generated2', 'goodies']

0 commit comments

Comments
 (0)