Message from discussion
A gnarly little python loop
Received: by 10.66.85.168 with SMTP id i8mr8509995paz.21.1352780044633;
Mon, 12 Nov 2012 20:14:04 -0800 (PST)
MIME-Version: 1.0
Received: by 10.68.240.103 with SMTP id vz7mr5901930pbc.10.1352780044617; Mon,
12 Nov 2012 20:14:04 -0800 (PST)
Path: s9ni5145pbb.0!nntp.google.com!kt20no7400233pbb.1!postnews.google.com!v9g2000pbi.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 12 Nov 2012 20:14:04 -0800 (PST)
Complaints-To: groups-abuse@google.com
Injection-Info: v9g2000pbi.googlegroups.com; posting-host=116.74.129.239; posting-account=mBpa7woAAAAGLEWUUKpmbxm-Quu5D8ui
NNTP-Posting-Host: 116.74.129.239
References: <roy-9EBEAD.17581410112012@news.panix.com> <a61c52b7-eb49-45a5-a4b4-8e4c6b4acaf1@v9g2000pbi.googlegroups.com>
<bf637b4d-c257-48af-bb3f-b5438f341c67@nl3g2000pbc.googlegroups.com> <c51bc296-f300-4f83-ac12-3f31217ba8fb@n2g2000pbp.googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0,gzip(gfe)
Message-ID: <bd59d069-1041-40b7-a8eb-30c2d9595467@v9g2000pbi.googlegroups.com>
Subject: Re: A gnarly little python loop
From: rusi <rustompm...@gmail.com>
Injection-Date: Tue, 13 Nov 2012 04:14:04 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Nov 12, 9:09=A0pm, Steve Howell <showel...@yahoo.com> wrote:
> On Nov 12, 7:21=A0am, rusi <rustompm...@gmail.com> wrote:
>
> > On Nov 12, 12:09=A0pm, rusi <rustompm...@gmail.com> wrote:> This is a c=
lassic problem -- structure clash of parallel loops
>
> > <rest snipped>
>
> > Sorry wrong solution :D
>
> > The fidgetiness is entirely due to python not allowing C-style loops
> > like these:
>
> > >> while ((c=3Dgetchar()!=3D EOF) { ... }
> > [...]
>
> There are actually three fidgety things going on:
>
> =A01. The API is 1-based instead of 0-based.
> =A02. You don't know the number of pages in advance.
> =A03. You want to process tweets, not pages of tweets.
>
> Here's yet another take on the problem:
>
> =A0 =A0 # wrap fidgety 1-based api
> =A0 =A0 def search(i):
> =A0 =A0 =A0 =A0 return api.GetSearch("foo", i+1)
>
> =A0 =A0 paged_tweets =3D (search(i) for i in count())
>
> =A0 =A0 # handle sentinel
> =A0 =A0 paged_tweets =3D iter(paged_tweets.next, [])
>
> =A0 =A0 # flatten pages
> =A0 =A0 tweets =3D chain.from_iterable(paged_tweets)
> =A0 =A0 for tweet in tweets:
> =A0 =A0 =A0 =A0 process(tweet)
[Steve Howell]
Nice on the whole -- thanks
Could not the 1-based-ness be dealt with by using count(1)?
ie use
paged_tweets =3D (api.GetSearch("foo", i) for i in count(1))
{Peter]
> >>> while ((c=3Dgetchar()!=3D EOF) { ... }
for c in iter(getchar, EOF):
...
Thanks. Learnt something