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


Groups > comp.lang.python > #33108

Re: A gnarly little python loop

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'empty,': 0.09; 'page)': 0.09; 'def': 0.10; 'subject:python': 0.11; 'sat,': 0.15; 'apis.': 0.16; 'cleaner': 0.16; 'roy': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'trying': 0.21; "i'd": 0.22; 'this:': 0.23; "i've": 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; "i'm": 0.29; 'asking': 0.32; 'to:addr:python- list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'page': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'more': 0.63; 'smith': 0.71; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=JylL/9yDmw5gjS/aqABd51ref661kfA1Y7aMp+aiR7k=; b=b2tpaDTV76DzLwqvx922tRGjN2FITJwecICxPClmrFRVjOamoRqvjJ2SvXskMtXY+V ri/4auFD3VJrO75150y0idAK5AtdeFKL2xpWOJe2cTnGyLDjtj8g+9srubvnWcTMUNAq lB2PKD3cZgAmj1XkykpK2lzqNNcdnwDFJRcUT3c1uPVPa4Fb9FKgoSpfOAzsOOPSOjrJ l5az7tdfybrGL4uq2ECqnM5TZ7EvJvd5vXcDs5dtG/0Q6tLjTMppxy8CSbq1kjxEhRq+ pDtmflp2OrqH9YgnkmE8YLir6yqo2qvzLD1UYFLzVG9ny0t3rGgEvm+/1uGw3wDTXJIB 6Knw==
MIME-Version 1.0
In-Reply-To <roy-9EBEAD.17581410112012@news.panix.com>
References <roy-9EBEAD.17581410112012@news.panix.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 10 Nov 2012 16:17:08 -0700
Subject Re: A gnarly little python loop
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3546.1352589460.27098.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352589460 news.xs4all.nl 6945 [2001:888:2000:d::a6]:39095
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33108

Show key headers only | View raw


On Sat, Nov 10, 2012 at 3:58 PM, Roy Smith <roy@panix.com> wrote:
> I'm trying to pull down tweets with one of the many twitter APIs.  The
> particular one I'm using (python-twitter), has a call:
>
> data = api.GetSearch(term="foo", page=page)
>
> The way it works, you start with page=1.  It returns a list of tweets.
> If the list is empty, there are no more tweets.  If the list is not
> empty, you can try to get more tweets by asking for page=2, page=3, etc.
> I've got:
>
>     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'd do something like this:

def get_tweets(term):
    for page in itertools.count(1):
        r = api.GetSearch(term, page)
        if not r:
            break
        for tweet in r:
            yield tweet

for tweet in get_tweets("foo"):
    process(tweet)

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


Thread

A gnarly little python loop Roy Smith <roy@panix.com> - 2012-11-10 17:58 -0500
  Re: A gnarly little python loop Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-10 16:17 -0700
  Re: A gnarly little python loop Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 00:23 +0000
  Re: A gnarly little python loop Steve Howell <showell@domaintools.com> - 2012-11-10 19:03 -0800
    Re: A gnarly little python loop Stefan Behnel <stefan_ml@behnel.de> - 2012-11-11 08:56 +0100
  Re: A gnarly little python loop rusi <rustompmody@gmail.com> - 2012-11-11 23:09 -0800
    Re: A gnarly little python loop rusi <rustompmody@gmail.com> - 2012-11-12 07:21 -0800
      Re: A gnarly little python loop Peter Otten <__peter__@web.de> - 2012-11-12 16:49 +0100
      Re: A gnarly little python loop Steve Howell <showell30@yahoo.com> - 2012-11-12 08:09 -0800
        Re: A gnarly little python loop rusi <rustompmody@gmail.com> - 2012-11-12 20:14 -0800

csiph-web