[Python-ideas] Integrate some itertools into the Python syntax

Ethan Furman ethan at stoneleaf.us
Tue Mar 22 14:41:15 EDT 2016


On 03/22/2016 10:51 AM, Michel Desmoulin wrote:

> def foo(p):
>      with open(p) as f:
>          def begin:
>              return x == "BEGIN SECTION"
>           def end:
>              return x == "STOP"
>          return f[begin, end][:10000]
>
> It's very clean, very convenient, very natural, and memory efficient.

Except the 10,000 limit doesn't happen until /after/ the end block is 
reached -- which could be a million lines later.


> Now compare it with itertools:
>
> from itertools import takewhile, dropwhile, islice
>
> def foo(p):
>      with open(p) as f:
>          def begin:
>              return x != "BEGIN SECTION"
>      def end:
>          return x != "STOP"
>      return islice(takewhile(end, dropwhile(begin, f)), 0, 10000)
>
> It's ugly, hard to read, hard to write.

Yes, it is.  I would make a function:

   def take_between(begin=0, end=None, limit=None):
       """
       return a list with all items between `begin` and `end`, but no 
more than `limit`

       begin -> an `int`, or function that returns `True` on first line 
to keep
       end -> None (for no end), an `int` relative to start of iterable, 
or a function that returns True on last item to keep
       limit -> max number of items to keep, or None for all items
       """

> In Python, you are always iterating on something, it makes sense to make
> sure we have the best tooling to do at our fingertips.

In Python, you are always using data containers -- yet we don't have a 
plethora of different tree types built in.

A simple import is "at our fingertips".

On a more supportive note: Are you aware of `more_itertools`[1] ?  If it 
doesn't already have `takeuntil` and `dropuntil` and your (pretty cool) 
`iter`, perhaps you could get your ideas included there.

--
~Ethan~


[1] https://pypi.python.org/pypi/more-itertools/


More information about the Python-ideas mailing list