Skip to content
This repository was archived by the owner on Oct 9, 2018. It is now read-only.

Commit b6ce810

Browse files
authored
Merge pull request #74 from tenoverpar/master
Session 4 Homework
2 parents 8b1d112 + 1d13e5b commit b6ce810

File tree

3 files changed

+255
-0
lines changed

3 files changed

+255
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
# Programming in python B Winter 2018
3+
# February 11, 2017
4+
# Copy file Lab - Section 4
5+
# Tracy Allen - git repo https://github.com/tenoverpar/Wi2018-Classroom
6+
7+
8+
# File Copy steps
9+
# Provide a file orginial file name
10+
source = input("Enter the source file name: ")
11+
output = input("Enter the destination file name:")
12+
while (source == output):
13+
print("Please provide unique file name:")
14+
output = input("Enter the destination file name:")
15+
16+
with open(source, 'rb') as infile, open(output, 'wb') as outfile:
17+
outfile.write(infile.read())

students/TracyA/session04/dict_lab.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
# Programming in python B Winter 2018
3+
# February 11, 2017
4+
# Dictionary Lab - Section 4
5+
# Tracy Allen - git repo https://github.com/tenoverpar/Wi2018-Classroom
6+
7+
8+
# Dictionaries 1
9+
10+
ta_dictionary = {'name': 'Chris', 'city': 'Seattle', 'cake': 'Chocolate'}
11+
print(ta_dictionary)
12+
# Remove cake from the dictionary
13+
ta_dictionary.pop('cake')
14+
# Display the Dictionary
15+
print(ta_dictionary)
16+
# Add Fruit to Dictionary
17+
ta_dictionary['fruit'] = 'Mango'
18+
# Print the Dictionary
19+
print(ta_dictionary)
20+
# Display keys
21+
print(ta_dictionary.keys())
22+
# Display Values
23+
print(ta_dictionary.items())
24+
# Display if cake is in Dictionary
25+
# This only works on the key
26+
print('cake' in ta_dictionary)
27+
# See if Mango is in dictionary. use .Values
28+
print('Mango' in ta_dictionary.values())
29+
30+
# Dictionaries 2
31+
# Make a dictionay from the earlier with t as key
32+
33+
34+
nDict = {}
35+
for k, v in ta_dictionary.items():
36+
nDict[k] = v.lower().count('t')
37+
print('--------New Dict ----------\n')
38+
print(nDict)
39+
40+
41+
s2 = []
42+
s3 = []
43+
s4 = []
44+
for i in range(20):
45+
if i % 2 == 0:
46+
s2.append(i)
47+
if i % 3 == 0:
48+
s3.append(i)
49+
if i % 4 == 0:
50+
s4.append(i)
51+
s2 = set(s2)
52+
s3 = set(s3)
53+
s4 = set(s4)
54+
55+
print('--------This is S2 ----------\n')
56+
print(s2)
57+
print('--------This is S3 ----------\n')
58+
print(s3)
59+
print('--------This is S4 ----------\n')
60+
print(s4)
61+
62+
# Print subset information
63+
print('-------- S3 sub of s2---------\n')
64+
print(s3.issubset(s2))
65+
print('-------- S4 sub of s2---------\n')
66+
print(s4.issubset(s2))
67+
68+
# Create set with python as the list
69+
python_set = set(list('Python'))
70+
print('--------python_set---------\n')
71+
print(python_set)
72+
python_set.add('i')
73+
print('--------python_set plus i---------\n')
74+
print(python_set)
75+
76+
# Create a frozenset with the letters in ‘marathon'.
77+
frozen_set = frozenset(list('marathon'))
78+
print(frozen_set)
79+
# Union and intersection.
80+
print('--------Print union of the sets---------\n')
81+
print(frozen_set.union(python_set))
82+
print('--------Print intersection of the sets---------\n')
83+
print(frozen_set.intersection(python_set))
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)