2

I have several functions in the database as a strings and there is a main function that should take these functions from the database and execute them as functions, and not as text.

How I can transform a string (which describes a function) into a function with arguments and execute it in Python?

I'm tried exec but I can't return something from function and use it.

For example my function:

some_function = """
def my_random_function(some_param):
    print(some_param)
    return time.time()
"""
Nataly Firstova
  • 661
  • 1
  • 5
  • 12
  • Hey, @Nataly Firstova, try this: https://stackoverflow.com/questions/7719466/i-have-a-string-whose-content-is-a-function-name-how-to-refer-to-the-correspond – Rutangaba Nov 19 '21 at 13:32
  • Function defined by `exec` can work like normal function, and of course you can also get the return values. Make sure you have import all the essential libraries (`time` in your example). PS: I will post some code for you in answer – tandat Nov 19 '21 at 13:35

2 Answers2

2

100% Never do this ever under any circumstances disclaimer...

the exec statement:

code = """
def f(x):
    x = x + 1
    return x

print('This is my output.')
print(f(my_var))
"""

exec(code, {}, {"my_var": 1})

output:

This is my output.
2
testfile
  • 2,145
  • 1
  • 12
  • 31
  • 100% Never do this ever under any circumstances disclaimer... - why? I just want to put all functions in DB, and change it without server reload – Nataly Firstova Nov 19 '21 at 13:42
  • It is very insecure and unreliable. See here for someone wanting to do this in PHP: https://stackoverflow.com/a/14653229/6014330 – testfile Nov 19 '21 at 13:46
  • Maybe you know better way for it? I want to add end edit my functions in DB (via frontend editor) and execute it in main function on server (without redeploy) – Nataly Firstova Nov 19 '21 at 13:48
  • That's exactly the problem. what you want to do is inherently dangerous. Its like asking to log into your bank account without a username and password. You cant do it securely. – testfile Nov 19 '21 at 13:55
  • The whole point of CI/CD is so that you can make changes, test them, validate it etc then promote to an environment and test again... You should not be able to change code on the fly without a deployment. – testfile Nov 19 '21 at 13:56
  • But how FaaS services like amazon lambda works? I can write python function in web interface and execute it. – Nataly Firstova Nov 19 '21 at 14:03
  • Lambda stores code in an encrypted S3 bucket. Amazon is also worth $46 billion and have a few thousand security engineers making sure its all secure. – testfile Nov 19 '21 at 14:06
  • I mean functionally, how it works for them, how text from a web-editor turns into a python function – Nataly Firstova Nov 19 '21 at 14:08
  • There will be cases we need to run code from the front end. Amazon Lambda is an example, and another example is an online IDE. But make sure that the code will not threaten your server, or more clearly will not change how your server works. For example, Amazon Lambda will isolate your code and run it on some virtual environment rather than run directly inside Amazon Lambda code. – tandat Nov 19 '21 at 14:13
0

Function defined by exec can work like normal function, and of course you can also get the return values. Make sure you have import all the essential libraries (time in your example)

import time

some_function = """
def my_random_function(some_param):
    print(some_param)
    return time.time()
"""

exec(some_function)
return_value = my_random_function(3)
print(return_value)

output

3
1637328869.3345668
tandat
  • 255
  • 1
  • 9