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)

0 commit comments

Comments
 (0)