-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment2.py
107 lines (78 loc) · 3.18 KB
/
assignment2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Create a program that returns a birthday or logs an error."""
import argparse
import csv
import logging
import urllib2
from datetime import datetime
def downloadData(url):
"""This function called downloadData downloads the contents located at the url
and returns it to the caller.
Args:
url takes a string called url
Returns:
Returns the contents located at the url.
"""
response = urllib2.urlopen(url)
return response.read()
def setLogger():
"""This function called setLogger creates a logger and returns it to a file error.log.
Returns:
Returns any errors to the error.log file.
"""
logging.basicConfig(filename='error.log', level=logging.ERROR)
return logging.getLogger('assignment2')
def processData(data, logger):
"""Takes the contents of a file as the file, processes it line by line, returning
it to a dictionary. The dictionary maps a Person's ID to a tuple(name, birthday).
Also converts birthday from a string to the correct format.
Args:
data which is all of the processed data from the processData function
logger which logs the errors from incorrectly formatted data
Returns:
Returns a dictionary where the keys are id, name, birthday
"""
splitter = data.split('\n')
keys = splitter.pop(0).split(',')
reader_list = csv.DictReader(splitter, fieldnames=keys)
birthdays = [x for x in reader_list]
result = {}
counter = 0
for person in birthdays:
try:
result[person['id']] = (person['name'], datetime.strptime(person['birthday'], '%d/%m/%Y'))
except:
logger.error('Error processing line #{} for ID #{}'.format(counter, person['id']))
counter += 1
return result
def displayPerson(personID, data):
"""This function called displayPerson prints out formatted information for the
id entered by the user.
Args:
personID holds all the possible ID's that could return a birthday
data which is all of the processed data from the processData function
Returns:
Returns one of two possible messages
"""
try:
print('{}, {}'.format(data[personID][0], data[personID][1].strftime('%Y-%m-%d')))
except:
print('No user found with that id')
if __name__ == '__main__':
# https://s3.amazonaws.com/cuny-is211-spring2015/birthdays100.csv
# setting up argparse
parser = argparse.ArgumentParser(description='Birthday')
parser.add_argument('--url', help='URL that represents Birthdays for user id', required=True)
args = vars(parser.parse_args())
# download data
data = downloadData(args['url'])
# set up logger
assignment2 = setLogger()
# process data
result = processData(data, assignment2)
# give me a number and get a birthday
personID = raw_input('Please enter a number.')
while int(personID) > 0:
displayPerson(personID, result)
personID = raw_input('Please enter a number.')