File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed
Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+
3+ import csv
4+ import sys
5+
6+
7+ def populate_dictionary (filename ):
8+ """Populate a dictionary with name/email pairs for easy lookup."""
9+ email_dict = {}
10+ with open (filename ) as csvfile :
11+ lines = csv .reader (csvfile , delimiter = ',' )
12+ for row in lines :
13+ name = str (row [0 ].lower ())
14+ email_dict [name ] = row [1 ]
15+ return email_dict
16+
17+
18+ def find_email (argv ):
19+ """ Return an email address based on the username given."""
20+ # Create the username based on the command line input.
21+ try :
22+ fullname = str (argv [1 ] + " " + argv [2 ])
23+ # Preprocess the data
24+ email_dict = populate_dictionary (
25+ '/home/{{ username }}/data/user_emails.csv' )
26+ # If email exists, print it
27+ if email_dict .get (fullname .lower ()):
28+ return email_dict .get (fullname .lower ())
29+ else :
30+ return "No email address found"
31+ except IndexError :
32+ return "Missing parameters"
33+
34+
35+ def main ():
36+ print (find_email (sys .argv ))
37+
38+
39+ if __name__ == "__main__" :
40+ main ()
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+
3+ import unittest
4+ from emails import find_email
5+
6+
7+ class EmailsTest (unittest .TestCase ):
8+ def test_basic (self ):
9+ testcase = [None , "Bree" , "Campbell" ]
10+ 11+ self .assertEqual (find_email (testcase ), expected )
12+
13+ def test_one_name (self ):
14+ testcase = [None , "John" ]
15+ expected = "Missing parameters"
16+ self .assertEqual (find_email (testcase ), expected )
17+
18+ def test_two_name (self ):
19+ testcase = [None , "Roy" , "Cooper" ]
20+ expected = "No email address found"
21+ self .assertEqual (find_email (testcase ), expected )
22+
23+
24+ if __name__ == '__main__' :
25+ unittest .main ()
You can’t perform that action at this time.
0 commit comments