Part 1: Write python code that will execute a list of functions with supplied pa
ID: 3549211 • Letter: P
Question
Part 1:
Write python code that will execute a list of functions with supplied parameters and report the observed runtime for each function run. Assume that the input file has a list of strings like so:
Func1, Param1, Param2, Param3
Func2, Param1
You would need to execute Func1(Param1, Param2, Param3) and Func2(Param1) reporting the runtimes for each execution. You can assume that Func1 and Func2 are already defined.
For your reference (if your python is a little rusty) -- if you need to look up the function that has been defined, you can do it as follows in python. First line looks up the function and the second line executes it with a parameter of 10.
function1 = globals()['myFunction']
function1( 10 )
In order to make this work for more than one parameter, here is how you can do it. The first line looks up the function, second line converts the string into a tuple ('1, 2, 3' into (1, 2, 3)) and the third line allows you to pass multiple parameters to the function.
Here we are assuming that myFunction2 takes three parameters. If you do not use eval, the input is a single string and not the three values that we need. If you do not use the * in the third line below, the tuple (1,2,3) would be treated as a single input and not three separate parameters.
function2 = globals()['myFunction2']
params = eval("1, 2, 3")
function2( *params )
You do not have to worry about error checking for now; you can assume that functions are pre-defined and that the number of parameters provided is correct.
Be sure to test your code.
Part 2:
Explanation / Answer
The "print" statement does nothing to return a value from the function,
so the strings "this is *" will not be stored in your list. They will,
however, be printed if you are some how observing your program output
(e.g. running it in IDLE, or a command shell).
To save the "results" of the functions, you need to produce results,
which means actually using "return" to return some value. Here is an
example:
def a():
print "this is a"
return "return value from a"
def b():
print "this is b"
return "return value from b"
functions = [a, b]
results = [f() for f in functions]
print results
Here is the result of this example:
py> def a():
... print "this is a"
... return "return value from a"
...
py> def b():
... print "this is b"
... return "return value from b"
...
py> functions = [a, b]
py> results = [f() for f in functions]
this is a
this is b
py> print results
['return value from a', 'return value from b']
A fun, but unfortunately deprecated, way to do this is with the "apply"
function in conjunction with the "map" function:
def a():
print "this is a"
return "return value from a"
def b():
print "this is b"
return "return value from b"
functions = [a, b]
results = map(apply, functions)
print results
Here is this example at work:
py> def a():
... print "this is a"
... return "return value from a"
...
py> def b():
... print "this is b"
... return "return value from b"
...
py> functions = [a, b]
py> results = map(apply, functions)
this is a
this is b
py> print results
['return value from a', 'return value from b']