Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8032
| From | Joel <joel.welling@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Generator Frustration |
| Date | 2011-06-20 15:04 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <08751bba-1216-4ee9-8a02-c14d21322213@a31g2000vbt.googlegroups.com> (permalink) |
| References | <4dea7932$0$28716$607ed4bc@cv.net> |
On Jun 4, 2:27 pm, "TommyVee" <xxxxx...@xxxxxx.xxx> wrote:
> 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. The problem of
> course, is that once a yield gets put into a function, the function is now a
> generator and its behavior changes. 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.
>
> Thanks in advance.
I actually found a reasonable answer to this, I think. If one of the
called functions contains a yield, that function is by definition a
generator, and will test as such with 'if
type(result)==types.GeneratorType:'. This makes it possible for the
function that calls the subroutine to either do its own yield, or to
yield the result of the function's next 'yield' statement. I've got a
class the run method of which calls the 'cycle' method of its derived
class, as follows:
while True:
result= self.cycle(now()) # result may or may not be a
generator
if type(result)==types.GeneratorType:
# Next is a generator, meaning it includes a
'yield'.
# Otherwise, result should be None and cycle is a
simple
# function.
try:
yield result.next()
while True:
yield result.next()
except StopIteration:
pass
else:
# self.cycle() was a simple function, returning None-
it's
# done now.
pass
# It is time for this process to go back to sleep; all the
yields
# in self.cycle() have been processed.
yield hold,self,self.nextCycleTime-now()
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
Re: Generator Frustration Joel <joel.welling@gmail.com> - 2011-06-20 15:04 -0700
Re: Generator Frustration Terry Reedy <tjreedy@udel.edu> - 2011-06-20 21:42 -0400
Re: Generator Frustration Joel <joel.welling@gmail.com> - 2011-06-20 20:03 -0700
csiph-web