Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion A gnarly little python loop

Received: by 10.66.75.164 with SMTP id d4mr863839paw.10.1353060084516;
        Fri, 16 Nov 2012 02:01:24 -0800 (PST)
Path: 6ni89337pbd.1!nntp.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!novia!news-hub.siol.net!news1.t-com.hr!newsfeed.CARNet.hr!aioe.org!rt.uk.eu.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path: <python-python-l...@m.gmane.org>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@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-l...@python.org
From: Peter Otten <__pete...@web.de>
Subject: Re: A gnarly little python loop
Date: Sun, 11 Nov 2012 19:34:06 +0100
Organization: None
References: <k7nlmo$1v...@ger.gmane.org>
	<mailman.3555.1352623728.27098.python-l...@python.org>
	<7x4nkwzesu....@ruckus.brouhaha.com>
	<8be50a3e-0ba6-439f-b445-7dedeacdc...@lg12g2000pbb.googlegroups.com>
Mime-Version: 1.0
X-Gmane-NNTP-Posting-Host: p5084bc6c.dip.t-dialin.net
User-Agent: KNode/4.7.3
X-BeenThere: python-l...@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-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.3562.1352658857.27098.python-l...@python.org>
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: ab...@xs4all.nl
Bytes: 5681
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7Bit

Steve Howell wrote:

> On Nov 11, 1:09 am, Paul Rubin <no.em...@nospam.invalid> wrote:
>> Cameron Simpson <c...@zip.com.au> 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)

;)