JonB
JonB

Reputation: 814

Regex and returning two lists as a tuple

I have this function, that I would like to see if can be done more pythonic. The function explains it self what it is trying to achieve.

My concern is that I'm using two regex expressions for content and expected that gives room for error creeping in, best would be if these two variables could use the same regex.

input example:

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

Upvotes: 1

Views: 101

Answers (1)

wildwilhelm
wildwilhelm

Reputation: 5019

You can match both groups at the same time:

def custom_steps(self, test_names):
    regex = 'tests\["(.*)(?::|"\]).* = (.+)(?::|;)'
    for match in re.finditer(regex, test_names):
        content, expected = match.groups()
        if '===' in expected:
            expected = expected[expected.index('===') + 4:]
        else:
            expected = 'true'
        yield content, expected

This gives you a generator over pairs of content, expected:

for c, e in custom_steps(None, test_names):
    print c, e

Output:

Status code 200
Schema validator GetChecksumReleaseEventForAll true

Upvotes: 1

Related Questions