Iterator class to allow self-restarting generator expressions?

Discussion in 'Python' started by John O'Hagan, Mar 1, 2009.

  1. John O'Hagan

    John O'Hagan Guest

    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
    John O'Hagan, Mar 1, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    578
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    592
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    399
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    372
    falcon
    Jul 31, 2005
  5. Replies:
    6
    Views:
    111
    Vivek
    Jan 25, 2006
Loading...

Share This Page