Question: Following up from code I provided, implement a base_message class with some fundamental attributes that a message might have, irrespective of medium (i.e. email, texting,

Following up from code I provided, implement a base_message class with some fundamental attributes that a message might have, irrespective of medium (i.e. email, texting, "DM", etc.). Examples you might use are date sent, message contents, to, from, etc.Now refactor your message class above as an email_message class so that it inherits from base_message.Be sure to separate out anything you feel is "email specific" into the email_message class.

class message:

##get words 'from' in line

def getfrom(self,line):

words = line.split()

return words[1]

##get words 'day' in line

def getday(self,line):

words = line.split()

return words[2]

class mailbox:

##initialize the mailbox class and create two empty dict:dict_from dict_day

def __init__(self, dict_from=dict(), dict_day=dict()):

self.dict_from = dict_from

self.dict_day = dict_day

self.msg = message()

##add sentence with 'from' into dict_from

def addFrom(self, From):

self.dict_from[From] = self.dict_from.get(From,0) + 1

##add sentence with 'day' into dict_day

def addDay(self, Day):

self.dict_day[Day] = self.dict_day.get(Day,0) + 1

def print_q1(self):

msg = message()

##input file name

filename = input('Enter the file name:')

##open file

file = open(filename)

##select sentence that startwith "From" and amounts of words in that sentence >3,add this into dict_day

for line in file:

words = line.split()

if len(words)>=3 and (words[0]=='From' or words[0]=='from'):

self.addDay(self.msg.getday(line))

return self.dict_day

def print_q2(self):

##input filename and open it

filename = input('Enter the file name:')

file = open(filename)

##select sentence that startwith "From" add this into dict_from

for line in file:

if line.startswith('from:') or line.startswith('From:'):

self.addFrom(self.msg.getfrom(line))

max_word_count =0

max_word = None

##get most frequent email and its corresponding counts in dict

for key, value in self.dict_from.items():

if value>max_word_count:

max_word_count = value

max_word = key

return(max_word,max_word_count)

##Call this class

Mailbox =mailbox()

print(Mailbox.print_q1())

print(Mailbox.print_q2())

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!