|
| 1 | +__author__ = 'Robert W. Perkins' |
| 2 | + |
| 3 | + |
| 4 | +def mk_dbase(): |
| 5 | + """Create data structure for donor list""" |
| 6 | + # donor name = key: sum of donations, donation 1, donation 2, ... |
| 7 | + ndbase = { |
| 8 | + 'Jeff McCarthy': [4000, 2500, 1000, 500], |
| 9 | + 'Tabitha Simmons': [2450, 450, 2000], |
| 10 | + 'Angela Cartwright': [5500, 5500], |
| 11 | + 'Billy Murray': [3700, 3450, 250], |
| 12 | + 'Alexa Dalton': [6940, 240, 1200, 5500] |
| 13 | + } |
| 14 | + return ndbase |
| 15 | + |
| 16 | + |
| 17 | +def get_input(): |
| 18 | + """Ask user whether to send thank you note or create report and return answer""" |
| 19 | + |
| 20 | + choices = { |
| 21 | + '1': 'Enter "1" to Send a Thank-You Note', |
| 22 | + '2': 'Enter "2" to Create a Report', |
| 23 | + 'q': 'Enter "q" to quit-->' |
| 24 | + } |
| 25 | + in_put = None |
| 26 | + while not in_put in choices: |
| 27 | + in_put = raw_input('%s, %s, %s' % (choices['1'], choices['2'], choices['q'])) |
| 28 | + return in_put |
| 29 | + |
| 30 | + |
| 31 | +def safe_input(): |
| 32 | + try: |
| 33 | + new_d = raw_input("Enter donation amount (must be numeric)-->") |
| 34 | + except EOFError: |
| 35 | + return None |
| 36 | + except KeyboardInterrupt: |
| 37 | + return None |
| 38 | + return int(new_d) |
| 39 | + |
| 40 | + |
| 41 | +def print_email(p_name, p_donation): |
| 42 | + """ Print thank you note for donation from p_name """ |
| 43 | + ltr_temp = {'Template1': 'Dear {name}, Thanks so much for your generous donation of ${donation}. ' |
| 44 | + 'It is greatly appreciated!' |
| 45 | + } |
| 46 | + |
| 47 | + print ltr_temp['Template1'].format(name=p_name, donation=p_donation) |
| 48 | + |
| 49 | + |
| 50 | +def app_record(app_name, app_dict): |
| 51 | + """ Append an existing donor record """ |
| 52 | + app_donation = safe_input() |
| 53 | + app_dict[app_name].append(app_donation) |
| 54 | + app_dict[app_name][0] += app_donation |
| 55 | + print_email(app_name, app_donation) |
| 56 | + |
| 57 | + |
| 58 | +def add_record(add_name, add_dict): |
| 59 | + """ Add new donor to database """ |
| 60 | + add_donation = safe_input() |
| 61 | + add_dict[add_name] = [add_donation, add_donation] |
| 62 | + print_email(add_name, add_donation) |
| 63 | + |
| 64 | + |
| 65 | +def thank_you(donor_dict): |
| 66 | + """ Find or create a donor, add new donation, and return a thank you note""" |
| 67 | + name = 'list' |
| 68 | + while name == 'list': |
| 69 | + new_name = raw_input("Enter full name of donor-->") |
| 70 | + name = str(new_name) |
| 71 | + if not (name == 'list'): |
| 72 | + break |
| 73 | + else: |
| 74 | + for item in donor_dict: |
| 75 | + print item |
| 76 | + |
| 77 | + if name in donor_dict: |
| 78 | + app_record(name, donor_dict) |
| 79 | + else: |
| 80 | + add_record(name, donor_dict) |
| 81 | + |
| 82 | + |
| 83 | +def write_efile(name, donation_list): |
| 84 | + """write a donor email to disk named "name".txt""" |
| 85 | + to_file = './%s.txt' % name |
| 86 | + outdata = 'Dear %s, Thanks so much for your recent generous donation of $%s. ' \ |
| 87 | + 'It is greatly appreciated!' % (name, donation_list[-1]) |
| 88 | + open(to_file, 'w').write(outdata) |
| 89 | + |
| 90 | + |
| 91 | +#def sum_element(key_dbase): |
| 92 | + #"""set key for sorting on sum element of data structure""" |
| 93 | + #return key_dbase[1] |
| 94 | + |
| 95 | + |
| 96 | +def mk_report(rep_dict): |
| 97 | + """ Create a sorted list of donors""" |
| 98 | + print 'Donor Name\t\t\tTotal Donations\t# of Donations\t\tAverage Donation' |
| 99 | + #rep_dbase.sort(key=sum_element) |
| 100 | + for j, k in rep_dict.items(): |
| 101 | + num_donations = len(k)-1 |
| 102 | + avg_donation = k[0]/(len(k)-1) |
| 103 | + print '%s\t\t\t%s\t\t\t\t%s\t\t\t\t\t\t%s' % (j, k[0], num_donations, avg_donation) |
| 104 | + write_efile(j,k) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + donor = mk_dbase() |
| 109 | + answer = None |
| 110 | + while not (answer == "q"): |
| 111 | + answer = get_input() |
| 112 | + if answer == "q": |
| 113 | + break |
| 114 | + elif answer == "1": |
| 115 | + thank_you(donor) |
| 116 | + else: |
| 117 | + mk_report(donor) |
| 118 | + print "Exiting" |
0 commit comments