Skip to content

Commit 726c8da

Browse files
started framework for the first class
1 parent eb48c37 commit 726c8da

21 files changed

Lines changed: 321 additions & 39 deletions

week-01/code/circle.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
circle class -- my solution to the exercise
5+
6+
This version with doctests...
7+
8+
"""
9+
10+
import math
11+
12+
class Circle(object):
13+
"""
14+
A class that defines a circle.
15+
16+
You can get and set either the radius or the diameter with properties. Example:
17+
18+
>>> c = Circle(4)
19+
>>> c.radius
20+
4.0
21+
>>> c.diameter
22+
8.0
23+
>>> c.diameter = 4
24+
>>> c.radius
25+
2.0
26+
>>> c.area
27+
12.566370614359172
28+
>>> c2 = Circle(6)
29+
>>> c + c2
30+
Circle(8.000000)
31+
>>> str(c2)
32+
'Circle Object with radius: 6.000000'
33+
>>>
34+
"""
35+
def __init__(self, radius):
36+
self.radius = float(radius)
37+
38+
@property
39+
def diameter(self):
40+
return self.radius * 2.0
41+
@diameter.setter
42+
def diameter(self, value):
43+
self.radius = value / 2.0
44+
45+
@property
46+
def area(self):
47+
return self.radius**2 * math.pi
48+
49+
def __add__(self, other):
50+
return Circle(self.radius + other.radius)
51+
52+
def __repr__(self):
53+
return "Circle(%f)"%self.radius
54+
55+
def __str__(self):
56+
return "Circle Object with radius: %f"%self.radius
57+
58+
if __name__ == "__main__":
59+
import doctest
60+
print doctest.testmod()

week-01/code/codingbat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
def sleep_in(weekday, vacation):
12+
return not (weekday == True and vacation == False)
13+
14+

week-01/code/codingbat_unittest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
test file for codingbat module
5+
6+
This version used unittest
7+
"""
8+
9+
import unittest
10+
from codingbat import sleep_in
11+
12+
class Test_sleep_in(unittest.TestCase):
13+
14+
def test_false_false(self):
15+
self.assertTrue( sleep_in(False, False) )
16+
17+
def test_true_false(self):
18+
self.assertFalse( sleep_in(True, False) )
19+
20+
def test_false_true(self):
21+
self.assertTrue( sleep_in(False, True) )
22+
23+
def test_true_true(self):
24+
self.assertTrue( sleep_in(True, True) )
25+
26+
if __name__ == "__main__":
27+
unittest.main()
28+
29+

week-01/code/profile_example.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
code for testing profiling
5+
6+
borrowed from:
7+
8+
http://pysnippet.blogspot.com/2009/12/profiling-your-python-code.html
9+
"""
10+
11+
def odd_numbers(max):
12+
""" Returns a list with all odd numbers between 0 to max (inclusive) """
13+
l = list()
14+
for i in xrange(max+1):
15+
if (i & 1):
16+
l.append(i)
17+
return l
18+
19+
def sum_odd_numbers(max):
20+
""" Sum all odd numbers between 0 to max (inclusive) """
21+
odd_nbrs = odd_numbers(max)
22+
23+
res = 0
24+
for odd in odd_nbrs:
25+
res += odd
26+
return res
27+
28+
def main():
29+
# Run this 100 times to make it measurable
30+
for i in xrange(100):
31+
print sum_odd_numbers(1024)
32+
33+
if __name__ == '__main__':
34+
main()
35+

week-01/code/test_codingbat.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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
10+
11+
def test_false_false():
12+
assert sleep_in(False, False)
13+
14+
def test_true_false():
15+
assert not ( sleep_in(True, False) )
16+
17+
def test_false_true():
18+
assert sleep_in(False, True)
19+
20+
def test_true_true():
21+
assert sleep_in(True, True)
22+
23+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
pytest example of a parameterized test
5+
"""
6+
import pytest
7+
8+
# a (really simple) function to test
9+
def add(a, b):
10+
"""
11+
returns the sum of a and b
12+
"""
13+
return a + b
14+
15+
# now some test data:
16+
17+
test_data = [ ( ( 2, 3), 5),
18+
( (-3, 2), -1),
19+
( ( 2, 0.5), 2.5),
20+
( ( "this", "that"), "thisthat"),
21+
( ( [1,2,3], [6,7,8]), [1,2,3,6,7,8]),
22+
]
23+
24+
@pytest.mark.parametrize(("input", "result"), test_data)
25+
def test_add(input, result):
26+
assert add(*input) == result

week-01/code/test_random_nose.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
port of the random unit tests from the python docs to nose/py.test
5+
"""
6+
7+
import random
8+
import nose.tools
9+
10+
seq = range(10)
11+
12+
def test_shuffle():
13+
# make sure the shuffled sequence does not lose any elements
14+
random.shuffle(seq)
15+
seq.sort()
16+
assert seq == range(10)
17+
18+
@nose.tools.raises(TypeError)
19+
def test_shuffle_immutable():
20+
# should raise an exception for an immutable sequence
21+
random.shuffle( (1,2,3) )
22+
23+
def test_choice():
24+
element = random.choice(seq)
25+
assert (element in seq)
26+
27+
def test_sample():
28+
for element in random.sample(seq, 5):
29+
assert element in seq
30+
31+
@nose.tools.raises(ValueError)
32+
def test_sample_too_large():
33+
random.sample(seq, 20)

week-01/code/test_random_pytest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
port of the random unit tests from the python docs to nose/py.test
5+
"""
6+
7+
import random
8+
import pytest
9+
10+
11+
seq = range(10)
12+
13+
def test_shuffle():
14+
# make sure the shuffled sequence does not lose any elements
15+
random.shuffle(seq)
16+
seq.sort()
17+
print "seq:", seq
18+
assert seq == range(10)
19+
20+
def test_shuffle_immutable():
21+
pytest.raises(TypeError, random.shuffle, (1,2,3) )
22+
23+
def test_choice():
24+
element = random.choice(seq)
25+
assert (element in seq)
26+
27+
def test_sample():
28+
for element in random.sample(seq, 5):
29+
assert element in seq
30+
31+
def test_sample_too_large():
32+
with pytest.raises(ValueError):
33+
random.sample(seq, 20)

week-01/code/timing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
timing example
5+
"""
6+
7+
def primes_stupid(N):
8+
"""
9+
a really simple way to compute the first N prime numbers
10+
"""
11+
primes = [2]
12+
i = 3
13+
while len(primes) < N:
14+
for j in range(2, i/2): # the "/2" is an optimization -- no point in checking even numbers
15+
if not i % j: # it's not prime
16+
break
17+
else:
18+
primes.append(i)
19+
i += 1
20+
21+
return primes
22+
23+
if __name__ == "__main__":
24+
import timeit
25+
26+
print "running the timer:"
27+
run_time = timeit.timeit("primes_stupid(5)",
28+
setup="from __main__ import primes_stupid",
29+
number=100000) # default: 1000000
30+
print "it took:", run_time

week-01/code/unittest_example.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
import unittest
3+
4+
class TestSequenceFunctions(unittest.TestCase):
5+
6+
def setUp(self):
7+
self.seq = range(10)
8+
9+
def test_shuffle(self):
10+
# make sure the shuffled sequence does not lose any elements
11+
random.shuffle(self.seq)
12+
self.seq.sort()
13+
self.assertEqual(self.seq, range(10))
14+
15+
# should raise an exception for an immutable sequence
16+
self.assertRaises(TypeError, random.shuffle, [1,2,3])
17+
18+
def test_choice(self):
19+
element = random.choice(self.seq)
20+
self.assertTrue(element in self.seq)
21+
22+
def test_sample(self):
23+
with self.assertRaises(ValueError):
24+
random.sample(self.seq, 20)
25+
for element in random.sample(self.seq, 5):
26+
self.assertTrue(element in self.seq)
27+
28+
if __name__ == '__main__':
29+
unittest.main()

0 commit comments

Comments
 (0)