Skip to content

Commit 7a06bb7

Browse files
committed
2 parents d886008 + bd519a5 commit 7a06bb7

124 files changed

Lines changed: 6921 additions & 377 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
a really simple module to use to test reloading
5+
"""
6+
7+
this = "this2"
8+
that = "that"
9+
10+
def print_something():
11+
print "I'm printing something else"
12+

‎Examples/Session03/test_script.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python
2+
3+
print "yes, it ran"
4+

‎Solutions/Session03/list_lab.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Chris' solutions to the list lab
5+
6+
A couple take-home concepts:
7+
8+
If you need to delete stuff from the list when looping through it,
9+
make a copy ([:]) first.
10+
11+
You almost never want to loop through a sequence by doing:
12+
13+
for i in range(len(seq)):
14+
...
15+
if you don't need the index, simply do:
16+
17+
for item in seq:
18+
...
19+
If you need both the item and the index, use enumerate():
20+
21+
for i, item in enumerate(seq):
22+
...
23+
24+
If you need to loop through more than one list at once, use zip():
25+
26+
for item1, item2 in zip(list1, list2):
27+
...
28+
29+
"""
30+
31+
# Task 1
32+
fruits = ["Apples", "Pears", "Oranges", "Peaches"]
33+
34+
print fruits
35+
36+
new = raw_input("type a fruit to add> ")
37+
38+
fruits.append(new)
39+
40+
print fruits
41+
42+
ind = int(raw_input("give me an index> "))
43+
44+
print "you selected fruit number: %i, which is %s"%(ind, fruits[ind-1])
45+
46+
print fruits
47+
48+
fruits.insert(0, 'Kiwi')
49+
50+
print fruits
51+
52+
print "All the P fruits"
53+
for fruit in fruits:
54+
if fruit[0].lower() == 'p':
55+
print fruit
56+
57+
# make a new list with duplicated entries - to test removal
58+
d_fruits = fruits * 2
59+
60+
print " All the fruits are:", fruits
61+
ans = raw_input("Which fruit would you like to delete? ")
62+
while ans in d_fruits:
63+
d_fruits.remove(ans)
64+
65+
print d_fruits
66+
67+
## another way to do that:
68+
## This one only requires looping through list once
69+
d_fruits = []
70+
for fruit in fruits * 2:
71+
if fruit != ans:
72+
d_fruits.append(fruit)
73+
74+
print d_fruits
75+
76+
# keep a copy around for next step
77+
orig_fruits = fruits[:]
78+
79+
for fruit in fruits:
80+
ans = raw_input("Do you like: %s? "%fruit)
81+
if ans[0].lower() == 'n': # so they could answer y or Y or yes or Yes...
82+
while fruit in fruits: # just in case there are duplicates
83+
fruits.remove(fruit)
84+
elif ans[0].lower() == 'y':
85+
pass
86+
else:
87+
print "please answer yes or no next time!"
88+
# Note: I could be smarter about re-asking here,
89+
# but that's not the point of this excercise...
90+
91+
print "here they are with only the ones you like"
92+
print fruits
93+
94+
fruits = orig_fruits[:] # makes a copy
95+
96+
# option 1: build up a copy one by one:
97+
r_fruits = []
98+
for fruit in fruits:
99+
r_fruits.append(fruit[::-1])
100+
101+
print "here they are reversed"
102+
print r_fruits
103+
104+
# option 2: make a copy, then modify in place:
105+
r_fruits = fruits[:]
106+
for i, fruit in enumerate(r_fruits):
107+
r_fruits[i] = fruit[::-1]
108+
109+
print r_fruits

‎Solutions/Session03/mailroom.py‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env python
2+
3+
from textwrap import dedent
4+
import math
5+
6+
# In memory representation of the donor database
7+
# using a tuple for each donor
8+
# -- kind of like a record in a database table
9+
donor_db = []
10+
donor_db.append( ("William Gates, III", [653772.32, 12.17]) )
11+
donor_db.append( ("Jeff Bezos", [877.33]) )
12+
donor_db.append( ("Paul Allen", [663.23, 43.87, 1.32]) )
13+
donor_db.append( ("Mark Zuckerberg", [1663.23, 4300.87, 10432.0]) )
14+
15+
16+
def print_donors():
17+
print "Donor list:\n"
18+
for donor in donor_db:
19+
print donor[0]
20+
21+
22+
def find_donor(name):
23+
"""
24+
find a donor in the donor db
25+
26+
:param: the name of the donor
27+
28+
:returns: The donor data structure -- None if not in the donor_db
29+
"""
30+
for donor in donor_db:
31+
# do an non-capitalized compare
32+
if name.strip().lower() == donor[0].lower():
33+
donor
34+
return None
35+
36+
37+
def main_menu_selection():
38+
"""
39+
Print out the main application menu and then read the user input.
40+
"""
41+
input = raw_input(dedent('''
42+
Choose an action:
43+
44+
1 - Send a Thank You
45+
2 - Create a Report
46+
3 - Quit
47+
48+
> '''))
49+
return input.strip()
50+
51+
52+
def gen_letter(donor):
53+
"""
54+
Generate a thank you letter for the donor
55+
56+
:param: donor tuple
57+
58+
:returns: string with letter
59+
"""
60+
return dedent('''
61+
Dear %s
62+
63+
Thank you for your very kind donation of %.2f.
64+
It will be put to very good use.
65+
66+
Sincerely,
67+
-The Team
68+
''' % (donor[0], donor[1][-1]) )
69+
70+
71+
def send_thank_you():
72+
"""
73+
Execute the logic to record a donation and generate a thank you message.
74+
"""
75+
# Read a valid donor to send a thank you from, handling special commands to
76+
# let the user navigate as defined.
77+
while True:
78+
name = raw_input("Enter a donor's name (or list to see all donors or 'menu' to exit)> ").strip()
79+
if name == "list":
80+
print_donors()
81+
elif name == "menu":
82+
return
83+
else:
84+
break
85+
86+
# Now prompt the user for a donation amount to apply. Since this is also an exit
87+
# point to the main menu, we want to make sure this is done before mutating the db
88+
# list object.
89+
while True:
90+
amount_str = raw_input("Enter a donation amount (or 'menu' to exit)> ").strip()
91+
if amount_str == "menu":
92+
return
93+
# Make sure amount is a valid amount before leaving the input loop
94+
amount = float(amount_str)
95+
if math.isnan(amount) or math.isinf(amount) or round(amount, 2) == 0.00:
96+
print "error: donation amount is invalid\n"
97+
else:
98+
break
99+
100+
# If this is a new user, ensure that the database has the necessary data structure.
101+
donor = find_donor(name)
102+
if donor is None:
103+
donor = (name, [])
104+
donor_db.append( donor )
105+
106+
# Record the donation
107+
donor[1].append(amount)
108+
print gen_letter(donor)
109+
110+
111+
def sort_key(item):
112+
return item[1]
113+
114+
115+
def print_donor_report():
116+
"""
117+
Generate the report of the donors and amounts donated.
118+
"""
119+
# First, reduce the raw data into a summary list view
120+
report_rows = []
121+
for (name, gifts) in donor_db:
122+
total_gifts = sum(gifts)
123+
num_gifts = len(gifts)
124+
avg_gift = total_gifts / num_gifts
125+
report_rows.append( (name, total_gifts, num_gifts, avg_gift) )
126+
127+
#sort the report data
128+
report_rows.sort(key=sort_key)
129+
print "%25s | %11s | %9s | %12s"%("Donor Name","Total Given","Num Gifts","Average Gift")
130+
print "-"*66
131+
for row in report_rows:
132+
print "%25s %11.2f %9i %12.2f"%row
133+
134+
if __name__ == "__main__":
135+
running = True
136+
while running:
137+
selection = main_menu_selection()
138+
if selection is "1":
139+
send_thank_you()
140+
elif selection is "2":
141+
print_donor_report()
142+
elif selection is "3":
143+
running = False
144+
else:
145+
print "error: menu selection is invalid!"

0 commit comments

Comments
 (0)