Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7081
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Generator Frustration |
| Date | 2011-06-06 11:07 +0200 |
| Organization | A newly installed InterNetNews server |
| Message-ID | <isi5dk$8h1$1@r03.glglgl.eu> (permalink) |
| References | <4dea7932$0$28716$607ed4bc@cv.net> |
Am 04.06.2011 20:27 schrieb TommyVee:
> I'm using the SimPy package to run simulations. Anyone who's used this
> package knows that the way it simulates process concurrency is through
> the clever use of yield statements. Some of the code in my programs is
> very complex and contains several repeating sequences of yield
> statements. I want to combine these sequences into common functions.
Which are then generators.
> The problem of course, is that once a yield gets put into a function,
> the function is now a generator and its behavior changes.
Isn't your "main" function a generator as well?
> Is there any elegant way to do this? I suppose I can do things like
> ping-pong yield statements, but that solutions seems even uglier than
> having a very flat, single main routine with repeating sequences.
I'm not sure if I got it right, but I think you could emulate this
"yield from" with a decorator:
def subgen1(): yield 1; yield 2;
def subgen2(): yield 1; yield 2;
Instead of doing now
def allgen():
for i in subgen1(): yield i
for i in subgen2(): yield i
you as well could do:
def yield_from(f):
def wrapper(*a, **k):
for sub in f(*a, **k):
for i in sub:
yield i
return wrapper
@yield_from
def allgen():
yield subgen1()
yield subgen2()
(Untested.)
Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Generator Frustration "TommyVee" <xxxxxxxx@xxxxxx.xxx> - 2011-06-04 14:27 -0400
Re: Generator Frustration Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-05 00:56 +0000
Re: Generator Frustration Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-05 13:43 +1200
Re: Generator Frustration Jack Diederich <jackdied@gmail.com> - 2011-06-04 22:06 -0400
Re: Generator Frustration "TommyVee" <xxxxxxxx@xxxxxx.xxx> - 2011-06-05 20:11 -0400
Re: Generator Frustration Jan Decaluwe <jan@jandecaluwe.com> - 2011-06-05 11:52 +0200
Re: Generator Frustration Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-06-06 11:07 +0200
Re: Generator Frustration "TommyVee" <xxxxxxxx@xxxxxx.xxx> - 2011-06-07 20:41 -0400
csiph-web