Skip to content

Commit 4f8a5d9

Browse files
committed
cleaned up session 09 and added ipython log.
1 parent c2effdc commit 4f8a5d9

File tree

4 files changed

+192
-5
lines changed

4 files changed

+192
-5
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+

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)