Aha: how to "interrupt" a generator

20 views
Skip to first unread message

Edward K. Ream

unread,
Apr 6, 2022, 3:33:23 PM4/6/22
to leo-editor
This post proposes the answer to a question I've had for years, namely, how to see what a generator is doing "while the generator is doing it" :-)

A long time ago, in a thread far away (no idea where), I asked Guido this question and he was stumped.  Iirc, we were both looking for the answer somewhere like in python's itertools module.

But during the recent rewrite of the TokenOrderGenerator class (in leoAst.py), I suddenly saw a dirt simple answer. If I want to "capture" and "restart" a generator, just do something like this:

captured_list = list(yield whatever)  # or "yield from"
yield from captured_list

To be clear, this is for debugging only. Calling list negates most of the advantages of having a generator, but for debugging we don't care.

I haven't tested this idea, but it smells like the solution.

What do you think?

Edward

tbp1...@gmail.com

unread,
Apr 6, 2022, 3:52:03 PM4/6/22
to leo-editor
There are a number of interesting possibilities in Resetting generator object in Python.  Some of them:

"You can now use more_itertools.seekable (a third-party tool) which enables resetting iterators.

Install via > pip install more_itertools

import more_itertools as mit y = mit.seekable(FunctionWithYield()) for x in y: print(x) y.seek(0) # reset iterator for x in y: print(x)"

"You can define a function that returns your generator
def f(): def FunctionWithYield(generator_args): code here... return FunctionWithYield

Now you can just do as many times as you like:

for x in f()(generator_args): print(x) for x in f()(generator_args): print(x)"

Edward K. Ream

unread,
Apr 6, 2022, 5:40:32 PM4/6/22
to leo-editor
On Wed, Apr 6, 2022 at 2:52 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
There are a number of interesting possibilities in Resetting generator object in Python.  Some of them:

"You can now use more_itertools.seekable (a third-party tool) which enables resetting iterators.

Thanks for this. I think this is akin to the solution I just mentioned.

Edward
Reply all
Reply to author
Forward
0 new messages