框架:机器人,语言: Python-3.7.1 熟练程度:新手
我具有以下核心方法,该方法可以在匹配时替换动态位置,并在我现有的自动化脚本中广泛使用。
def querybuilder(self, str, symbol, *args):
count = str.count(symbol)
str = list(str)
i = 0
j = 0
if (count == (len(args))):
while (i < len(str)):
if (str[i] == symbol):
str[i] = args[j]
j = j + 1
i = i + 1
else:
return ("Number of passed arguments are either more or lesser than required.")
return ''.join(str)
如果参数的发送方式如下所示,则效果很好
def querybuilder("~test~123", "~", "foo","boo")
但是,如果我想以列表的形式发送可选参数,则它将每个列表/元组/数组作为一个参数,因此如果条件不进入。
例如:-
i = ["foo", "boo"] -- Optional argument consider it as ('foo, boo',)
显然,由于现有框架的广泛使用,由于-ve影响,我无法对方法(querybuilder)进行任何更改。
我试图摆脱的原因:-
1, ", ".join("{}".format(a) for a in i,
2, tuple(i),
3, list(i),
4, numpy.array(i) part of import numpy
有没有可能按照要求转换参数的解决方案?
答案 0 :(得分:2)
以*['foo', 'boo']
的身份传递您的列表
def querybuilder(s, symbol, *args):
count = s.count(symbol)
st = list(s)
i = 0
j = 0
if (count == (len(args))):
while (i < len(st)):
if (st[i] == symbol):
st[i] = args[j]
j = j + 1
i = i + 1
else:
return ("Number of passed arguments are either more or lesser than required.")
return ''.join(st)
print(querybuilder("~test~123", "~", "foo","boo"))
# footestboo123
print(querybuilder("~test~123", "~", *["foo","boo"]))
# footestboo123