Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'chunk': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'subject:python': 0.11; 'inaccessible': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'simpson': 0.16; 'wrote:': 0.17; 'skip:i 40': 0.17; 'yield': 0.17; "i'd": 0.22; 'paul': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'writes:': 0.29; 'code': 0.31; 'goes': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'too.': 0.35; 'received:org': 0.36; 'but': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'page': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'times': 0.63; 'succession': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: A gnarly little python loop Date: Sun, 11 Nov 2012 10:54:33 +0100 Organization: None References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bc6c.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352627686 news.xs4all.nl 6955 [2001:888:2000:d::a6]:58208 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33125 Paul Rubin wrote: > Cameron Simpson 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.