Nested generators is the python equivalent of unix pipe cmds.
Steven D. Majewski
sdm7g at Virginia.EDU
Fri Aug 3 17:00:25 EDT 2001
... and if you define an __or__ method ( "|" operator), you can make it
look more like a real unix pipeline :
class Gen:
def __init__( self, generator ):
self.generator = generator
def __iter__( self ):
return self.generator
class Genpipe(Gen):
def Test( self, pred ):
self.generator = Test( self.generator, pred )
return self
def Count( self, n ):
self.generator = Count( self.generator, n )
return self
def __or__( self, other ):
if callable(other):
self.generator = Test( self.generator, other )
return self
# not an all-the-options-complete implementation!
>>> for x in Genpipe(Files('.')).Count( 100 ) | isGif :
... print x
I find the separation by space-operator-space to make it more readable,
but I think that overriding of the __or__ operator might be a bit
confusing -- If anything an __and__ ( "&" ) or a shift ">>" might
make a but more logical sense.
Perhaps "+" should be concatenation of generators:
gen1 + gen2 + gen3
means do gen1 until empty, then gen2, ...
"|" could be alternation: one from column (generator) A, one from column B
We need one for the generator equivalent of 'zip' :
genA <op> genB
generates (A0,B0), (A1,B1), (A2,B2), ...
-- Steve
More information about the Python-list
mailing list