-1

I have some code that connects directly to outlook and extracts metadata from an inbox then complies into a pandas. However, I seem to be getting an error which I haven't encountered before and I suspect it has to do with bad data in the outlook extraction (i.e. blank data on an email). But I can't seem to isolate. Has anyone seen this before?

ERROR:app:Exception on /ctplive [GET]
Traceback (most recent call last):
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\programdata\anaconda\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\programdata\anaconda\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\ADM\Code Projects\web_data_connector\outlook_api\app.py", line 100, in mailbox_insights
    inbox['Conversation Length'] = determine_conversation_length(inbox['ConversationIndex'], archive['ConversationIndex']).astype(int).values
  File "c:\users\adm\code projects\ccf_email_automation\ccf_email_automation\insights.py", line 92, in determine_conversation_length
    res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
  File "c:\users\adm\code projects\ccf_email_automation\ccf_email_automation\insights.py", line 92, in <listcomp>
    res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
TypeError: 'NoneType' object is not subscriptable

Here is the block from app.py

    if mailbox not in ['ctp', 'csu', 'dmt']:
        return 404
    archive, inbox = get_emails(mailbox, get_outlook())
    inbox['Sender'] = inbox[['SenderName', 'SenderEmailAddress']].apply(lambda x: read_email_address(*x), axis=1)
    inbox['Sender Domain'] = inbox['SenderEmailAddress'].apply(lambda x: x[x.index('@') + 1:] if '@' in x else 'gilead.com')
    inbox['TABL_Highest Volume Senders'] = top_senders(inbox)
    inbox['TABL_Highest Volume Sender Domains'] = top_sender_domains(inbox)
    *************line 100 starts here**************
    inbox['Conversation Length'] = determine_conversation_length(inbox['ConversationIndex'], archive['ConversationIndex']).astype(int).values
    inbox['TABL_Longest Conversations'] = longest_conversations(inbox['Conversation Length'])
    holiday_list = get_holidays()
    inbox['Business Day SLA'] = inbox[['Received Date', 'Flag Completed Date']].apply(lambda x: calculate_sla(*x, holiday_list=holiday_list), axis=1)
    inbox['Team Member'] = hardcode_team_member(inbox, mailbox)
    inbox['Sentiment Score'] = determine_sentiment(inbox['Body'])
    inbox['Flag Status'] = inbox[['Flag Completed Date', 'FlagRequest']].apply(lambda x: flag_status(*x), axis=1)
    inbox['Complexity'] = inbox['Categories'].str.extract(r'Complexity Level (\d)').astype(float)
    inbox['Protocol'] = inbox.pipe(identify_first_protocol)
    inbox['Speed to Market'] = inbox['Categories'].str.contains('Speed to Market')
    return inbox.rename(columns=lambda x: x.replace(' ', '_')).drop(columns=['ConversationIndex']).to_json(), 200

Here is the block from insights.py:

    res = pd.Series(index=inbox_index, dtype='object')
    for index in tqdm(inbox_index):
    *******Line 92 starts here********
        res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
    return res
Mike Mann
  • 528
  • 4
  • 18
  • 1
    Do you mind sharing the code mate? – Libby Lebyane Nov 23 '21 at 16:30
  • Either `res` or `x` is not a list (or other subscriptable type). – Barmar Nov 23 '21 at 16:33
  • @LibbyLebyane sorry about that, I have added the blocks referenced in the error. Hope that clarifies, thanks in advance – Mike Mann Nov 23 '21 at 16:43
  • Did you step through the code in a debugger to see what the values of all variables are at that point? Have you ensured that `pd.Series(index=inbox_index, dtype='object')` is returning something? If you haven't done basic debugging yet, please do. PyCharm is free and has a great debugger: https://www.jetbrains.com/help/pycharm/debugging-your-first-python-application.html – Random Davis Nov 23 '21 at 16:46

1 Answers1

0

Usually, when working with APIs it's recommended to use if statements since you might not be sure what may be returned. Perhaps you can try to check what type inbox and res is, so your code might look like this:

For insights.py

res = pd.Series(index=inbox_index, dtype='object')
if res: # Check if res actually has a value before subscribing to it with res[key]
    for index in tqdm(inbox_index):
    *******Line 92 starts here********
        res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
        return res

Then you might as well do the same with the for the app.py before using the inbox variable, or just use a bunch of if statements checking before accessing a data point then handling the error if something is type NoneType.

Libby Lebyane
  • 167
  • 2
  • 14
  • hmmm, when I do this it returns entire df as null so I don't think it's reviewing any under that if. I will add this issue only started a few days ago and has been working for quite some time. So I suspect it's only one value in the dataset, but I can't figure out how to isolate – Mike Mann Nov 23 '21 at 17:30
  • Looks like you have some direction on your issue, at lease you have an idea where the issue might be. – Libby Lebyane Nov 23 '21 at 17:46