Skip to content

Commit 479f992

Browse files
committed
pulled Examples back from the fall class repo.
1 parent a1d2763 commit 479f992

24 files changed

+339
-95
lines changed

Examples/Session01/schedule.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ week 6: Adam Hollis
1818
week 6: Nachiket Galande
1919
week 6: Paul A Casey
2020
week 7: Charles E Robison
21+
week 7: Paul S Briant
2122
week 7: Paul Vosper
22-
week 8: Paul S Briant
2323
week 8: Brandon Chavis
2424
week 8: Jay N Raina
2525
week 8: Josh Hicks

Examples/Session01/students.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ Vosper, Paul: C#, macscript, python
2828
Weidner, Matthew T: German, python, html
2929
Williams, Marcus D: python
3030
Wong, Darryl: perl, php
31-
Yang, Minghao
31+
Yang, Minghao: None
3232
Hagi, Abdishu: python, bash

Examples/Session04/__main__example.py

100644100755
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
print("What it is depends on how it is used")
66

7-
print("right now, this module's __name is: {}".format(__name__))
7+
print("right now, this module's __name__ is: {}".format(__name__))
88

99
# so if you want coce to run only when a module is a top level script,
1010
# you use this clause:
11-
if __name__ == "__main__":
12-
print("I must be running as a top-level script")
11+
#if __name__ == "__main__":
12+
13+
print("I must be running as a top-level script")

Examples/Session06/cigar_party.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
"""
3+
When squirrels get together for a party, they like to have cigars.
4+
A squirrel party is successful when the number of cigars is between
5+
40 and 60, inclusive. Unless it is the weekend, in which case there
6+
is no upper bound on the number of cigars.
7+
8+
Return True if the party with the given values is successful,
9+
or False otherwise.
10+
"""
11+
12+
13+
def cigar_party(num, weekend):
14+
return num >= 40 and (num <= 60 or weekend)
15+

Examples/Session06/codingbat.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Examples from: http://codingbat.com
5+
6+
Put here so we can write unit tests for them ourselves
7+
"""
8+
9+
# Python > Warmup-1 > sleep_in
10+
11+
# The parameter weekday is True if it is a weekday, and the parameter
12+
# vacation is True if we are on vacation.
13+
#
14+
# We sleep in if it is not a weekday or we're on vacation.
15+
# Return True if we sleep in.
16+
17+
18+
def sleep_in(weekday, vacation):
19+
return not weekday or vacation
20+
21+
22+
# We have two monkeys, a and b, and the parameters a_smile and b_smile
23+
# indicate if each is smiling.
24+
25+
# We are in trouble if they are both smiling or if neither of them is
26+
# smiling.
27+
28+
# Return True if we are in trouble.
29+
30+
def monkey_trouble(a_smile, b_smile):
31+
return a_smile is b_smile

Examples/Session06/safe_input.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def safe_input():
2+
try:
3+
the_input = input("\ntype something >>> ")
4+
except (KeyboardInterrupt, EOFError) as error:
5+
print("the error: ", error)
6+
error.extra_info = "extra info for testing"
7+
# raise
8+
return None
9+
return the_input
10+
11+
def main():
12+
safe_input()
13+
14+
def divide(x,y):
15+
try:
16+
return x/y
17+
except ZeroDivisionError as err:
18+
print("you put in a zero!!!")
19+
print("the exeption:", err)
20+
err.args = (("very bad palce for a zero",))
21+
err.extra_stuff = "all kinds of things"
22+
raise
23+
24+
# if __name__ == '__main__':
25+
# main()

Examples/Session06/test_cigar_party.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ def test_10():
6161

6262
def test_11():
6363
assert cigar_party(39, True) is False
64+

Examples/Session06/test_codingbat.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
test file for codingbat module
5+
6+
This version can be run with nose or py.test
7+
"""
8+
9+
from codingbat import sleep_in, monkey_trouble
10+
11+
12+
# tests for sleep_in
13+
def test_false_false():
14+
assert sleep_in(False, False)
15+
16+
17+
def test_true_false():
18+
assert not (sleep_in(True, False))
19+
20+
21+
def test_false_true():
22+
assert sleep_in(False, True)
23+
24+
25+
def test_true_true():
26+
assert sleep_in(True, True)
27+
28+
29+
# put tests for monkey_trouble here
30+
# monkey_trouble(True, True) → True
31+
# monkey_trouble(False, False) → True
32+
# monkey_trouble(True, False) → False
33+
34+
def test_monkey_true_true():
35+
assert monkey_trouble(True, True)
36+
37+
def test_monkey_false_false():
38+
assert monkey_trouble(False, False)
39+
40+
def test_monkey_true_false():
41+
assert monkey_trouble(True, False) is False
42+
43+
# more!
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
pytest example of a parameterized test
5+
6+
NOTE: there is a failure in here! can you fix it?
7+
8+
"""
9+
import pytest
10+
11+
12+
# a (really simple) function to test
13+
def add(a, b):
14+
"""
15+
returns the sum of a and b
16+
"""
17+
return a + b
18+
19+
# now some test data:
20+
21+
test_data = [((2, 3), 5),
22+
((-3, 2), -1),
23+
((2, 0.5), 2.5),
24+
(("this", "that"), "this that"),
25+
(([1, 2, 3], [6, 7, 8]), [1, 2, 3, 6, 7, 8]),
26+
]
27+
28+
29+
@pytest.mark.parametrize(("input", "result"), test_data)
30+
def test_add(input, result):
31+
assert add(*input) == result
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
port of the random unit tests from the python docs to py.test
5+
"""
6+
7+
import random
8+
import pytest
9+
10+
11+
seq = list(range(10))
12+
13+
14+
def test_shuffle():
15+
# make sure the shuffled sequence does not lose any elements
16+
random.shuffle(seq)
17+
# seq.sort() # commenting this out will make it fail, so we can see output
18+
print("seq:", seq) # only see output if it fails
19+
assert seq == list(range(10))
20+
21+
22+
def test_shuffle_immutable():
23+
"""should get a TypeError with an imutable type """
24+
with pytest.raises(TypeError):
25+
random.shuffle((1, 2, 3))
26+
27+
28+
def test_choice():
29+
"""make sure a random item selected is in the original sequence"""
30+
element = random.choice(seq)
31+
assert (element in seq)
32+
33+
34+
def test_sample():
35+
"""make sure all items returned by sample are there"""
36+
for element in random.sample(seq, 5):
37+
assert element in seq
38+
39+
40+
def test_sample_too_large():
41+
"""should get a ValueError if you try to sample too many"""
42+
with pytest.raises(ValueError):
43+
random.sample(seq, 20)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
import unittest
3+
4+
5+
class TestSequenceFunctions(unittest.TestCase):
6+
7+
def setUp(self):
8+
self.seq = list(range(10))
9+
10+
def test_shuffle(self):
11+
# make sure the shuffled sequence does not lose any elements
12+
random.shuffle(self.seq)
13+
self.seq.sort()
14+
self.assertEqual(self.seq, list(range(10)))
15+
16+
# should raise an exception for an immutable sequence
17+
self.assertRaises(TypeError, random.shuffle, (1, 2, 3))
18+
19+
def test_choice(self):
20+
element = random.choice(self.seq)
21+
self.assertTrue(element in self.seq)
22+
23+
def test_sample(self):
24+
with self.assertRaises(ValueError):
25+
random.sample(self.seq, 20)
26+
for element in random.sample(self.seq, 5):
27+
self.assertTrue(element in self.seq)
28+
29+
if __name__ == '__main__':
30+
unittest.main()

Examples/Session07/html_render/run_html_render.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,25 @@ def render_page(page, filename):
4242

4343
page = hr.Element()
4444

45-
page.append("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text")
45+
page.append("Here is a paragraph of text -- there could be more of them, "
46+
"but this is enough to show that we can do some text")
4647

4748
page.append("And here is another piece of text -- you should be able to add any number")
4849

4950
render_page(page, "test_html_output1.html")
5051

52+
# The rest of the steps have been commented out.
53+
# Uncomment them a you move along with the assignment.
54+
5155
# ## Step 2
5256
# ##########
5357

5458
# page = hr.Html()
5559

5660
# body = hr.Body()
5761

58-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text"))
62+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
63+
# "but this is enough to show that we can do some text"))
5964

6065
# body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
6166

@@ -75,7 +80,8 @@ def render_page(page, filename):
7580

7681
# body = hr.Body()
7782

78-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text"))
83+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
84+
# "but this is enough to show that we can do some text"))
7985
# body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
8086

8187
# page.append(body)
@@ -94,7 +100,8 @@ def render_page(page, filename):
94100

95101
# body = hr.Body()
96102

97-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
103+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
104+
# "but this is enough to show that we can do some text",
98105
# style="text-align: center; font-style: oblique;"))
99106

100107
# page.append(body)
@@ -113,7 +120,8 @@ def render_page(page, filename):
113120

114121
# body = hr.Body()
115122

116-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
123+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
124+
# "but this is enough to show that we can do some text",
117125
# style="text-align: center; font-style: oblique;"))
118126

119127
# body.append(hr.Hr())
@@ -134,7 +142,8 @@ def render_page(page, filename):
134142

135143
# body = hr.Body()
136144

137-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
145+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
146+
# "but this is enough to show that we can do some text",
138147
# style="text-align: center; font-style: oblique;"))
139148

140149
# body.append(hr.Hr())
@@ -161,7 +170,8 @@ def render_page(page, filename):
161170

162171
# body.append( hr.H(2, "PythonClass - Class 6 example") )
163172

164-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
173+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
174+
# "but this is enough to show that we can do some text",
165175
# style="text-align: center; font-style: oblique;"))
166176

167177
# body.append(hr.Hr())
@@ -200,7 +210,8 @@ def render_page(page, filename):
200210

201211
# body.append( hr.H(2, "PythonClass - Class 6 example") )
202212

203-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
213+
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, "
214+
# "but this is enough to show that we can do some text",
204215
# style="text-align: center; font-style: oblique;"))
205216

206217
# body.append(hr.Hr())

Examples/Session07/simple_classes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ def __init__(self, x, y):
4646
def get_color(self):
4747
return self.color
4848

49+
def get_size(self):
50+
return self.size
51+
52+
class Rect:
53+
54+
def __init__(self, w, h):
55+
self.w = w
56+
self.h = h
57+
58+
def get_size(self):
59+
return self.w * self.h
60+
4961

5062
p3 = Point3(4, 5)
5163
print(p3.size)

Examples/Session08/circle.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@
99

1010

1111
class Circle:
12-
pass
12+
@staticmethod
13+
def a_method(x):
14+
return x**2
15+
16+
def regular_method(self):
17+
print(self)
18+
19+
@classmethod
20+
def class_method(cls):
21+
print(cls)
22+
23+
24+

Examples/Session09/capitalize/capitalize/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)