I see. I guess the ambiguity would stem from trying to force match objects into
the sequence protocol even though the custom __getitem__() means that they're
essentially a mixed mapping:
Mapping[int | str, str | None]
If we avoid any sort of "smart" length derived from only mo.groups() or
mo.groupdict(), there's nothing stopping match objects from acting as proper
mappings. We would need __iter__() which would simply yield all the available
keys, including group 0 and all the named groups, and __len__() which would
return the total number of keys.
My point is that the match object doesn't need to masquerade as something else
to be useful, just implement the protocol to describe the available keys.
m = re.match(r"(a) (?P<foo>b)(x)?", "a b")
list(m) # [0, 1, 2, 3, 'foo']
dict(m) # {0: 'a b', 1: 'a', 2: 'b', 3: None, 'foo': 'b'}
This means that pattern matching with mapping patterns would work
automatically. The first example I shared would look like this:
match re.match(r"(v|f) (\d+) (\d+) (\d+)", line):
case {1: "v", 2: x, 3: y, 4: z}:
print("Handle vertex")
case {1: "f", 2: a, 3: b, 4: c}:
print("Handle face")
The second example would work without any changes:
match re.match(r"(?P<number>\d+)|(?P<add>+)|(?P<mul>*)", line):
case {"number": str(value)}:
return Token(type="number", value=int(value))
case {"add": str()}:
return Token(type="add")
case {"mul": str()}:
return Token(type="mul")
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/FUB4CD2DKPJV5QVKZEFJ6XBAFKIP4EZ6/
Code of Conduct: http://python.org/psf/codeofconduct/