Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Exercises #226

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
df8388c
Add codingbat exercises and homework assignments
Oct 31, 2018
0c507f1
Add slicing_lab
Oct 31, 2018
33a37dd
Update slicing lab
Oct 31, 2018
7600d41
Add completed list lab exercise
Oct 31, 2018
db7eccf
Add mailroom 1 exercise
Nov 28, 2018
6f28b25
Update code to meet PEP8 style standards
Nov 28, 2018
5594b91
Add completed str formatting lab
Nov 28, 2018
9644162
Update to meet PEP8 style standards
Nov 28, 2018
9502c8d
resolving merge conflict
Nov 28, 2018
9070037
Resolve merge conflicts
Nov 28, 2018
a07f102
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Dec 9, 2018
dc5341c
Add homework files to personal cloned repository
Dec 9, 2018
978f679
Add dict set lab to student repo
Dec 9, 2018
f7d658a
Add paths and file processing exercise
Dec 9, 2018
23512cc
Add file and processing exercise
Dec 10, 2018
bbede62
Add mailroom 2
Dec 11, 2018
27a56c0
Refactoring mailroom 1 exercise
Dec 11, 2018
f2a3621
Add recent changes to mailroom
Dec 12, 2018
c4979ad
Refactor mailroom with dicts
Dec 12, 2018
57fe3da
Add mailroom 2 refactor. Create report needs work still.
Dec 12, 2018
592133a
Completed mailroom2 refactor
Dec 12, 2018
e62eac9
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Dec 12, 2018
b450ac9
Update to meet pycodestyle
Dec 13, 2018
8fe54ee
Remove whitespace to meet pycodestyle
Dec 13, 2018
786696f
Add session05 exercises
Dec 13, 2018
1e508d8
Update gitignore to include .csv file extensions
Dec 13, 2018
6027e9c
Update gitignore to include .txt files
Dec 13, 2018
cd797e7
Completed exceptions exercise
Dec 13, 2018
ca54533
Add exceptions lab
Dec 13, 2018
c5565ea
Completed safe_input exceptions lab
Dec 13, 2018
9be136a
Completed cigar_party test excercise
Dec 13, 2018
8283d04
Add unit testing for make_chocolate codindbat
Dec 13, 2018
02feb90
Completed dict set lab with comprehensions
Dec 14, 2018
8cbe009
Completed args kwargs lab
Dec 17, 2018
14578f7
Fixed syntax error
Dec 17, 2018
1826882
Update function name
Dec 17, 2018
367e142
Fix NameErrors to pass pytest
Dec 17, 2018
319e4b3
Fix unindentation
Dec 17, 2018
6a5e524
Fix unindentation
Dec 17, 2018
9147e42
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Dec 17, 2018
394fc63
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Dec 17, 2018
700389c
Add mailroom 4 to student repo still wip
Dec 17, 2018
2e98045
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Dec 17, 2018
104ef25
Add mailroom 4 and testing
Dec 17, 2018
bb7374f
Update typo
Dec 17, 2018
5c56b53
Adding Mailroom 4 and html render assignment
Dec 17, 2018
fe6c586
Add html render exercise to student repo
Dec 17, 2018
3e0f84f
Reworked html to pass tests
Dec 17, 2018
12fb4cf
Completed circle exercise
Dec 17, 2018
43a8d0f
Add code for tests to pass
Dec 18, 2018
8632a29
Refactored code to pass tests
Dec 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 0 additions & 108 deletions .gitignore

This file was deleted.

200 changes: 200 additions & 0 deletions students/FyfyNguyen/session06/mailroom4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#!/usr/bin/env python3

"""
Mailroom 4
"""

import sys
import tempfile
import pathlib
from textwrap import dedent

donor_db = {"Robyn Rihanna": [100, 200, 300],
"Ariana Grande": [2250, 4000, 1000],
"Beyonce Carter-Knowles": [150000, 3500, 25000],
"Aubrey Drake Graham": [15000, 5500.25, 1200],
"Justin Bieber": [2500, 250, 750.50]
}

file_dir = pathlib.Path(tempfile.gettempdir())


def print_donors():
"""
Prints a list of exiting donors in donor_db
"""
for donor in donor_db:
print(donor)


def find_donor(name_entered):
"""
Lookup donor in donor_db

:param: the name of the donor
:returns: the donor data structure -- None if donor not found in donor_db
"""
donor_key = name_entered.strip().title()
return donor_db.get(donor_key)


def add_new_donor(name_entered):
"""
Add a new donor to donor_db

:param: the name of the donor
:returns: the new donor data structure
"""
donor = (name_entered, [])
donor_db[name_entered.title()] = []
print("Successfully added new donor to database.")
ask_donation(name_entered)


def update_donation(name_entered, donation):
donor_db.setdefault(name_entered.title(), []).append(donation)
print("Successfully updated donation amount.")
send_thank_you(name_entered, donation)


def ask_donation(name_entered):
"""
Ask the user for donation amount

:param: the name of the donor
"""
donation = float(input("Enter donation amount >>> "))
update_donation(name_entered, donation)


def confirm_donor(name_entered):
"""
Confirm the user wants to add a new donor to donor_db

:param: the name of the donor
"""
response = input(f"Donor does not exist. Add {name_entered.title()} to "
"database? [y/n?] >>> ")
if response.strip() == 'y':
add_new_donor(name_entered)
else:
return


def send_thank_you(name_entered, donation):
"""
Generate a thank you letter to the donor

:param: the name of the donor
:param: the donation amount
:returns:string with letter
"""
return dedent("""Dear {},

Your generous ${:,.2f} donation just made our day!

Thank you!
-Our Charity
""".format(name_entered.title(), donation))


def thank_you():
"""
Execute the logic to record a donation and generate a thank you message
"""
while True:
name_entered = input("Enter the name of a donor or 'list' to view "
"donors >>> ").lower().strip()
if name_entered.strip() == "list":
print_donors()
else:
break

donor = find_donor(name_entered)
if donor is None:
confirm_donor(name_entered)
else:
ask_donation(name_entered)


def thank_you_all():
"""
Generate a letter for each donor and save it to temporary directory
"""
for donor in donor_db:
letters = send_thank_you(donor, donor_db[donor][-1])
file_path = file_dir / (donor.replace(" ", "_") + ".txt")
with open(file_path, 'w') as f:
f.write(letters)
print(f'Files downloaded to: {file_dir}')


def sort_key(donor):
return donor


def create_report():
"""
Generate report of donors and the amounts donated

:returns: the donor report as a string
"""
report_rows = []
for name, donations in donor_db.items():
total_given = sum(donations)
num_gifts = len(donations)
avg_gifts = total_given / num_gifts
report_rows.append((name, num_gifts, num_gifts, avg_gifts))

report_rows.sort(key=sort_key)
report = []
report.append("{:30s} | {:11s} | {:9s} | {:12s}".format("Donor Name",
"Total Given",
"Num Gifts",
"Average Gift"
))
report.append("-" * 72)
for row in report_rows:
report.append("{:30s} | {:11d} | {:9d} | ${:12,.2f}".format(*row))
return "\n".join(report)


def print_report():
print(create_report())


def exit_program():
"""
Quit the program
"""
print("Exiting Program. Goodbye!")
sys.exit()


def main_menu():
selection = input(dedent("""
Welcome to Mailroom!

Select an option:

1 - Create a Report
2 - Send Thank You to a single donor
3 - Send Thank You to all donors
4 - Quit

>>> """))
return selection.strip()


if __name__ == "__main__":
menu_selections = {"1": print_report,
"2": thank_you,
"3": thank_you_all,
"4": exit_program}

while True:
user_selection = main_menu()
try:
menu_selections[user_selection]()
except KeyError:
print("Error: Invalid Selection. Select from the main menu.")
60 changes: 60 additions & 0 deletions students/FyfyNguyen/session06/test_mailroom4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3

"""
Mailroom 4 tests
"""

import pytest
import os
import pathlib
import mailroom4 as mr


def test_print_donors():
listing = mr.print_donors()
assert "Robyn Rihanna" in listing
assert "Aubrey Drake Graham" in listing


def test_find_donor():
donor = mr.find_donor("beyonce caRter Knowles ")
assert donor[0] == "Beyonce Carter Knowles"


def test_if_not_find_donor():
donor = mr.find_donor("Beyonce Carter Knowles")
assert donor is None


def test_add_new_donor():
name = " Jennifer Lopez"
donor = mr.add_new_donor(name)
donor[1].append(750)
assert donor[0] == "Jennifer Lopez"
assert donor[1] == [750]
assert mailroom.find_donor(name) == donor


def test_create_report():
result = mr.create_report()
assert result.startswith("Donor Name | Total Given | Num Gifts | Average Gift")
assert "------------------------------------------------------------------------" in result
assert "Ariana Grande | 3 | 3 | $ 2,416.67" in result


def test_thank_you():
result = mr.thank_you("Chrissy Teigen", 4500)
assert "Dear Chrissy Teigen," in result
assert "$4,500.00" in result
assert "4500" not in result


def test_thank_you_all():
result = mr.thank_you_all("Fyfy Nguyen")
assert result.name == "Fyfy_Nguyen.txt"


def test_thank_you_all_files():
mr.thank_you_all()
for donor in mr.donor_db.items():
file_path = mr.thank_you_all
Binary file not shown.
Binary file not shown.
Loading