|
| 1 | +#!/usr/bin/env python |
| 2 | +# Programming in python B Winter 2018 |
| 3 | +# February 12, 2017 |
| 4 | +# Mailroom Session 4 |
| 5 | +# Tracy Allen - git repo https://github.com/tenoverpar/Wi2018-Classroom |
| 6 | + |
| 7 | + |
| 8 | +donor_data = {"Allen, Paul": [1000000, 50000, 300000], |
| 9 | + "Gates, Bill": [5000000, 80000, 700000], |
| 10 | + "Warren, Buffett": [30000, 30000, 40000], |
| 11 | + "Musk, Elon": [1000000, 30000], |
| 12 | + "Zuckerberg, Mark": [10000, 50000, 12000, 400000]} |
| 13 | + |
| 14 | + |
| 15 | +def show_list(): |
| 16 | + donor_data = [] |
| 17 | + for donor in donor_data: |
| 18 | + donor_data.append(donor) |
| 19 | + sort_donors = sorted(donor_data) |
| 20 | + for donor in sort_donors: |
| 21 | + print(donor[0]) |
| 22 | + |
| 23 | + |
| 24 | +def get_donor(name): |
| 25 | + """retieve donor form donor_data list |
| 26 | + :param: name of donor |
| 27 | + :returns: donor tuple |
| 28 | + """ |
| 29 | + for donor in donor_data: |
| 30 | + if name.strip().lower() == donor[0].lower(): |
| 31 | + return donor |
| 32 | + return None |
| 33 | + |
| 34 | +# Find Donor |
| 35 | +# def find_donor(name): |
| 36 | +# key = name.title().strip() |
| 37 | +# donations = donor_data.get(key) |
| 38 | +# print("{} has donated the following:".format(key)) |
| 39 | +# print(total) |
| 40 | + |
| 41 | + |
| 42 | +# # Add donor |
| 43 | +# def add_donor(name): |
| 44 | +# name = name.title().strip() |
| 45 | +# donor_data[name] = [] |
| 46 | +# return name |
| 47 | + |
| 48 | + |
| 49 | +def init_prompt(): |
| 50 | + response = input('''\n |
| 51 | + Would you like to: |
| 52 | + '1' - Send a Thank You |
| 53 | + '2' - Create a Report |
| 54 | + '3' - Send letters to everyone |
| 55 | + '4' - Quit |
| 56 | + > ''') |
| 57 | + return response.strip() |
| 58 | + |
| 59 | + |
| 60 | +def split_name(donor): |
| 61 | + """ I can now split the names into first and last name""" |
| 62 | + first_name = donor.split(",")[1].strip() |
| 63 | + last_name = donor.split(",")[0].strip() |
| 64 | + return first_name, last_name |
| 65 | + |
| 66 | + |
| 67 | +def create_letter_files(): |
| 68 | + """This will write the letter as a text file for the donors""" |
| 69 | + letter_dict = {} |
| 70 | + for donor in donor_data: |
| 71 | + letter_dict["first name"], letter_dict["last name"] = split_name(donor) |
| 72 | + letter_dict["amt"] = donor_data[donor][-1] |
| 73 | + with open('{last name}_{first name}.txt'.format(**letter_dict), 'w') as outfile: |
| 74 | + outfile.write(make_donor_email(letter_dict)) |
| 75 | + |
| 76 | + |
| 77 | +def make_donor_email(d): |
| 78 | + """ |
| 79 | + Make a thank you email for the donor |
| 80 | + :param: donor tuple |
| 81 | + returns: string containing text of email1 |
| 82 | + """ |
| 83 | + return '''\n |
| 84 | + Dear {first name} {last name}, |
| 85 | + Thank you for you1r donation of ${amt: .2f}. |
| 86 | + You will be blessed. |
| 87 | + Sincerely, |
| 88 | + -Director |
| 89 | + '''.format(**d) |
| 90 | + |
| 91 | + |
| 92 | +def send_donor_email(): |
| 93 | + donor_dict = {} |
| 94 | + while True: |
| 95 | + name = input("Please enter a donor's name in the form of \ |
| 96 | + 'Last name, First name' " |
| 97 | + "(or 'list' to see a list of all donors, \ |
| 98 | + or 'menu' to exit)> ").strip() |
| 99 | + if name == "list": |
| 100 | + show_list() |
| 101 | + elif name == "menu": |
| 102 | + return None |
| 103 | + else: |
| 104 | + break |
| 105 | + while True: |
| 106 | + amount_str = input("Please enter a donation amount \ |
| 107 | + (or 'menu' to exit)> ").strip() |
| 108 | + if amount_str == "menu": |
| 109 | + return None |
| 110 | + else: |
| 111 | + amount = float(amount_str) |
| 112 | + donor = get_donor(name) |
| 113 | + if donor is None: |
| 114 | + donor_data.setdefault(name, []) |
| 115 | + donor_dict["first name"], donor_dict["last name"] = split_name(name) |
| 116 | + donor_data[name].append(amount) |
| 117 | + donor_dict["amt"] = amount |
| 118 | + break |
| 119 | + print(make_donor_email(donor_dict)) |
| 120 | + |
| 121 | + |
| 122 | +def sort_key(item): |
| 123 | + """ key function used to sort the list by first (not zeroth) item""" |
| 124 | + return item[1] |
| 125 | + |
| 126 | + |
| 127 | +def make_report(): |
| 128 | + rows = [] |
| 129 | + for donor in donor_data: |
| 130 | + total = sum(donor_data[donor]) |
| 131 | + num = len(donor_data[donor]) |
| 132 | + avg = total / num |
| 133 | + rows.append((donor, total, num, avg)) |
| 134 | + rows.sort(key=sort_key, reverse=True) |
| 135 | + print("{:20s}{:15s}{:15s}{:12s}".format( |
| 136 | + "Donor Name", "| Total Given", "| Num Gifts", "| Average Gift")) |
| 137 | + print("_" * 67) |
| 138 | + for row in rows: |
| 139 | + print('{:20s}{:15.2f}{:^15d}{:12.2f}'.format(*row)) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + running = True |
| 144 | + while running: |
| 145 | + selection = init_prompt() |
| 146 | + if selection == "1": |
| 147 | + send_donor_email() |
| 148 | + elif selection == "2": |
| 149 | + make_report() |
| 150 | + elif selection == "3": |
| 151 | + create_letter_files() |
| 152 | + elif selection == "4": |
| 153 | + running = False |
| 154 | + else: |
| 155 | + print("error: please make a valid selection!") |
0 commit comments