Python generator generator: does not return a new value after submit

This is a rather strange question, so I will explain:

I have a generator like this that acts as a generator interface for an IRC server:

def irc_iter(): # not the real code, simplified
    msgs = get_msgs()
    for msg in msgs:
        if is_ping(msg):
            pong()
        else:
            to_send = yield msg
            for s in to_send:
                send(s)

This, theoretically, should allow me to do something cool, for example:

server = connect()
for line in server:
       if should_respond(line):
           server.send('WOW SUCH MESSAGE')

However, there is a hitch: generator.sendalso gives the following meaning. This means that it server.sendalso gives me the following message ... which I would prefer to process like all other messages received as line.

I know that I can fix this ugly by simply yielding the value of spam after receiving the send, but I try to make my code elegant, and vice versa. Is there any way to tell the generator that I do not want a new value?

Thank.

+3
1

, , , .send(None) ( for) .send(response).

, for .send() .next(), - ( continue pep342?) (, .send() .next()). , , :

server = connect()
response = None 
try:
    while True:
        line = server.send(response)
        response = None
        if should_respond(line):
            response ='WOW SUCH MESSAGE'
except StopIteration:
    pass
0

All Articles