Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'chunk': 0.07; 'api': 0.09; 'collections': 0.09; 'page)': 0.09; '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; 'steve': 0.13; 'folks': 0.15; '1:09': 0.16; 'context:': 0.16; 'count,': 0.16; 'deque': 0.16; 'functools': 0.16; 'imap,': 0.16; 'inaccessible': 0.16; 'itertools': 0.16; 'literal,': 0.16; 'pages:': 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; "shouldn't": 0.17; 'skip:i 40': 0.17; 'version.': 0.17; 'thanks,': 0.18; 'import': 0.21; "i'd": 0.22; 'this:': 0.23; 'idea': 0.24; 'paul': 0.24; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'skip:@ 10': 0.27; 'header:X-Complaints-To:1': 0.28; 'assert': 0.29; 'paul.': 0.29; 'writes:': 0.29; 'class': 0.29; 'version,': 0.30; 'code': 0.31; 'print': 0.32; 'goes': 0.33; '11,': 0.33; 'version:': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'wrong': 0.34; 'nov': 0.35; 'process,': 0.35; 'too.': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'too': 0.36; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'page': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'times': 0.63; 'here': 0.65; 'results': 0.65; 'alternative.': 0.84; 'itertools,': 0.84; 'metaphor': 0.84; '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 19:34:06 +0100 Organization: None References: <7x4nkwzesu.fsf@ruckus.brouhaha.com> <8be50a3e-0ba6-439f-b445-7dedeacdc1c7@lg12g2000pbb.googlegroups.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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352658857 news.xs4all.nl 6868 [2001:888:2000:d::a6]:56153 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33139 Steve Howell wrote: > On Nov 11, 1:09 am, 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. > > Thanks, Paul. > > Even though I supplied the "inaccessible" itertools version, I can > understand why folks find it inaccessible. As I said to the OP, there > was nothing wrong with the original imperative approach; I was simply > providing an alternative. > > It took me a while to appreciate itertools, but the metaphor that > resonates with me is a Unix pipeline. It's just a metaphor, so folks > shouldn't be too literal, but the idea here is this: > > page_nums -> pages -> valid_pages -> tweets > > The transforms are this: > > page_nums -> pages: call API via imap > pages -> valid_pages: take while true > valid_pages -> tweets: use chain.from_iterable to flatten results > > Here's the code again for context: > > 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 > Actually you supplied the "accessible" itertools version. For reference, here's the inaccessible version: class api: """Twitter search API mock-up""" pages = [ ["a", "b", "c"], ["d", "e"], ] @staticmethod def GetSearch(term, page): assert term == "foo" assert page >= 1 if page > len(api.pages): return [] return api.pages[page-1] from collections import deque from functools import partial from itertools import chain, count, imap, takewhile def process(tweet): print tweet term = "foo" deque( imap( process, chain.from_iterable( takewhile(bool, imap(partial(api.GetSearch, term), count(1))))), maxlen=0) ;)