-
Notifications
You must be signed in to change notification settings - Fork 38
Lambda functions can be defined in config #374
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Lambda functions can be defined in config #374
Conversation
I have a very similar changeset on my machine, which I never propose because of the dangers of fdef = 'x**2 + 3'
f = lambda x: eval(fdef, locals()) I think this would severely limit the harm that can be done in the lambda function body. But then I never figured out a good way to specify the function arguments. |
I'm not sure there's anyway to resolve #318 without using e: just saw your edit, we can go with that if you prefer it. Can you explain how it limits the damage |
It's true (at the moment) that arbitrary functions can be run from config files, and that's another issue. But I wasn't thinking of malicious code There is a way to build lambdas programmatically, but it requires work: >>> lambda_args = ['x', 'y']
>>> lambda_body = 'x**2 + y + 1'
>>>
>>> import ast
>>> args = ast.arguments(posonlyargs=[],
... args=[ast.arg(arg) for arg in lambda_args],
... kwonlyargs=[],
... kw_defaults=[],
... defaults=[])
>>> body = ast.parse(lambda_body, mode='eval').body
>>> expr = ast.Expression(ast.Lambda(args, body))
>>> expr.line_no = 1 # from YAML
>>> expr.col_offset = 1 # from YAML
>>> expr = ast.fix_missing_locations(expr)
>>> code = compile(expr, filename='<ast>', mode='eval')
>>> func = eval(code, {}) # lambda works in a vacuum
>>> func(2, 3)
8 And this still leaves the problem of how to obtain the list of arguments and the function body from the YAML. Edit for your edit about my edit: If the eval() is the body of the lambda, then I believe it can only contain a single expression which cannot be an import statements and such. |
"And this still leaves the problem of how to obtain the list of arguments and the function body from the YAML." Something like this should work
|
We want the lambdas to produce callables and not values, in order to define parametrisations such as in the examples in a flexible manner. Since there is no good way to use YAML intrinsics to make this easier, I think the
Since there's a
In addition to all of this, we will also need to export symbols such as |
On further thought there's even a whole other infrastructure dimension to this, because the lambda's should have access to certain other parts of the pipeline, such as parameters. @JonathanDHarris I will review this now and then we will iterate later on. |
Co-authored-by: Nicolas Tessore <n.tessore@gmail.com>
Description
Allow users to define lambda functions in their configs. Resolve's #318
Checklist