reiter: decorator to make generators reiterable
I think this decorator sould be included in itertools: from functools import wraps class ReIter(object): __slots__ = 'f', 'args', 'kwargs' def __init__(self,f,args,kwargs): self.f = f self.args = args self.kwargs = kwargs def __iter__(self): return self.f(*self.args, **self.kwargs) def reiter(f): @wraps(f) def _reiter(*args,**kwargs): return ReIter(f,args,kwargs) return _reiter Using this you can iterate over the return value of a generator decorated with reiter as often as you like (and not just once): @reiter def gen(x,y): for i in xrange(x): yield i*y g = gen(5,3) for x in g: sys.stdout.write('%r\n' % x) sys.stdout.write('\n') for x in g: sys.stdout.write('%r\n' % x) The difference to tee is that old values are not remembered but the generator is evaluated when __iter__() is called. This might come in handy when you implement methods like items(), values() and keys() in a custom dict implementation. Or is there already such a thing and I missed it? -panzi PS: Maybe it should be called regen/ReGen instead of reiter/ReIter? PPS: Funny thing, both "Reiter" and "Regen" are german words. Reiter = rider or tab; Regen = rain.
Mathias Panzenböck
I think this decorator sould be included in itertools:
from functools import wraps
class ReIter(object):
...
Or is there already such a thing and I missed it?
-panzi
It looks pretty cool to me. I'll probably include it in my personal project, and it would be cool if it'll be added to itertools. (Along with many other things that should be added to itertools.) Ram.
It looks pretty cool to me. I'll probably include it in my personal project, and it would be cool if it'll be added to itertools. (Along with many other things that should be added to itertools.)
Ram.
There are a collection of recipes in the docs. I think there has been a discussion about making them available in the stdlib, but I’m not sure. If you want to add things to itertools, open feature requests on the bug tracker or launch a discussion thread here first. Regards
On Sun, Jun 13, 2010 at 11:39 AM, Éric Araujo
It looks pretty cool to me. I'll probably include it in my personal project, and it would be cool if it'll be added to itertools. (Along with many other things that should be added to itertools.)
Ram.
There are a collection of recipes in the docs. I think there has been a discussion about making them available in the stdlib, but I’m not sure. If you want to add things to itertools, open feature requests on the bug tracker or launch a discussion thread here first.
This would be much appreciated. Geremy Condra
participants (4)
-
geremy condra
-
Mathias Panzenböck
-
Ram Rachum
-
Éric Araujo