pythonjsonimap

How to check if a subject is not in the all_subjects of emails


I have a list of email subjects which were sent today (I connect to my IMAP server, and fetch all emails):

sent_subjects = [
    'subject1 backup up completed', 'subject2-2 backup failed', 'subject4 backup partial complete', 'email3 done', 'ak47 failed', 'mp5 is good', 'm4 is good'
]

I have a json file and almost all scripts I have and send email, they have entry there:

emails = [
    {'subject': 'mp3 is good', 'date': '2020-02-02'},
    {'subject': 'mp5 is good', 'date': '2020-02-02'},
    {'subject': 'm4 is good', 'date': '2020-02-02'}
]

And I create subjects like this:

subjects = [x['subject'] for x in emails]

These are all the subjects that should be sent (some of them were not sent):

static_subjects = [
    'subject1', 'subject2-2', 'subject4', 'email3', 'ak47', 'subject10', 'email11', 'final destination'
    ]
subjects += static_subjects
subjects.sort()

I know I can do this, but this does not return the expected output:

final = list(set(subjects) - set(sent_subjects))

Because some subjects like subject1 and subject4 do not have static subject and they may change based on some conditions (these subjects are not mine, and I do not send email with these subjects. I just receive them).

So, I need to check if any subjects of subjects are not in sent_subjects, so I can trace which emails I did not receive, to see if there any issues.

In this case, I did not receive these subjects and the output should have them:

mp3 is good
subject10
email11
final destination

The other thing I know is using if == does not work, because email11 for example is like subject1 and subject2-2 that has two or more subjects (if fails or success).

Would you please help me regarding this?


Solution

  • You can try this:

    sent_subjects = [
        'subject1 backup up completed', 'subject2-2 backup failed', 'subject4 backup partial complete', 'email3 done', 'ak47 failed', 'mp5 is good', 'm4 is good'
    ]
    
    emails = [
        {'subject': 'mp3 is good', 'date': '2020-02-02'},
        {'subject': 'mp5 is good', 'date': '2020-02-02'},
        {'subject': 'm4 is good', 'date': '2020-02-02'}
    ]
    
    subjects = [x['subject'] for x in emails]
    
    static_subjects = [
        'subject1', 'subject2-2', 'subject4', 'email3', 'ak47', 'subject10', 'email11', 'final destination'
        ]
    subjects += static_subjects
    subjects.sort()
    
    final = list(set(subjects) - set(sent_subjects) - set([s.split(' ')[0] for s in sent_subjects]))  # here~
    

    The only difference between my code and yours is I exclude set([s.split(' ')[0] for s in sent_subjects]) additionally at last.