开发者

IndexError: list index out of range (in query results)

I am having problems understanding how to work with query results. I asked about half a dozen questions about this but I still do not understand. I copy from previous code and I make it work somehow but since I don't understand the underlying concept the code breaks down if I make a minor change. I would really appreciate if you could tell me how you visualize what is happenning here and explain it to me. Thank you.

class ReceiveEmail(InboundMailHandler):
    def receive(self, message):
        logging.info("Received email from %s" % message.sender)
        plaintext = message.bodies(content_type='text/plain')
        for text in plaintext:
            txtmsg = ""
            txtmsg = text[1].decode()
            logging.info("Body is %s" % txtmsg)
            logging.info("CC email is %s" % ((message.cc).split(",")[1]))            

        query = User.all()           
        query.filter("userEmail =",  ((message.cc).split(",")[1])) 
        results = query.fetch(1)                   

        for result in results:                     
            result.userScore += 1                  

        um = results[0]                            
        um.userScore = result.userScore            
        um.put()

In this code, as I understand it, the query takes the second email address from the cc list and fetches the result.

Then I increment the userScore by 1.

Next, I want to update this item in Datastore so I say

        um = results[0]                            
        um.userScore = result.userScore            
        um.put()

But this gives an index out of range error:

um = results[0]
IndexError: list index out of range

Why? I am imagining tha开发者_如何学Got results[0] is the zeroeth item of the results. Why is it out of range? Only thing I can think of is that, the list may be None. But I don't understand why. It must have the 1 item that was fetched.

Also, if I try to test for the first email address by changing the index from [1] to [0]

query.filter("userEmail =",  ((message.cc).split(",")[0]))

then I don't get the IndexError.

What am I doing wrong here?

Thanks!

EDIT

See comments:

(message.cc).split(",")[0]) 

left a space in front of the emails (starting with the second email), so the query was not matching them;

>>> cc.split(",")
['cc12@example.com', ' cc13@example.com', ' cc13@example.com']

adding a space after comma fixed the problem:

>>> listcc = cc.split(", ")
>>> listcc
['cc12@example.com', 'cc13@example.com', 'cc13@example.com']
>>> 


To understand the code break it down and look at it piece by piece:

class ReceiveEmail(InboundMailHandler):
    def receive(self, message):
        logging.info("Received email from %s" % message.sender)

        # Get a list of CC addresses.  This is basically a for loop.
        cc_addresses = [address.strip() for address in message.cc.split(",")]
        # The CC list goes with the message, not the bodies.
        logging.info("CC email is %s" % (cc_addresses))

        # Get and iterate over all of the *plain-text* bodies in the email.
        plaintext = message.bodies(content_type='text/plain')
        for text in plaintext:
            txtmsg = ""
            txtmsg = text[1].decode()
            logging.info("Body is %s" % txtmsg)

        # Setup a query object.
        query = User.all()
        # Filter the user objects to get only the emails in the CC list.
        query.filter("userEmail IN",  cc_addresses)
        # But, only get at most 10 users.
        users = query.fetch(10)

        logging.info('Got %d user entities from the datastore.' % len(users))

        # Iterate over each of the users increasing their score by one.
        for user in users:
            user.userScore += 1

        # Now, write the users back to the datastore.
        db.put(users)
        logging.info('Wrote %d user entities.' % len(users))

I would make an adjustment to your model structure. When you create the User entity, I would set the key_name to the email address. You will be able to make your queries much more efficient.

Some references:

  • List Comprehension.
  • Query Object.
  • db.put().
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜