Generator woes

Discussion in 'Python' started by Erich, Mar 13, 2008.

  1. Erich

    Erich Guest

    Hi all,

    I am trying to get the following generator to work to these goals:

    1. When it recieves an exception (via a throw()) it yeilds the value
    of handler.remaining. Otherwise it yeilds None.
    2. Send adds data to the generator.

    Goal 2 is working great. Goal 1 on the other hand, is not working. The
    result of a throw is always None.

    Any reasons why this does not work as I expect? If not, what is wrong?

    Code:
    def make_handler():
    def handler():
    eol = '\r\n'

    handler.remaining = 1
    response = ''
    data = ''

    while not response.endswith(eol):
    trv = None
    try:
    ndata = (yield trv)
    if ndata: response += ndata
    trv = None
    except:
    trv = handler.remaining
    response = response.strip()
    yield response * 2
    res = handler()
    res.next()
    return res

    x = make_handler()
    y = x.send('a')
    print 'y (should be None):',y
    y = x.send('b')
    print 'y (should be None):',y
    y = x.throw(Exception)
    print 'y (should be 1):',y
    y = x.send('c\r\n')
    print 'y (should be abcabc):',y

    Output:
    y (should be None): None
    y (should be None): None
    y (should be 1): None
    y (should be abcabc): abcabc

    Thanks,
    Erich
     
    Erich, Mar 13, 2008
    #1
    1. Advertising

  2. Erich

    Erich Guest

    On Mar 13, 12:33 am, Erich <> wrote:
    > Hi all,
    >
    > I am trying to get the following generator to work to these goals:
    >
    > 1. When it recieves an exception (via a throw()) it yeilds the value
    > of handler.remaining. Otherwise it yeilds None.
    > 2. Send adds data to the generator.
    >
    > Goal 2 is working great. Goal 1 on the other hand, is not working. The
    > result of a throw is always None.
    >
    > Any reasons why this does not work as I expect? If not, what is wrong?
    >
    > Code:
    > def make_handler():
    > def handler():
    > eol = '\r\n'
    >
    > handler.remaining = 1
    > response = ''
    > data = ''
    >
    > while not response.endswith(eol):
    > trv = None
    > try:
    > ndata = (yield trv)
    > if ndata: response += ndata
    > trv = None
    > except:
    > trv = handler.remaining
    > response = response.strip()
    > yield response * 2
    > res = handler()
    > res.next()
    > return res
    >
    > x = make_handler()
    > y = x.send('a')
    > print 'y (should be None):',y
    > y = x.send('b')
    > print 'y (should be None):',y
    > y = x.throw(Exception)
    > print 'y (should be 1):',y
    > y = x.send('c\r\n')
    > print 'y (should be abcabc):',y
    >
    > Output:
    > y (should be None): None
    > y (should be None): None
    > y (should be 1): None
    > y (should be abcabc): abcabc
    >
    > Thanks,
    > Erich


    Never mind its obviously a case of "I need to look foolish before I
    can see the simple error". Im going to blush and hide now.

    Erich
     
    Erich, Mar 13, 2008
    #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. Martin Maurer
    Replies:
    3
    Views:
    4,947
    Peter
    Apr 19, 2006
  2. TheDustbustr
    Replies:
    1
    Views:
    474
    Sami Hangaslammi
    Jul 25, 2003
  3. Replies:
    9
    Views:
    567
  4. Chris Withers

    Problems with email.Generator.Generator

    Chris Withers, Sep 11, 2006, in forum: Python
    Replies:
    20
    Views:
    1,729
    Max M
    Sep 12, 2006
  5. Terry Reedy

    Generator functions subclass generator?

    Terry Reedy, Jun 18, 2009, in forum: Python
    Replies:
    0
    Views:
    481
    Terry Reedy
    Jun 18, 2009
Loading...

Share This Page