0

I am using python with a mysql db. I have a table in that db storing names of functions to be run.
I would like to also add a possibility to store variables in the table, retrieve them on the fly and pass them into the function. However when stored in the db, the arguments are strings, and I don't know how to convert them to variable names to pass to the function.

For instance lets consider the following example where the table contains:

module        function      arguments  (<--columns names)
indicators    scrape_web    datapoints   

Let's say that I retrieve the data with a mysql lib and save it in a dict such as:

script:{'module': 'indicators',
        'function': 'scrape_web',
        'arguments': 'datapoints'}

I can retrieve the module and function name, and run the function without arguments like that :

module = __import__(script['module'])
func = getattr(module, script['function'])
func()

But what if i want to pass the arguments into the function so that the function run func(datapoints)

func(script['arguments'])

Obviously doesn't work.

All help greatly appreciated.

6
  • Are you trying to call func('datapoints') or func(datapoints) (where datapoints is a variable containing some data)? Commented Mar 28, 2017 at 16:45
  • 1
    What namespace is datapoints supposed to be found in? Commented Mar 28, 2017 at 16:45
  • 1
    @Rocket Hazmat as mentionned in the question , func(datapoints) Commented Mar 28, 2017 at 16:46
  • @user2357112 in the namespace of the script running the function func Commented Mar 28, 2017 at 16:47
  • If the args are in the calling code's global namespace then you can access them by name with, eg globals()['datapoints'] Commented Mar 28, 2017 at 16:58

1 Answer 1

3

Assuming script['arguments'] returns a string which is the name of a variable.

func(eval(script['arguments']))

For multiple arguments split the string using script['arguments'].split(",") and use eval for each element in the list.

If you want this to be as generic as possible:

args = script(['arguments']).split(",")
fstr = 'func('
for i in range(0,len(args)) : fstr +='eval(args['+str(i)+']),'
fstr = fstr[:-1]
#to remove the trailing comma
fstr += ')'
eval(fstr)
Sign up to request clarification or add additional context in comments.

7 Comments

that is great thx - what if the function takes multiple arguments? For instance if script:{'module': 'indicators', 'function': 'scrape_web', 'arguments': 'datapoints, 25'} ?
Use script['arguments'].split(",") to get a list of the arguments and call eval for each argument
@jimbasquiat I updated my answer to incorporate your comment
I have been trying func([eval(x) for x in script['arguments'].split(",")]) but i receive the error message: missing 1 required positional argument. Any what is the expression i should actually use?
@jimbasquiat [eval(x) for x in script['arguments'].split(",")] will return a list so it will be 1 argument. Either your function should take a list or you should pass the arguments at the respective positions. args= script['arguments'].splti(",") func(eval(args[0]) , eval(args[1]) .. etc)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.