Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'api.': 0.04; 'chunk': 0.07; 'api': 0.09; "%s'": 0.09; 'page)': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:python': 0.11; '%d,': 0.16; '1):': 0.16; 'inaccessible': 0.16; 'itertools': 0.16; 'simpson': 0.16; 'simulate': 0.16; 'wrote:': 0.17; 'skip:i 40': 0.17; "i'd": 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'paul': 0.24; 'testing': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'writes:': 0.29; 'code': 0.31; 'print': 0.32; 'goes': 0.33; '11,': 0.33; 'version:': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'too.': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'test': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'page': 0.38; 'received:209.85.214': 0.39; 'first': 0.61; 'improved': 0.62; 'times': 0.63; 'here': 0.65; 'otten': 0.84; 'received:209.85.214.184': 0.84; 'received:mail- ob0-f184.google.com': 0.84; 'succession': 0.84 Newsgroups: comp.lang.python Date: Sun, 11 Nov 2012 09:16:06 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=199.27.178.37; posting-account=siT9MAoAAABdTBQh0iQ6NHgnerF0yiR- References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 199.27.178.37 MIME-Version: 1.0 Subject: Re: A gnarly little python loop From: Steve Howell To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352654170 news.xs4all.nl 6931 [2001:888:2000:d::a6]:58113 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33133 On Sunday, November 11, 2012 1:54:46 AM UTC-8, Peter Otten wrote: > 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 first version was sketching out the technique, and I don't have handy access to the API. Here is an improved version: def get_tweets(term): def get_page(page): return getSearch(term, page) page_nums = itertools.count(1) pages = itertools.imap(get_page, page_nums) valid_pages = itertools.takewhile(bool, pages) tweets = itertools.chain.from_iterable(valid_pages) return tweets for tweet in get_tweets("foo"): process(tweet) This is what I used to test it: def getSearch(term = "foo", page = 1): # simulate api for testing if page < 5: return [ 'page %d, tweet A for term %s' % (page, term), 'page %d, tweet B for term %s' % (page, term), ] else: return None def process(tweet): print tweet