I want to pass function definition to a python command line script. What is the best way to do this? I am using python 2. Suppose i have a script like this:
#myscript.py
x = load_some_data()
my_function = load_function_definition_from_command_line()
print my_function(x)
And i want to call it like this: python myscript.py 'def fun(x): return len(x)'
How do i perform the load_function_definition_from_command_line
part ?
I imagine a workaround:
- get the string function definition from command line
- write it to a file with .py extension in some temp directory
- load the definition from file using solutions from this question: How to import a module given the full path?
- execute
- cleanup
But I am sure there must be a better way.