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: 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: References: From: Ian Kelly Date: Sat, 10 Nov 2012 16:17:08 -0700 Subject: Re: A gnarly little python loop To: Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sat, Nov 10, 2012 at 3:58 PM, Roy Smith 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)