Need help writing coroutine

Discussion in 'Python' started by Matthew Wilson, Nov 7, 2007.

  1. I'm working on two coroutines -- one iterates through a huge stream, and
    emits chunks in pieces. The other routine takes each chunk, then scores
    it as good or bad and passes that score back to the original routine, so
    it can make a copy of the stream with the score appended on.

    I have the code working, but it just looks really ugly. Here's a vastly
    simplified version. One function yields some numbers, and the other
    function tells me if they are even or odd.

    def parser():
    "I just parse and wait for feedback."
    for i in 1, 2, 3, 4, 5:
    score = (yield i)
    if score:
    print "%d passed!" % i

    def is_odd(n):
    "I evaluate each number n, and return True if I like it."
    if n and n % 2: return True

    def m():
    try:
    number_generator = parser()
    i = None
    while 1:
    i = number_generator.send(is_odd(i))
    except StopIteration: pass

    and here's the results when I run this:
    In [90]: m()
    1 passed!
    3 passed!
    5 passed!

    So, clearly, the code works. But it is nonintuitive for the casual
    reader.

    I don't like the while 1 construct, I don't like manually
    trapping the StopIteration exception, and this line is really ugly:

    i = number_generator.send(is_odd(i))

    I really like the old for i in parser(): deal, but I can't figure out
    how to use .send(...) with that.

    Can anyone help me pretty this up? I want to make this as intuitive as
    possible.

    TIA

    Matt
     
    Matthew Wilson, Nov 7, 2007
    #1
    1. Advertising

  2. Matthew Wilson

    Paul Hankin Guest

    Matthew Wilson wrote:
    > I'm working on two coroutines -- one iterates through a huge stream, and
    > emits chunks in pieces. The other routine takes each chunk, then scores
    > it as good or bad and passes that score back to the original routine, so
    > it can make a copy of the stream with the score appended on.
    >
    > I have the code working, but it just looks really ugly. Here's a vastly
    > simplified version. One function yields some numbers, and the other
    > function tells me if they are even or odd.
    >
    > def parser():
    > "I just parse and wait for feedback."
    > for i in 1, 2, 3, 4, 5:
    > score = (yield i)
    > if score:
    > print "%d passed!" % i
    >
    > def is_odd(n):
    > "I evaluate each number n, and return True if I like it."
    > if n and n % 2: return True
    >
    > def m():
    > try:
    > number_generator = parser()
    > i = None
    > while 1:
    > i = number_generator.send(is_odd(i))
    > except StopIteration: pass
    >
    > and here's the results when I run this:
    > In [90]: m()
    > 1 passed!
    > 3 passed!
    > 5 passed!
    >
    > So, clearly, the code works. But it is nonintuitive for the casual
    > reader.
    >
    > I don't like the while 1 construct, I don't like manually
    > trapping the StopIteration exception, and this line is really ugly:
    >
    > i = number_generator.send(is_odd(i))
    >
    > I really like the old for i in parser(): deal, but I can't figure out
    > how to use .send(...) with that.
    >
    > Can anyone help me pretty this up? I want to make this as intuitive as
    > possible.


    Why use coroutines?

    def parser(score):
    for i in xrange(1, 6):
    yield i
    if score(i):
    print "%d passed!" % i

    def is_odd(n):
    return n % 2

    def m():
    for i in parser(is_odd):
    # Presumably do something here...
    pass

    --
    Paul Hankin
     
    Paul Hankin, Nov 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Bennett

    need help reading/writing binary

    Steve Bennett, Aug 25, 2004, in forum: Perl
    Replies:
    1
    Views:
    510
    Joe Smith
    Aug 25, 2004
  2. John Knoop

    Need help writing special user control

    John Knoop, Feb 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    346
    Natty Gur
    Feb 23, 2004
  3. HNguyen
    Replies:
    4
    Views:
    2,440
    HNguyen
    Dec 21, 2004
  4. Carlos Ribeiro
    Replies:
    3
    Views:
    357
    Carlos Ribeiro
    Sep 26, 2004
  5. Phillip B Oldham

    Most "active" coroutine library project?

    Phillip B Oldham, Aug 23, 2009, in forum: Python
    Replies:
    46
    Views:
    1,617
    Denis
    Oct 9, 2009
Loading...

Share This Page