Nested generators is the python equivalent of unix pipe cmds.

Terry Reedy tjreedy at home.com
Fri Aug 3 15:50:13 EDT 2001


"Tim Hochberg" <tim.hochberg at ieee.org> wrote in message
news:ssya7.78137$Cy.11439637 at news1.rdc1.az.home.com...
> Although I do think it would be easier to read if the order of the
test and
> the generator was reversed:
>
> for f in Count(20, Test(isGif,  Files('.'))): print f
>
> I may just have a weak mind, but on the first version, I lost track
of who
> the 20 belonged to by the time I got to the end.

I noticed the same.   A genpipe wrapping rclass would give us the very
clear:

for f in gen(Files('.')).Test(isGif).Count(20)

My untested idea for genpipe:

class genpipe:
  def __init__(self, generator):
    self.gen = generator #sanity check omitted
  def Test(self, pred):
    self.gen = _Test(self.gen, pred) # ditto for Count and other
filters
  def __iter__(self):
    return self.gen.next

Terry J. Reedy







More information about the Python-list mailing list