Re: itertools, functools, file enhancement ideas

2007-04-10 Thread Paul Rubin
"Klaas" <[EMAIL PROTECTED]> writes: > Still don't see much advantage over writing a lambda (except perhaps > speed). Well, it's partly a matter of avoiding boilerplate, especially with the lambdaphobia that many Python users seem to have. -- http://mail.python.org/mailman/listinfo/python-list

Re: itertools, functools, file enhancement ideas

2007-04-10 Thread Klaas
On Apr 8, 9:34 am, Paul Rubin wrote: > [EMAIL PROTECTED] writes: > > > a) def flip(f): return lambda x,y: f(y,x) > > Curious resemblance to: > >itemgetter(1,0) > > Not sure I understand that. I think he read it as lambda (x, y): (y, x) More interesting would

Re: itertools, functools, file enhancement ideas

2007-04-08 Thread Paul Rubin
[EMAIL PROTECTED] writes: > >for block in f.iterchars(n=1024): ... > for block in iter(partial(f.read, 1024), ''): ... Hmm, nice. I keep forgetting about that feature of iter. It also came up in a response to my queue example from another post. > > a) def flip(f): return lambda x

Re: itertools, functools, file enhancement ideas

2007-04-08 Thread rdhettinger
[Paul Rubin] > 1. File iterator for blocks of chars: > >f = open('foo') >for block in f.iterchars(n=1024): ... for block in iter(partial(f.read, 1024), ''): ... > iterates through 1024-character blocks from the file. The default iterator > a) def flip(f): return lambda x

Re: itertools, functools, file enhancement ideas

2007-04-08 Thread Alexander Schmolck
[EMAIL PROTECTED] (Alex Martelli) writes: > > 4. functools enhancements (Haskell-inspired): > >Let f be a function with 2 inputs. Then: > > a) def flip(f): return lambda x,y: f(y,x) > > b) def lsect(x,f): return partial(f,x) > > c) def rsect(f,x): return partial(flip(f), x)

Re: itertools, functools, file enhancement ideas

2007-04-07 Thread Paul Rubin
[EMAIL PROTECTED] (Alex Martelli) writes: > >for line in file_lines(filename): > >crunch(line) > > I'm +/-0 on this one vs the idioms: > with open(filename) as f: > for line in f: crunch(line) > Making two lines into one is a weak use case for a stdlib function. Well, t

Re: itertools, functools, file enhancement ideas

2007-04-07 Thread Alex Martelli
Paul Rubin wrote: > I just had to write some programs that crunched a lot of large files, > both text and binary. As I use iterators more I find myself wishing > for some maybe-obvious enhancements: > > 1. File iterator for blocks of chars: > >f = open('foo')

Re: itertools, functools, file enhancement ideas

2007-04-07 Thread Paul Rubin
Paul Rubin writes: > # loop through all the files crunching all lines in each one > for line in (ichain(file_lines(x) for x in all_filenames)): > crunch(x) supposed to say crunch(line) of course. -- http://mail.python.org/mailman/listinfo/python-lis

itertools, functools, file enhancement ideas

2007-04-07 Thread Paul Rubin
I just had to write some programs that crunched a lot of large files, both text and binary. As I use iterators more I find myself wishing for some maybe-obvious enhancements: 1. File iterator for blocks of chars: f = open('foo') for block in f.iterchars(n=1024): ... iterates thro