I am using Imaplib to fetch data from Gmail and get the number of emails each day. I am going to login using Imaplib twice for fetching emails for the inbox well as for the outbox (sent mail). Is it possible that I have to login only once and get result as I need?The code works fine and I am asking for review that how I can do more optimizations with this approach.from imaplib import IMAP4_SSL from datetime import date, timedelta, datetime from time import mktime from email.utils import parsedate import emailimport pygaladdress = '<Your Email>'password = '<password>'inbox = 'INBOX'outbox = '[Gmail]/Sent Mail'd = 6def inbox_week(address,password,inbox,d):    Monday_Tuple = ('M','o','n')    Monday_List = []    Tuesday_Tuple = ('T','u','e')    Tuesday_List = []    Wednesday_Tuple = ('W','e','d')    Wednesday_List = []    Thursday_Tuple = ('T','h','u')    Thursday_List = []    Friday_Tuple = ('F','r','i')    Friday_List = []    Saturday_Tuple = ('S','a','t')    Saturday_List = []    Sunday_Tuple = ('S','u','n')    Sunday_List = []    mail = IMAP4_SSL('imap.gmail.com')    mail.login(address, password)    mail.select(inbox)    interval = (date.today()-timedelta(d)).strftime(%d-%b-%Y)    result, data =                mail.uid('search',None,'(SENTSINCE{date})'.format(date=interval))    for num in data[0].split():        result, data = mail.uid('fetch',num,'(RFC822)')        msg = email.message_from_string(data[0][1])        msg['Date']        main_date = msg['Date']        Date_Tuple = main_date[0],main_date[1],main_date[2]        if (Date_Tuple==Monday_Tuple):            Monday_List.append(Monday_Tuple)                if (Date_Tuple == Tuesday_Tuple):            Tuesday_List.append(Tuesday_Tuple)        if (Date_Tuple == Wednesday_Tuple):            Wednesday_List.append(Wednesday_Tuple)        if (Date_Tuple == Thursday_Tuple):            Thursday_List.append(Thursday_Tuple)        if (Date_Tuple == Friday_Tuple):            Friday_List.append(Friday_Tuple)        if (Date_Tuple == Saturday_Tuple):            Saturday_List.append(Saturday_Tuple)        if (Date_Tuple == Sunday_Tuple):            Sunday_List.append(Sunday_Tuple)    monday_inbox = len(Monday_List)    tuesday_inbox =  len(Tuesday_List)    wednesday_inbox =  len(Wednesday_List)        thursday_inbox = len(Thursday_List)    friday_inbox =  len(Friday_List)    saturday_inbox =  len(Saturday_List)    sunday_inbox =  len(Sunday_List)    return           monday_inbox,tuesday_inbox,wednesday_inbox,thursday_inbox,friday_inbox,saturday_inbox,sunday_inboxa,b,c,d,e,f,g = inbox_week(address,password,inbox,d)def outbox_week(address,password,outbox,d):    Monday_Tuple = ('M','o','n')    Monday_List = []    Tuesday_Tuple = ('T','u','e')    Tuesday_List = []    Wednesday_Tuple = ('W','e','d')    Wednesday_List = []    Thursday_Tuple = ('T','h','u')    Thursday_List = []    Friday_Tuple = ('F','r','i')    Friday_List = []    Saturday_Tuple = ('S','a','t')    Saturday_List = []    Sunday_Tuple = ('S','u','n')    Sunday_List = []    mail = IMAP4_SSL('imap.gmail.com')    mail.login(address, password)    mail.select(outbox)    interval = (date.today()-timedelta(d)).strftime(%d-%b-%Y)    result, data = mail.uid('search', None,'(SENTSINCE {date})'.format(date=interval))   for num in data[0].split():       result, data = mail.uid('fetch',num,'(RFC822)')       msg = email.message_from_string(data[0][1])       main_date = msg['Date']       Date_Tuple = main_date[0],main_date[1],main_date[2]       if (Date_Tuple==Monday_Tuple):           Monday_List.append(Monday_Tuple)               if (Date_Tuple == Tuesday_Tuple):           Tuesday_List.append(Tuesday_Tuple)       if (Date_Tuple == Wednesday_Tuple):           Wednesday_List.append(Wednesday_Tuple)       if (Date_Tuple == Thursday_Tuple):           Thursday_List.append(Thursday_Tuple)       if (Date_Tuple == Friday_Tuple):           Friday_List.append(Friday_Tuple)       if (Date_Tuple == Saturday_Tuple):           Saturday_List.append(Saturday_Tuple)       if (Date_Tuple == Sunday_Tuple):           Sunday_List.append(Sunday_Tuple)       monday_outbox = len(Monday_List)       tuesday_outbox =  len(Tuesday_List)       wednesday_outbox =  len(Wednesday_List)           thursday_outbox = len(Thursday_List)       friday_outbox =  len(Friday_List)       saturday_outbox =  len(Saturday_List)       sunday_outbox =  len(Sunday_List)    return      monday_outbox,tuesday_outbox,wednesday_outbox,thursday_outbox,friday_outbox,saturday_outbox,sunday_outboxaa,bb,cc,dd,ee,ff,gg = outbox_week(address,password,outbox,d)bar_chart = pygal.Bar()bar_chart.title = 'Weekly Email Analysis'bar_chart.x_labels =     ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')bar_chart.add('Received', [a,b,c,d,e,f,g])bar_chart.add('Sent',[aa,bb,cc,dd,ee,ff,gg])bar_chart.render_to_file('honey4.svg')
Gmail Data Analysis
python;python 2.7;email