| Store | Cart

Suggesting a new feature - "Inverse Generators"

From: Terry Reedy <tjre...@udel.edu>
Fri, 25 Mar 2005 14:13:52 -0500
Terminology: To me and some (most?) others posting here and, I believe, 
both the docs and the common meaning of 'generator', a Python generator is 
the particular kind of iterator that produces multiple values on request 
and which is created by the generator function that you write.

Acceptors (consumers): The concept is known to both CS and Python 
developers.  According to Tim Peters, Python generators constitute 
'semi-coroutines'.  Any full coroutine mechanism (Stackless, for instance) 
will allow the reverse (not inverse, which would undo rather than 
complement).  For various reasons, the Python developers choose that data 
chains should be written as consumer code getting data from a generator, 
which might in turn get data from a generator.

In your example, opening and reading the lines of the data file could be 
done by filterjunk, not the main function, but I can see reasons both ways.

For your specific problem, you could, I believe, use an intermediate 
generator (which could also be combined with filterjunk) that combines 
lines into complete text records.  Something like (ignoring any fussy 
details left out):

def textrecord(file):
  trlines = []
  for line in filterjunk(file):
    trlines.append(line)
    if complete(trline): yield ''.join(trlines)
    # where 'complete' is code to determine if have all lines in record or 
not

Terry J. Reedy

Recent Messages in this Thread
Jordan Rastrick Mar 25, 2005 04:04 pm
Jordan Rastrick Mar 25, 2005 04:10 pm
Andrew Koenig Mar 25, 2005 04:18 pm
Jordan Rastrick Mar 25, 2005 04:36 pm
Andrew Koenig Mar 26, 2005 01:40 pm
Tim Hochberg Mar 25, 2005 04:25 pm
Michael Spencer Mar 25, 2005 04:46 pm
Jordan Rastrick Mar 25, 2005 05:23 pm
Michael Spencer Mar 25, 2005 06:41 pm
Serge Orlov Mar 25, 2005 07:14 pm
Jordan Rastrick Mar 26, 2005 06:56 am
Peter Otten Mar 26, 2005 08:04 am
Scott David Daniels Mar 25, 2005 07:34 pm
Michael Spencer Mar 25, 2005 08:07 pm
Scott David Daniels Mar 25, 2005 09:00 pm
Bengt Richter Mar 25, 2005 10:11 pm
Bengt Richter Mar 25, 2005 07:43 pm
Terry Reedy Mar 25, 2005 07:13 pm
Jordan Rastrick Mar 25, 2005 04:49 pm
Diez B. Roggisch Mar 25, 2005 06:13 pm
Jordan Rastrick Mar 26, 2005 04:11 pm
phil...@yahoo.com Mar 27, 2005 03:44 am
Oren Tirosh Mar 27, 2005 06:58 am
Messages in this thread