Iterator class to allow self-restarting generator expressions?

J

John O'Hagan

Inspired by some recent threads here about using classes to extend the
behaviour of iterators, I'm trying to replace some some top-level functions
aimed at doing such things with a class.

So far it's got a test for emptiness, a non-consuming peek-ahead method, and
an extended next() which can return slices as well as the normal mode, but
one thing I'm having a little trouble with is getting generator expressions
to restart when exhausted. This code works for generator functions:

class Regen(object):
"""Optionally restart generator functions"""
def __init__(self, generator, options=None, restart=False):
self.gen = generator
self.options = options
self.gen_call = generator(options)
self.restart = restart

def __iter__(self):
return (self)

def next(self):
try:
return self.gen_call.next()
except StopIteration:
if self.restart:
self.gen_call = self.generator(self.options)
return self.gen_call.next()
else:
raise

used like this:

def gen():
for i in range(3):
yield i

reg = Regen(gen, restart=True)

I'd like to do the same for generator expressions, something like:

genexp = (i for i in range(3))

regenexp = Regen(genexp, restart=True)

such that regenexp would behave like reg, i.e. restart when exhausted (and
would only raise StopIteration if it's actually empty). However because
generator expressions aren't callable, the above approach won't work.

I suppose I could convert expressions to functions like:

def gen():
genexp = (i for i in range(3))
for j in genexp:
yield j

but that seems tautological.

Any clues or comments appreciated.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top