HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Counting senders in a mailbox

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
senderscountingmailbox

Problem

I'm learning Python from Coursera and am doing a class. One of the assignments is as follows:


Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

I have completed this assignment and I think I did pretty good. How can I make it better?

def get_email(filename, name):
    count = 0
    user = {}
    handle = open(filename)

    for line in handle:      
        if name in line and 'From' in line and 'From:' not in line:
            arr = line.split(' ')
            count += 1
            user['email'] = count

    email = ''.join(arr[1])
    print "%s %d" % (email, count)

get_email('mbox-short.txt', 'cwen')
#<= cwen@iupui.edu 5
get_email('mbox-short.txt', 'david')
#<= david.horwitz@uct.ac.za 4


Full text file to read from here

Solution

In some sense, you haven't really completed the task, which is to find the most prolific commenter. Your get_email() function requires you to know the names before you call the function, and to know what those names are, you would have to read the mailbox!

Furthermore, your user dictionary is kind of pointless. It's only ever used as a write-only variable user['email']. And since only one key is used, there is no advantage to using a dictionary rather than just a regular variable.

To do the job properly, you should make one pass through the file, keeping track of the counts of all of the users as you go. Using a dictionary to accomplish that is the point of the exercise.

Context

StackExchange Code Review Q#135032, answer score: 3

Revisions (0)

No revisions yet.