Skip to content

Commit 37c20cf

Browse files
committed
2 parents 2d3cae5 + 9e9411e commit 37c20cf

File tree

6 files changed

+253
-8
lines changed

6 files changed

+253
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
slides_sources/build
22

3+
.DS_Store
34
#ignore compiled files, sublime workspace and project files
45
*.pyc
56
*.sublime*

Examples/Session09/decorators.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def new_function(*args, **kwargs):
99

1010

1111
def add(a, b):
12-
print "Function 'add' called with args: %r" % locals()
12+
# print "Function 'add' called with args: %r" % locals()
1313
result = a + b
14-
print "\tResult --> %r" % result
14+
# print "\tResult --> %r" % result
1515
return result
1616

1717

@@ -32,7 +32,7 @@ def simple_add(a, b):
3232
return a + b
3333

3434

35-
class Memoize:
35+
class Memoize(object):
3636
"""
3737
memoize decorator from avinash.vora
3838
http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
@@ -49,9 +49,13 @@ def __call__(self, *args): # runs when memoize instance is called
4949
return self.memoized[args]
5050

5151

52-
@Memoize
52+
#@Memoize
5353
def sum2x(n):
5454
return sum(2 * i for i in xrange(n))
55+
sum2x = Memoize(sum2x)
56+
57+
58+
5559

5660
import time
5761

@@ -69,3 +73,5 @@ def timed(*args, **kwargs):
6973
@Memoize
7074
def sum2x(n):
7175
return sum(2 * i for i in xrange(n))
76+
77+

Examples/Session09/ipythonlog.txt

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# IPython log file
2+
3+
4+
get_ipython().magic(u'run decorators.py')
5+
add
6+
add(2,3)
7+
get_ipython().magic(u'run decorators.py')
8+
add(2,3)
9+
new_add = substitute(add)
10+
new_add(2,3)
11+
@substitute
12+
def fun(1,2,3)
13+
@substitute
14+
def fun(1,2,3):
15+
print "in fun"
16+
17+
@substitute
18+
def fun(a,b):
19+
print "in fun"
20+
21+
fun()
22+
@logged_func
23+
def fun(a,b):
24+
print "in fun"
25+
26+
fun(2,3)
27+
fun(2,3,fred=4)
28+
get_ipython().magic(u'run decorators.py')
29+
get_ipython().magic(u'timeit sum2x(10)')
30+
get_ipython().magic(u'run decorators.py')
31+
get_ipython().magic(u'timeit sum2x(10)')
32+
sum2x(100000000)
33+
sum2x(100000000)
34+
sum2x(10000000)
35+
sum2x(10000000)
36+
type(sum2x)
37+
sum2x.function
38+
sum2x.function(10)
39+
sum2x.memoized
40+
@Memoize
41+
def other_fun():
42+
def other_fun(x):
43+
44+
@Memoize
45+
def fun2(x):
46+
return x**100
47+
48+
fun2(4)
49+
fun2(4)
50+
fun2.memoized
51+
@Memoize
52+
def fun2asdf(x):
53+
return x**100
54+
55+
get_ipython().magic(u'run decorators.py')
56+
sum2x(1e6)
57+
sum2x(1000000)
58+
get_ipython().magic(u'run decorators.py')
59+
sum2x(1000000)
60+
sum2x(1000000)
61+
sum2x(10000000)
62+
sum2x(10000000)
63+
get_ipython().magic(u'paste')
64+
@Memoize
65+
@timed_func
66+
def sum2x(n):
67+
return sum(2 * i for i in xrange(n))
68+
sum2x(1000000)
69+
sum2x(1000000)
70+
get_ipython().magic(u'run context_managers.py')
71+
with Context(False):
72+
print something
73+
74+
with Context(False):
75+
print "something"
76+
77+
get_ipython().magic(u'paste')
78+
with Context(True) as foo:
79+
print 'This is in the context'
80+
raise RuntimeError('this is the error message')
81+
with Context(False):
82+
raise RuntimeError('this is the error message')
83+
84+
with context(False):
85+
raise RuntimeError('this is the error message')
86+
87+
with context(True):
88+
raise RuntimeError('this is the error message')
89+
90+
get_ipython().magic(u'run timer_context.py')
91+
with Timer as t:
92+
[x^2 for x in range(100000000)]
93+
94+
get_ipython().magic(u'run timer_context.py')
95+
with Timer as t:
96+
[x^2 for x in range(100000000)]
97+
98+
with Timer as t:
99+
[x^2 for x in range(100000000)]
100+
101+
get_ipython().magic(u'run timer_context.py')
102+
with Timer as t:
103+
[x^2 for x in range(100000000)]
104+
105+
with Timer() as t:
106+
[x^2 for x in range(100000000)]
107+
108+
with Timer() as t:
109+
[x^2 for x in range(1000000)]
110+
111+
get_ipython().magic(u'run timer_context.py')
112+
with Timer() as t:
113+
[x^2 for x in range(1000000)]
114+
115+
get_ipython().magic(u'run timer_context.py')
116+
with Timer() as t:
117+
[x^2 for x in range(1000000)]
118+
119+
get_ipython().magic(u'run timer_context.py')
120+
with Timer() as t:
121+
[x^2 for x in range(1000000)]
122+
123+
with Timer() as t:
124+
[x^2 for x in range(10000000)]
125+
126+
t
127+
t.elapsed
128+
with Timer() as t:
129+
[x^2 for x in range(10000000)]
130+
raise RuntimeError("this is an error")
131+
132+
with Timer() as t:
133+
[x^2 for x in range(10000000)]
134+
raise RuntimeError("this is an error")
135+
136+
with Timer() as t:
137+
[x^2 for x in range(10000000)]
138+
raise RuntimeError("this is an error")
139+
140+
get_ipython().magic(u'run timer_context.py')
141+
with Timer() as t:
142+
[x^2 for x in range(10000000)]
143+
raise RuntimeError("this is an error")
144+
145+
get_ipython().magic(u'run timer_context.py')
146+
with Timer() as t:
147+
[x^2 for x in range(10000000)]
148+
raise RuntimeError("this is an error")
149+
150+
get_ipython().magic(u'run timer_context.py')
151+
with Timer() as t:
152+
[x^2 for x in range(10000000)]
153+
raise RuntimeError("this is an error")
154+
155+
get_ipython().magic(u'pinfo isinstance')
156+
get_ipython().magic(u'run timer_context.py')
157+
with Timer() as t:
158+
[x^2 for x in range(10000000)]
159+
raise RuntimeError("this is an error")
160+
161+
get_ipython().magic(u'run timer_context.py')
162+
with Timer() as t:
163+
[x^2 for x in range(10000000)]
164+
raise RuntimeError("this is an error")
165+
166+
with Timer() as t:
167+
[x^2 for x in range(10000000)]
168+
raise RuntimeError("this is an error")
169+
print "after Exception"
170+
171+
with Timer() as t:
172+
[x^2 for x in range(10000000)]
173+
raise RuntimeError("this is an error")
174+
print "after Exception"
175+
176+
with Timer() as t:
177+
[x^2 for x in range(10000000)]
178+
raise ValueError("this is an error")
179+
print "after Exception"
180+

Solutions/Session08/slice_sparse.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,23 @@ def __setitem__(self, index, value):
5454
if step is None:
5555
step = 1
5656
key = start
57+
new_values = []
58+
new_keys = []
5759
for each in value:
58-
print('key', key)
59-
print('each', each)
60-
self[key] = each
60+
#print('key', key)
61+
#print('each', each)
62+
if key < stop:
63+
self[key] = each
64+
else:
65+
# now instead of replacing values, we need to add (a) value(s) in the center,
66+
# and move stuff over, probably want to collect all of the changes,
67+
# and then make a new dictionary
68+
new_values.append(each)
69+
new_keys.append(key)
6170
key += step
71+
if new_keys:
72+
self.add_in_slice(new_keys, new_values)
73+
6274
else:
6375
raise TypeError("index must be int or slice")
6476

@@ -72,6 +84,47 @@ def set_single_value(self, key, value):
7284
# remove a key from our dictionary.
7385
self.sparse_array.pop(key, None)
7486

87+
def add_in_slice(self, new_keys, new_values):
88+
# sometimes we need to add in extra values
89+
# add in the extra values, any existing values
90+
# greater than the last key of the new data
91+
# will be increased by how many
92+
#print('old dict', self.sparse_array)
93+
#print new_keys
94+
#print new_values
95+
new_dict = {}
96+
slice_length = len(new_keys)
97+
for k, v in self.sparse_array.iteritems():
98+
if k >= new_keys[-1]:
99+
#print('change keys')
100+
# if greater than slice, change key
101+
new_dict[k + slice_length] = v
102+
elif k in new_keys:
103+
#print('change values')
104+
# if this is a key we are changing, change it,
105+
# unless we are changing to a zero...
106+
new_value = values[new_keys.index(k)]
107+
if new_value != 0:
108+
new_dict[k] = new_value
109+
else:
110+
#print('remains the same')
111+
new_dict[k] = v
112+
#print new_dict
113+
# what if our new key was not previously in the dictionary?
114+
# stick it in now
115+
for k in new_keys:
116+
if k not in new_dict.keys():
117+
#print 'put in dict'
118+
#print('key', k)
119+
#print('value', new_values[new_keys.index(k)])
120+
new_dict[k] = new_values[new_keys.index(k)]
121+
#print new_dict
122+
# note we don't want to do update, since we need to make sure we are
123+
# getting rid of the old keys, when we moved the value to a new key
124+
self.sparse_array = new_dict
125+
# now we need to increase the length by the amount we increased our array by
126+
self.length += slice_length
127+
75128
def __delitem__(self, key):
76129
# we probably need to move the keys if we are not deleting the last
77130
# number, use pop in case it was a zero

Solutions/Session08/test_slice_sparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_get_slice():
3131
def test_set_slice():
3232
my_array, my_sparse = set_up()
3333
my_sparse[2:4] = [2, 3, 4]
34+
#print my_sparse[:]
3435
assert my_sparse[:] == [2, 0, 2, 3, 4, 3, 0, 0, 0, 4, 5, 6, 0, 2, 9]
3536

3637
def test_get_length():
@@ -97,3 +98,7 @@ def test_indices_change():
9798
assert (my_sparse[3] == 3)
9899

99100

101+
102+
103+
104+

slides_sources/source/session09.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ Next Week
12511251

12521252
We'll be talking about Unicode. Read:
12531253

1254-
rst-class:: medium centered
1254+
.. rst-class:: medium centered
12551255

12561256
The Absolute Minimum Every Software Developer Absolutely, Positively
12571257
Must Know About Unicode and Character Sets (No Excuses!)

0 commit comments

Comments
 (0)