我正在与一个相对复杂的程序进行交互,并在其上放置一个GUI来发出命令。我做了一个简单的例子,我认为可以说明问题。我知道发出命令需要传递哪些参数,但是我只能将它们作为字符串获取。
我动态获得了数百个具有不同返回值的命令,但这是一个示例,当我传递命令ID时可以取回
def get_command_vars(commandID = None):
if commandID == '0x4F':
return [['mode', '16bits'], ['seconds', '8bits']]
def issueMyCommand(commandID = None):
commandParameters = get_command_vars(command=0x4F)
commandParameters告诉我此命令的参数是mode和seconds,但它们是字符串
commandParm_1 = commandParameters[0][0] # 'mode'
commandParm_2 = commandParameters[1][0] # 'seconds'
>get User Input from the gui to pass to issuetheCommand
input1 = getinputEntry1() # this is the 'mode' value, e.g., 8
input2 = getinputEntry2() # this is the 'seconds' value, e.g., 14
我有要从用户输入传递的值,但是我不知道如何将它们传递给函数,因为我只有变量作为字符串,即“ mode”和“ seconds”
c = issueTheCommand(mode = input1, seconds = input2)
该命令格式将根据get_command_vars中的参数类型而改变,因此它可能是'count','datalength','milliseconds,'delay'等。
@sberry-实际上,用户输入值是将通过mode和seconds传递的值。16位和8位在这里并没有真正发挥作用。我正在尝试执行此操作,而不更改“ issueTheCommand”函数期望的格式(如果可能)。我发出它的方式现在看起来像这样:c = issueTheCommand(mode = 8,seconds = 14)。我不认为这会成文吗?
如果我对您的问题的评论是您所追求的,那么也许可以使用字典作为关键字参数对您有用。
>>> def issueTheCommand(mode, seconds):
... print mode, seconds
...
>>> issueTheCommand(**{"mode": "16bits", "seconds": "8bits"})
16bits 8bits
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句