itertools, functools, file enhancement ideas

Paul Rubin http
Sun Apr 8 02:16:29 CEST 2007


aleax at mac.com (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, the inspiration is being able to use the iterator in another
genexp:

       for line in (ichain(file_lines(x) for x in all_filenames)):
          crunch(line)

so it's making more than two lines into one, and just flows more
naturally, like the perl idiom "while(<>) {...}".

> >    lsect and rsect allow making what Haskell calls "sections".  Example:
> >       # sequence of all squares less than 100
> >       from operator import lt
> >       s100 = takewhile(rsect(lt, 100), (x*x for x in count()))
> 
> Looks like they'd be useful, but I'm not sure about limiting them to
> working with 2-argument functions only.

I'm not sure how to generalize them but if there's an obvious correct
way to do it, that sounds great ;).

Also forgot to include the obvious:

    def compose(f,g):  
       return lambda(*args,**kw): f(g(*args,**kw))




More information about the Python-list mailing list