Skip to content

Commit 5399118

Browse files
committed
finished up session 08
1 parent 3ed5433 commit 5399118

File tree

8 files changed

+405
-46
lines changed

8 files changed

+405
-46
lines changed

Examples/Session08/iterator_1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Simple iterator examples
5+
"""
6+
7+
8+
class IterateMe_1(object):
9+
"""
10+
About as simple an iterator as you can get:
11+
12+
returns the sequence of numbers from zero to 4
13+
( like xrange(4) )
14+
"""
15+
def __init__(self, stop=5):
16+
self.current = -1
17+
self.stop = stop
18+
def __iter__(self):
19+
return self
20+
def next(self):
21+
self.current += 1
22+
if self.current < self.stop:
23+
return self.current
24+
else:
25+
raise StopIteration
26+
27+
if __name__ == "__main__":
28+
29+
print "Testing the iterator"
30+
for i in IterateMe_1():
31+
print i
32+

Examples/Session08/my_for.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
hand writing 'for'
5+
6+
demonstrates how for interacts with an iterable
7+
"""
8+
9+
10+
l = [1,2,3,4,5,]
11+
12+
13+
def my_for(an_iterable, func):
14+
"""
15+
Emulation of a for loop.
16+
17+
func() will be called with each item in an_iterable
18+
19+
:param an_iterable: anything that satisfies the interation protocol
20+
21+
:param func: a callable -- it will be called, passing in each item
22+
in an_iterable.
23+
24+
"""
25+
# equiv of "for i in l:"
26+
iterator = iter(an_iterable)
27+
while True:
28+
try:
29+
i = iterator.next()
30+
except StopIteration:
31+
break
32+
func(i)
33+
34+
35+
if __name__ == "__main__":
36+
37+
def print_func(x):
38+
print x
39+
40+
l = [1,2,3,4,5,]
41+
my_for(l, print_func)
42+
43+
t = ('a','b','c','d')
44+
45+
my_for(t, print_func)
46+
47+
48+
49+
50+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
test_generator.py
3+
4+
tests the solution to the generator lab
5+
6+
can be run with py.test or nosetests
7+
"""
8+
9+
import generator_solution as gen
10+
11+
12+
def test_intsum():
13+
14+
g = gen.intsum()
15+
16+
assert g.next() == 0
17+
assert g.next() == 1
18+
assert g.next() == 3
19+
assert g.next() == 6
20+
assert g.next() == 10
21+
assert g.next() == 15
22+
23+
24+
def test_intsum2():
25+
26+
g = gen.intsum2()
27+
28+
assert g.next() == 0
29+
assert g.next() == 1
30+
assert g.next() == 3
31+
assert g.next() == 6
32+
assert g.next() == 10
33+
assert g.next() == 15
34+
35+
36+
def test_doubler():
37+
38+
g = gen.doubler()
39+
40+
assert g.next() == 1
41+
assert g.next() == 2
42+
assert g.next() == 4
43+
assert g.next() == 8
44+
assert g.next() == 16
45+
assert g.next() == 32
46+
47+
for i in range(10):
48+
j = g.next()
49+
50+
assert j == 2**15
51+
52+
53+
def test_fib():
54+
g = gen.fib()
55+
56+
assert g.next() == 1
57+
assert g.next() == 1
58+
assert g.next() == 2
59+
assert g.next() == 3
60+
assert g.next() == 5
61+
assert g.next() == 8
62+
assert g.next() == 13
63+
assert g.next() == 21
64+
65+
66+
def test_prime():
67+
g = gen.prime()
68+
69+
assert g.next() == 2
70+
assert g.next() == 3
71+
assert g.next() == 5
72+
assert g.next() == 7
73+
assert g.next() == 11
74+
assert g.next() == 13
75+
assert g.next() == 17
76+
assert g.next() == 19
77+
assert g.next() == 23
78+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def counter():
2+
print 'counter: starting counter'
3+
i = -3
4+
while i < 3:
5+
i = i + 1
6+
print 'counter: yield', i
7+
yield i
8+
9+
10+
11+
if __name__ == '__main__':
12+
print "the generator function:"
13+
print repr(counter)
14+
print "call generator function"
15+
16+
c = counter()
17+
print "the generator:"
18+
print repr(c)
19+
20+
print 'iterate'
21+
for item in c:
22+
print 'received:', item

Solutions/Session08/sparse_array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __len__(self):
2020
return self.length
2121

2222
def __getitem__(self, key):
23-
try:
23+
try:
2424
return self.sparse_array[key]
2525
except KeyError:
2626
if key >= self.length:
@@ -33,17 +33,17 @@ def __setitem__(self, key, value):
3333
if value != 0:
3434
self.sparse_array[key] = value
3535
else:
36-
# if the value is being set to zero, we probably need to
36+
# if the value is being set to zero, we probably need to
3737
# remove a key from our dictionary.
3838
self.sparse_array.pop(key, None)
3939

4040
def __delitem__(self, key):
41-
# we probably need to move the keys if we are not deleting the last
41+
# we probably need to move the keys if we are not deleting the last
4242
# number, use pop in case it was a zero
4343
if key == self.length - 1:
4444
self.sparse_array.pop(key, None)
4545
else:
46-
# since we need to adjust all of the keys after the one we are
46+
# since we need to adjust all of the keys after the one we are
4747
# deleting, probably most efficient to create a new dictionary
4848
new_dict = {}
4949
for k, v in self.sparse_array.iteritems():

Solutions/Session08/test_sparse_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_delete_last_number():
7070
my_array, my_sparse = set_up()
7171
del(my_sparse[13])
7272
# should get an error?
73-
print 'print some stuf damnit'
73+
print 'print some stuff damnit'
7474
with pytest.raises(IndexError):
7575
my_sparse[13]
7676
assert len(my_sparse) == 13

slides_sources/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'sphinx.ext.todo',
3636
'sphinx.ext.coverage',
3737
'sphinx.ext.pngmath',
38+
# 'sphinx.ext.mathjax',
3839
'sphinx.ext.ifconfig',
3940
'IPython.sphinxext.ipython_console_highlighting',
4041
'IPython.sphinxext.ipython_directive',

0 commit comments

Comments
 (0)