A gnarly little python loop
rusi
rustompmody at gmail.com
Mon Nov 12 23:14:04 EST 2012
On Nov 12, 9:09 pm, Steve Howell <showel... at yahoo.com> wrote:
> On Nov 12, 7:21 am, rusi <rustompm... at gmail.com> wrote:
>
> > On Nov 12, 12:09 pm, rusi <rustompm... at gmail.com> wrote:> This is a classic 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=getchar()!= EOF) { ... }
> > [...]
>
> There are actually three fidgety things going on:
>
> 1. The API is 1-based instead of 0-based.
> 2. You don't know the number of pages in advance.
> 3. You want to process tweets, not pages of tweets.
>
> Here's yet another take on the problem:
>
> # wrap fidgety 1-based api
> def search(i):
> return api.GetSearch("foo", i+1)
>
> paged_tweets = (search(i) for i in count())
>
> # handle sentinel
> paged_tweets = iter(paged_tweets.next, [])
>
> # flatten pages
> tweets = chain.from_iterable(paged_tweets)
> for tweet in tweets:
> 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 = (api.GetSearch("foo", i) for i in count(1))
{Peter]
> >>> while ((c=getchar()!= EOF) { ... }
for c in iter(getchar, EOF):
...
Thanks. Learnt something
More information about the Python-list
mailing list