parse
parse copied to clipboard
Iterate over results
It's not possible to iterate over results. It would be great if we could add this.
As a workaround for now I've gone for
for group, value in result.named.items()
However it would be great if we could do
for group, value in result.items()
What does this return if you've matched both positional and named items?
I wasn't aware you could mix and match named and positional?
Ah! Indeed both format()
and parse()
support mixing named and positional values.
Ok I see the problem with my request then!
One solution could be to write an iterator with __iter__
and __next__
which iterates through both things and returns a two-tuple of key and value, like iterating over dict().items()
does. For named results the key would be the string name, in positional results the key would be the integer index? Mixing key types is not necessarily great but it would allow for group, value in result:
which is neat.
Otherwise adding an "iterating" section to the README with examples of for group, value in result.named.items():
and for value in result.fixed:
would be simpler.
The "fixed" results are already iterable as a side-effect of parse.Result
being indexable (i.e. implementing __getitem__
). I don't think any Pythonic API is possible to combine the tuple of result.fixed
with the dict of result.named
in a single iterator.