我有这个功能,我想看看是否可以做更多的pythonic。 该功能自我解释了它想要实现的目标。
我担心的是我正在为content
和expected
使用两个正则表达式,为错误爬行提供了空间,最好是这两个变量可以使用相同的正则表达式。
输入示例:
test_names = "tests[\"Status code: \" +responseCode.code] = responseCode.code === 200;\ntests[\"Schema validator GetChecksumReleaseEventForAll\"] = tv4.validate(data, schema);"
def custom_steps(self, test_names):
""" Extracts unique tests from postman collection """
content = re.findall(r'(?<=tests\[")(.*)(?::|"\])', test_names)
expected = re.findall(r'(?<=\] = )(.*)(?::|;)', test_names)
for i, er in enumerate(expected):
if "===" in er:
expected[i] = er[er.find('===')+4:]
else:
expected[i] = "true"
return content, expected
答案 0 :(得分:1)
您可以同时匹配两个组:
content
这为您提供了一对expected
,for c, e in custom_steps(None, test_names):
print c, e
:
Status code 200
Schema validator GetChecksumReleaseEventForAll true
输出:
$.ajax({
url: 'controller/myaction',
dataType: "json",
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify({
myKey: myArray
}),
success: function(data) { /* Whatever */ }
});