A gnarly little python loop

Peter Otten __peter__ at web.de
Sun Nov 11 10:54:33 CET 2012

Paul Rubin wrote:

> Cameron Simpson <cs at zip.com.au> writes:
>> | I'd prefer the original code ten times over this inaccessible beast.
>> Me too.
> Me, I like the itertools version better.  There's one chunk of data
> that goes through a succession of transforms each of which
> is very straightforward.

[Steve Howell]
>     def get_tweets(term, get_page):
>         page_nums = itertools.count(1)
>         pages = itertools.imap(api.getSearch, page_nums)
>         valid_pages = itertools.takewhile(bool, pages)
>         tweets = itertools.chain.from_iterable(valid_pages)
>         return tweets

But did you spot the bug(s)?
My itertools-based version would look like this

    def get_tweets(term):
        pages = (api.GetSearch(term, pageno)
                 for pageno in itertools.count(1))
        for page in itertools.takewhile(bool, pages):
            yield from page

but I can understand that it's not everybody's cup of tea.

More information about the Python-list mailing list