Nested generators is the python equivalent of unix pipe cmds.

Steven D. Majewski sdm7g at Virginia.EDU
Fri Aug 3 14:06:28 EDT 2001


On Fri, 3 Aug 2001, Tim Hochberg wrote:

> "Steven D. Majewski" <sdm7g at Virginia.EDU> wrote in message>
> 
> # This is MUCH nicer than using os.path.walk() with a callback!
> >
> > ##  print the first 20 gif files I can find in your cwd...
> > for f in Count(Test( Files('.'), isGif ), 20 ): print f
> 
> Very cool!
> 
> 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 think you're probably right about swapping the arg order making it
more readable, Tim. 

Those generators, or something similar, are probably good candidates
for a standard generator utility module. 

The Files generator could also take multiple tests, so they wouldn't
have to be nested, and maybe also default paramater args for some
common things like file-extension, glob-patterns, dates, etc. 
( Basically the sort of stuff in the unix 'find' command. 
  Long ago, I recall someone doing a python equivalent of unix 'file'
  matching 'magic numbers' -- that might be a nice addition. ) 


-- Steve 





More information about the Python-list mailing list