[英]Wrapper function that reads arguments from file and then passes to subroutines in python3
I have a wrapper function that runs multiple subroutines.我有一个运行多个子例程的包装器 function。 Instead defining the wrapper function to take hundreds of inputs that would pass to each subroutine, I would like the wrapper function to read an input file containing all the arguments to be passed to each subroutine.
而是定义包装器 function 以获取将传递给每个子例程的数百个输入,我希望包装器 function 读取包含所有 ZDBC11CAA5BDA99F77E6FB4DABD882E7 的输入文件传递给每个子例程
For example, given an input file called eg_input.py
:例如,给定一个名为
eg_input.py
的输入文件:
## example of argument file
a = [1]
b = [2]
c = [3]
d = [4]
e = [5]
f = [6]
I'd like to have a wrapper function that looks something like this:我想要一个看起来像这样的包装器 function:
def wrap_fun(argument_file):
## read in the argument file
exec(open(argument_file).read())
## run first sub routine
sub_fun1(fun1_parma1=a, fun1_param2=b, fun1_param3=c)
## run second sub routine
sub_fun2(fun2_param1=d, fun2_param2=e, fun2_param3=f)
## run third sub routine
## note some arguments are shared between subroutines
sub_fun2(fun3_param1=a, fun3_param2=f)
## etc...
return()
such that I could run a script like:这样我就可以运行如下脚本:
wrap_fun(eg_input.py)
When I run code similar to the above, however, the arguments are not available to the subroutines.但是,当我运行与上述类似的代码时,子程序无法使用 arguments。
You'll note I used the exec()
function in the above example but I am not adamant about using it, ie if there is a pythonic way to accomplish this I would prefer that solution.你会注意到我在上面的例子中使用了
exec()
function 但我并不坚持使用它,即如果有一个pythonic方法来完成这个我更喜欢那个解决方案。
My motivation for this question is the ability to easily produce a variety of input files (again with each input file containing 100s of arguments) and observing how changing each argument affects the results of wrap_fun()
.我对这个问题的动机是能够轻松生成各种输入文件(同样每个输入文件包含 100 个参数)并观察更改每个参数如何影响
wrap_fun()
的结果。
Thanks!谢谢!
You can supply a dictionary for global variables in exec
.您可以在
exec
中为全局变量提供字典。 Any globals defined in the executed code end up there.在执行的代码中定义的任何全局变量都到了那里。 Now, when you call your functions, read the values from that dict.
现在,当您调用函数时,请从该字典中读取值。 Since class instances already have a dict, you could write the wrapper as a class that updates class instances from the file and then you can write any number of instance methods to run your code.
由于 class 实例已经有一个字典,您可以将包装器编写为 class 以更新文件中的 class 实例,然后您可以编写任意数量的实例方法来运行您的代码。
# functions to call
def sub_fun1(fun1_param1="not"):
print("fun", fun1_param1)
def sub_fun2(fun2_param1="not"):
print("too", fun2_param1)
# option 1: use a dict
def wrap_fun(argument_file):
ns = {}
exec(open(argument_file).read(), ns)
print(ns.keys())
sub_fun1(fun1_param1=ns["a"])
# option 2: use instance dict
class FunRunner:
def __init__(self, argument_file):
exec(open(argument_file).read(), self.__dict__)
def have_some_fun(self):
sub_fun1(fun1_param1=self.a)
def have_more_fun(self):
sub_fun2(fun2_param1=self.b)
# write test file
fn = "my_test_args"
open(fn,"w").write("""a = [1]
b = [2]
c = [3]
d = [4]
e = [5]
f = [6]""")
# option1
wrap_fun(fn)
# option2 one shot
FunRunner(fn).have_some_fun()
# option2 multiple
fun = FunRunner(fn)
fun.have_some_fun()
fun.have_more_fun()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.