Python Forum
poplib - parsing message body, could somebody please help explain this code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
poplib - parsing message body, could somebody please help explain this code
#1
Hi all, 
We have a python program that works with the ibm watson transcriber to transcribe voicemail and send to appropriate staff. 
It stopped working on 9/22, for seemingly no reason. 

I've tracked down the problem line by printing/logging lines and objects until I found the issue. 
The error I'm getting is that "index is out of range", presumably a list is causing the problem. Specifically, the line where the below function tries to obtain the message UID. 

So basically there's a for loop which takes the number of new messages in the mailbox, and loops through each one to process each email, like this:

for messageNum in range(numberofMessages):
    try:
        processMessage(messagegNum+1)
and this is the function processMessage():
def processMessage(messageNum):
    #assemble message contents
    raw_message = popServer.retr(messageNum)[1]
    str_message = email.message_from_bytes(b'\n'.join(raw_message))
    body = str(str_message.get_payload()[0])

    messageUID = str(popServer.uidl(messageNum))
    messageUID = re.findall('UID\d+-\d+,messageUID,0)[0]   
 


The last line is what I believe is causing the error, but I'm not completely understanding what's going on with the regular expression line, and also why the functions have the [0]'s after them. Forgive me I am still learning. I understand they are lists but I can't exactly figure out what's going wrong here. 

Any input is, of course, appreciated.
Reply
#2
You can find the docs for re.findall to see that it returns a list of all the different matches.  [0] gives element zero, or the first element of the list.  If there are no elements, you get an IndexError.

>>> re.findall("b", "bobby") # returns all matches. There are three.
['b', 'b', 'b']
>>> re.findall("b", "bobby")[0] # picks the first of all the matches
'b'
>>> re.findall("x", "bobby")[0]  # tries to return the first of all matches, but with no matches, this is an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
The messageUID value probably doesn't have the UID string in the expected format.
Reply
#3
Thank you for the response... 
This all happened after we migrated the mailbox from one service provider to another, so I'm guessing the format in which the UID string is passed changed. 

When I print the contents of 'messageUID' currently I'm getting:
b' +OK 1 3'
and when I issue popServer.uidl() to get a list of all uid's in the mailbox, I get:
(b' +OK', [b'1 3', b'2 5', b'3 11, b'4 19', ..... b'74 267'], 556)
So I'm confused what the point of 'UID\d+-\d+

is in the last line of my code. I suspect this to be the problem area.
I created a test script to issue the same commands on the old mail server and the return from that re.findall line is like this:

(b' +OK', [b'1 UID38-1726373849', b'2 UID39-28288393', b3 UID40-2883839'], 15746)

So I just need to figure out how to modify that first portion of the last line to accomodate for the change in format. 

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Explain the python code in this definition Led_Zeppelin 1 736 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,001 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,084 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,117 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,234 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Could you explain each part of the code? Tsushida 2 1,497 Mar-20-2022, 08:19 AM
Last Post: Larz60+
  What is the run time complexity of this code and please explain? samlee916 2 2,287 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  Outlook Emails & HTML Table in Message Body JoeDainton123 1 11,359 Sep-02-2020, 05:15 AM
Last Post: buran
  Error Message Coming Up When Running Code eddywinch82 0 1,881 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  Explain range in this code RavCOder 4 2,313 Oct-02-2019, 05:04 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020