[Python-Dev] RFC: generator combinator syntax

Steven D. Majewski sdm7g@Virginia.EDU
Fri, 3 Aug 2001 19:35:04 -0400 (EDT)


On Fri, 3 Aug 2001, Paul Prescod wrote:

> I propose:
> 
> [yield file for file in Files('.') 
>        if file.lower.find(.'gif')>=0]
> 
> By reusing the yield keyword we can get rid of the need for the new "|"
> or "&" syntax.
> 
> Any kind of combination that can be done with list comprehensions would
> be possible with generator comprehensions.

I assume you intend that "[yield file for..."  expression to evaluate
to a generator, not a list. 

I like it. 

I'm not sure that I want to get rid of the operator syntax. 
I'm still getting used to it. 

Either works a lot better than using filter: I was complaining 
in an earlier thread about having to explicitly ask for the
.next attrib. It just took a while to figure out how they are
meant to be used. 

Generators & Iterators really seem to open up functional programming
in Python. 

I'm trying to put together some more basic file processing functionals: 
for example, fileExt(ext) instead of isGif:

def fileExt(ext):
        return lambda s: os.path.splitext( s )[-1].lower() == ext.lower() 

Then isGif becomes:

isGif = fileExt( '.gif' )

and others like fileSize, owner, etc.

def fileSize( test ):
	return lambda s: test( os.stat(s)[stat.ST_SIZE] ) 


and folding the Genops class into the File function, I can 
do things like: 


for file in Files() & isGif & fileSize( lambda s : s > 1024 ): 
	process( file )


-- Steve