parse icon indicating copy to clipboard operation
parse copied to clipboard

Iterate over results

Open jacobtomlinson opened this issue 5 years ago • 4 comments

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()

jacobtomlinson avatar May 24 '19 15:05 jacobtomlinson

What does this return if you've matched both positional and named items?

r1chardj0n3s avatar Jul 16 '19 22:07 r1chardj0n3s

I wasn't aware you could mix and match named and positional?

jacobtomlinson avatar Jul 17 '19 08:07 jacobtomlinson

Ah! Indeed both format() and parse() support mixing named and positional values.

r1chardj0n3s avatar Jul 17 '19 10:07 r1chardj0n3s

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.

jacobtomlinson avatar Jul 18 '19 12:07 jacobtomlinson

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.

wimglenn avatar Nov 09 '23 04:11 wimglenn