Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7081

Re: Generator Frustration

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.tota-refugium.de!.POSTED!news.glglgl.de!not-for-mail
From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: Generator Frustration
Date Mon, 06 Jun 2011 11:07:53 +0200
Organization A newly installed InterNetNews server
Lines 49
Message-ID <isi5dk$8h1$1@r03.glglgl.eu> (permalink)
References <4dea7932$0$28716$607ed4bc@cv.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace tota-refugium.de 1307351455 12447 eJwFwYEBwCAIA7CXYJZaz0GE/09YEovO2mAQMTEPeLcP7pemnVRWq5lZtmRtCLnhNHbJa+YHKNcRYQ== (6 Jun 2011 09:10:55 GMT)
X-Complaints-To abuse@news.tota-refugium.de
NNTP-Posting-Date Mon, 6 Jun 2011 09:10:55 +0000 (UTC)
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 Hamster/2.1.0.11
X-User-ID eJwFwQEBACAIA7BKoodjHEHfP4Kbr7BoIjzgcrWPt/ppE0TOnGDI7NIwoiBO7btkpypHHn4OeRCu
In-Reply-To <4dea7932$0$28716$607ed4bc@cv.net>
Cancel-Lock sha1:YX8ca6xgpOq4ybY0RvUrRvbrakI=
X-NNTP-Posting-Host eJwFwYEBwCAIA7CXxNLizgEm/59gQsjU4aKcw1lIULx2NeDqsNX/6fISdhxLakdbg1X5oR4M0hC8
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7081

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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