可选参数约束

Optional argument constraint

框架:机器人,语言: 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")

但是如果我想作为一个列表来代替可选参数发送,它将每个 list/tuple/array 作为一个参数,因此不会进入 if 条件。

例如:-

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

任何可能的解决方案来根据要求转换参数?

将您的列表作为 *['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