A gnarly little python loop

rusi rustompmody at gmail.com
Mon Nov 12 16:21:49 CET 2012

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) { ... }

Putting it into coroutine form, it becomes something like the
following [Untested since I dont have the API]. Clearly the
fidgetiness is there as before and now with extra coroutine plumbing

def genStage(term, target):
  page = 1
  while 1:
        r = api.GetSearch(term="foo", page=page)
        if not r:        break
        for tweet in r:  target.send(tweet)
        page += 1

def endStage():
    while True:     process((yield))

if __name__ == '__main__':
    genStage("foo", endStage())

More information about the Python-list mailing list