通过命令行将值传递给Python类 [英] Pass in a value into Python Class through command line
问题描述
我有一些代码可以从命令行将变量传递到脚本中。我可以为 var
arg传递任何值到 function
中。问题是当我将 function
放到一个类中时,变量不会被读取到 function
中。脚本为:
I have got some code to pass in a variable into a script from the command line. I can pass any value into function
for the var
arg. The problem is that when I put function
into a class the variable doesn't get read into function
. The script is:
import sys, os
def function(var):
print var
class function_call(object):
def __init__(self, sysArgs):
try:
self.function = None
self.args = []
self.modulePath = sysArgs[0]
self.moduleDir, tail = os.path.split(self.modulePath)
self.moduleName, ext = os.path.splitext(tail)
__import__(self.moduleName)
self.module = sys.modules[self.moduleName]
if len(sysArgs) > 1:
self.functionName = sysArgs[1]
self.function = self.module.__dict__[self.functionName]
self.args = sysArgs[2:]
except Exception, e:
sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e))
def execute(self):
try:
if self.function:
self.function(*self.args)
except Exception, e:
sys.stderr.write("%s %s\n" % ("PythonCall#execute", e))
if __name__=="__main__":
function_call(sys.argv).execute()
这可以通过输入来实现。 < arg1 arg2 ....>
。问题是我想选择一个我想在类中的函数,而不仅仅是一个函数本身。我尝试过的代码是相同的,只是 function(var):
在一个类中。我希望就如何修改我的 function_call
类接受一些想法。
This works by entering ./function <function> <arg1 arg2 ....>
. The problem is that I want to to select the function I want that is in a class rather than just a function by itself. The code I have tried is the same except that function(var):
is in a class. I was hoping for some ideas on how to modify my function_call
class to accept this.
如果我想通过在 Hello
值中,我像这样运行脚本- python function_call.py函数Hello
。然后将 var
变量打印为 Hello
。
If i want to pass in the value Hello
I run the script like so -- python function_call.py function Hello
. This then prints the var
variable as Hello
.
通过在命令行中输入变量,我可以在整个代码中使用该变量。如果脚本是一堆函数,我可以使用此代码选择函数,但我想在特定类中选择函数。代替 python function.py function hello
,我也可以输入类。 python function.py函数你好
。
By entering the variable into the command lines I can then use this variable throughout the code. If the script was a bunch of functions I could just select the function using this code but I would like to select the functions inside a particular class. Instead of python function.py function hello
I could enter the class in as well eg. python function.py A function hello
.
另外,我遇到了在保存值以供外部使用时遇到的问题功能。如果有人能解决这个问题,我将非常感激。
_________________________________________________________________________________
Also I have encounter that I have problem's saving the value for use outside the function. If anyone could solve this I would appreciate it very much.
_________________________________________________________________________________
修订代码。
class A:
def __init__(self):
self.project = sys.argv[2]
def run(self, *sysArgs):
pass
def funct(self):
print self.project
class function_call(object):
def __init__(self, sysArgs):
try:
self.function = None
self.args = []
self.modulePath = sysArgs[0]
self.moduleDir, tail = os.path.split(self.modulePath)
self.moduleName, ext = os.path.splitext(tail)
__import__(self.moduleName)
self.module = sys.modules[self.moduleName]
if len(sysArgs) > 1:
self.functionName = sysArgs[1]
self.function = getattr(A(), sysArgs[1])(*sysArgs[2:])
self.args = sysArgs[2:]
except Exception, e:
sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e))
def execute(self):
try:
if self.function:
self.function(*self.args)
except Exception, e:
sys.stderr.write("%s %s\n" % ("PythonCall#execute", e))
if __name__=="__main__":
function_call(sys.argv).execute()
inst_A = A()
inst_A.funct()
感谢所有帮助。
推荐答案
您可能会找到 getattr
有用:
you might find getattr
useful:
>>> argv = ['function.py', 'run', 'Hello']
>>> class A:
def run(self, *args):
print(*args)
>>> getattr(A(), argv[1])(*argv[2:])
Hello
这篇关于通过命令行将值传递给Python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!