Repository navigation
Expand file tree
/
Copy pathmailroom.py
More file actions
executable file
·210 lines (160 loc) · 5.52 KB
/
Copy pathmailroom.py
File metadata and controls
executable file
·210 lines (160 loc) · 5.52 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
"""
mailroom assignment
This version uses a dict for the main db, and exception handling to
check input
"""
import sys
import math
# handy utility to make pretty printing easier
from textwrap import dedent
# In memory representation of the donor database
# using a tuple for each donor
# -- kind of like a record in a database table
# using a dict with a lower case version of the donor's name as the key
donor_db = {}
donor_db['william gates, iii'] = ("William Gates, III", [653772.32, 12.17])
donor_db['jeff bezos'] = ("Jeff Bezos", [877.33])
donor_db['paul allen'] = ("Paul Allen", [663.23, 43.87, 1.32])
donor_db['mark zuckerberg'] = ("Mark Zuckerberg", [1663.23, 4300.87, 10432.0])
def list_donors():
"""
creates alist of the donors as a string, so tehy can be printed
not calling print from here makes it more flexible and easier to
test
"""
listing = ["Donor list:"]
for donor in donor_db.values():
listing.append( donor[0] )
return "\n".join(listing)
def find_donor(name):
"""
find a donor in the donor db
:param: the name of the donor
:returns: The donor data structure -- None if not in the donor_db
"""
key = name.strip().lower()
return donor_db.get(key)
def add_donor(name):
"""
add a new donor to the donr db
:param: the name of the donor
:returns: the new Donor data structure
"""
name = name.strip()
donor = (name, [])
donor_db[name.lower()] = donor
return donor
def main_menu_selection():
"""
Print out the main application menu and then read the user input.
"""
input = raw_input(dedent('''
Choose an action:
1 - Send a Thank You
2 - Create a Report
3 - Send letters to everyone
4 - Quit
> '''))
return input.strip()
def gen_letter(donor):
"""
Generate a thank you letter for the donor
:param: donor tuple
:returns: string with letter
"""
return dedent('''Dear {0:s}
Thank you for your very kind donation of ${1:.2f}.
It will be put to very good use.
Sincerely,
-The Team
'''.format(donor[0], donor[1][-1]) )
def send_thank_you():
"""
Execute the logic to record a donation and generate a thank you message.
"""
# Read a valid donor to send a thank you from, handling special commands to
# let the user navigate as defined.
while True:
name = raw_input("Enter a donor's name (or list to see all donors or 'menu' to exit)> ").strip()
if name == "list":
print list_donors()
elif name == "menu":
return
else:
break
# Now prompt the user for a donation amount to apply. Since this is
# also an exit point to the main menu, we want to make sure this is
# done before mutating the db .
while True:
amount_str = raw_input("Enter a donation amount (or 'menu' to exit)> ").strip()
if amount_str == "menu":
return
# Make sure amount is a valid amount before leaving the input loop
try:
amount = float(amount_str)
# extra check here -- unlikely that someone will type "NaN", but
# it IS possible, and it is a valid floating point number:
# http://en.wikipedia.org/wiki/NaN
if math.isnan(amount) or math.isinf(amount) or round(amount, 2) == 0.00:
raise ValueError
# in this case, the ValueError could be raised by the float() call, or by the NaN-check
except ValueError:
print "error: donation amount is invalid\n"
else:
break
# If this is a new user, ensure that the database has the necessary
# data structure.
donor = find_donor(name)
if donor is None:
donor = add_donor(name)
# Record the donation
donor[1].append(amount)
print gen_letter(donor)
def sort_key(item):
## used to sort on name in donor_db
return item[1]
def generate_donor_report():
"""
Generate the report of the donors and amounts donated.
:returns: the donor report as a string.
"""
# First, reduce the raw data into a summary list view
report_rows = []
for (name, gifts) in donor_db.values():
total_gifts = sum(gifts)
num_gifts = len(gifts)
avg_gift = total_gifts / num_gifts
report_rows.append( (name, total_gifts, num_gifts, avg_gift) )
#sort the report data
report_rows.sort(key=sort_key)
report = []
report.append("%25s | %11s | %9s | %12s"%("Donor Name","Total Given","Num Gifts","Average Gift") )
report.append("-"*66)
for row in report_rows:
report.append("%25s %11.2f %9i %12.2f"%row)
return "\n".join(report)
def save_letters_to_disk():
"""
make a letter for each donor, and save it to disk.
"""
for donor in donor_db.values():
letter = gen_letter(donor)
filename = donor[0].replace(" ","_") + ".txt" # I don't like spaces in filenames...
open(filename, 'w').write(letter)
def print_donor_report():
print generate_donor_report()
def quit():
sys.exit(0)
if __name__ == "__main__":
running = True
selection_dict = {"1": send_thank_you,
"2": print_donor_report,
"3": save_letters_to_disk,
"4": quit}
while True:
selection = main_menu_selection()
try:
selection_dict[selection]()
except KeyError:
print "error: menu selection is invalid!"