Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33122

Re: A gnarly little python loop

Date 2012-11-11 19:48 +1100
From Cameron Simpson <cs@zip.com.au>
Subject Re: A gnarly little python loop
References <k7nlmo$1v6$2@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3555.1352623728.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 11Nov2012 08:56, Stefan Behnel <stefan_ml@behnel.de> wrote:
| Steve Howell, 11.11.2012 04:03:
| > On Nov 10, 2:58 pm, Roy Smith <r...@panix.com> wrote:
| >>     page = 1
| >>     while 1:
| >>         r = api.GetSearch(term="foo", page=page)
| >>         if not r:
| >>             break
| >>         for tweet in r:
| >>             process(tweet)
| >>         page += 1
| >>
| >> It works, but it seems excessively fidgety.  Is there some cleaner way
| >> to refactor this?
| > 
| > I think your code is perfectly readable and clean, but you can flatten
| > it like so:
| > 
| >     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
| 
| I'd prefer the original code ten times over this inaccessible beast.

Me too.
-- 
Cameron Simpson <cs@zip.com.au>

In an insane society, the sane man must appear insane.
        - Keith A. Schauer <keith@balrog.dseg.ti.com>

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: A gnarly little python loop Cameron Simpson <cs@zip.com.au> - 2012-11-11 19:48 +1100
  Re: A gnarly little python loop Paul Rubin <no.email@nospam.invalid> - 2012-11-11 01:09 -0800
    Re: A gnarly little python loop Peter Otten <__peter__@web.de> - 2012-11-11 10:54 +0100
      Re: A gnarly little python loop Steve Howell <showell30@yahoo.com> - 2012-11-11 09:16 -0800
      Re: A gnarly little python loop Steve Howell <showell30@yahoo.com> - 2012-11-11 09:16 -0800
    Re: A gnarly little python loop Steve Howell <showell30@yahoo.com> - 2012-11-11 09:29 -0800
      Re: A gnarly little python loop Peter Otten <__peter__@web.de> - 2012-11-11 19:34 +0100
        Re: A gnarly little python loop Steve Howell <showell30@yahoo.com> - 2012-11-11 11:16 -0800
          Re: A gnarly little python loop Cameron Simpson <cs@zip.com.au> - 2012-11-12 11:43 +1100
            Re: A gnarly little python loop Steve Howell <showell30@yahoo.com> - 2012-11-11 17:38 -0800
        Re: A gnarly little python loop Roy Smith <roy@panix.com> - 2012-11-11 14:23 -0500

csiph-web