First of all, I'd like to mention that I am not particularly familiar with Python. I have recently been forced to familiarise myself with a code sample that's left my jaws ajar, and I have been unable to "translate" it. The various documents and articles I've taken a look at have not helped either:
- What does the "yield" keyword do?
- The Python yield keyword explained
- The yield statement
- Yield expressions
Here's a shortened-down version of the function in question:
@coroutine
def processMessage(receiver):
global userID
#...
while True:
msg = (yield)
try:
#...
except Exception as err:
#...
I cannot understand what it does and am thus not able to "walk through" the code. My questions are "What does this function do?" and "What sequences does this function follow?"
The line that throws me off is msg = (yield)
. I have no idea what it's trying to achieve. Intuition tells me it simply grabs new messages as they come through, but I fail to understand why. If anyone knows and if I have provided enough information, I'd really appreciate an explanation.
The Try
clause:
if msg['event'] == 'message' and 'text' in msg and msg['peer'] is not None:
if msg['sender']['username'] == username:
userID = msg['receiver']['peer_id']
config.read(fullpath + '/cfg/' + str(userID) + '.cfg')
if config.has_section(str(userID)):
log('Config found')
readConfig()
log('Config loaded')
else:
log('Config not found')
writeConfig()
log('New config created')
if 'username' in msg['sender']:
parse_text(msg['text'], msg['sender']['username'], msg['id'])
P.S. receiver
is a socket receiver.