-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch01-code-listing.py
More file actions
242 lines (106 loc) · 3.16 KB
/
ch01-code-listing.py
File metadata and controls
242 lines (106 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# coding: utf-8
# # Chapter 1: Computing with Python
# Robert Johansson
#
# Source code listings for [Numerical Python - A Practical Techniques Approach for Industry](http://www.apress.com/9781484205549) (ISBN 978-1-484205-54-9).
#
# The source code listings can be downloaded from http://www.apress.com/9781484205549
# ## Interpreter
# In[1]:
get_ipython().run_cell_magic(u'writefile', u'hello.py', u'print("Hello from Python!")')
# In[2]:
get_ipython().system(u'python hello.py')
# In[3]:
get_ipython().system(u'python --version')
# ## Input and output caching
# In[1]:
3 * 3
# In[2]:
In[1]
# In[3]:
Out[1]
# In[4]:
In
# In[5]:
Out
# In[6]:
1+2
# In[7]:
1+2;
# In[8]:
x = 1
# In[9]:
x = 2; x
# ## Documentation
# In[10]:
import os
# In[11]:
# try os.w<TAB>
# In[12]:
import math
# In[13]:
get_ipython().magic(u'pinfo math.cos')
# ## Interaction with System Shell
# In[14]:
get_ipython().system(u'touch file1.py file2.py file3.py')
# In[15]:
get_ipython().system(u'ls file*')
# In[16]:
files = get_ipython().getoutput(u'ls file*')
# In[17]:
len(files)
# In[18]:
files
# In[19]:
file = "file1.py"
# In[20]:
get_ipython().system(u'ls -l $file')
# ## Running scripts from the IPython console
# In[21]:
get_ipython().run_cell_magic(u'writefile', u'fib.py', u'\ndef fib(N): \n """ \n Return a list of the first N Fibonacci numbers.\n """ \n f0, f1 = 0, 1\n f = [1] * N\n for n in range(1, N):\n f[n] = f0 + f1\n f0, f1 = f1, f[n]\n\n return f\n\nprint(fib(10))')
# In[22]:
get_ipython().system(u'python fib.py')
# In[23]:
get_ipython().magic(u'run fib.py')
# In[24]:
fib(6)
# ## Debugger
# In[25]:
fib(1.0)
# In[26]:
get_ipython().magic(u'debug')
# ## Timing and profiling code
# In[27]:
get_ipython().magic(u'timeit fib(100)')
# In[28]:
result = get_ipython().magic(u'time fib(100)')
# In[29]:
len(result)
# In[30]:
import numpy as np
def random_walker_max_distance(M, N):
"""
Simulate N random walkers taking M steps, and return the largest distance
from the starting point achieved by any of the random walkers.
"""
trajectories = [np.random.randn(M).cumsum() for _ in range(N)]
return np.max(np.abs(trajectories))
# In[31]:
get_ipython().magic(u'prun random_walker_max_distance(400, 10000)')
# ## IPython nbconvert
# In[36]:
get_ipython().system(u'ls ch01-code-listing.ipynb')
# In[38]:
get_ipython().system(u'ipython nbconvert --to html ch01-code-listing.ipynb')
# In[40]:
get_ipython().system(u'ipython nbconvert --to pdf ch01-code-listing.ipynb')
# In[41]:
get_ipython().run_cell_magic(u'writefile', u'custom_template.tplx', u"((*- extends 'article.tplx' -*))\n\n((* block title *)) \\title{Document title} ((* endblock title *))\n((* block author *)) \\author{Author's Name} ((* endblock author *))")
# In[42]:
get_ipython().system(u'ipython nbconvert ch01-code-listing.ipynb --to pdf --template custom_template.tplx')
# In[43]:
get_ipython().system(u'ipython nbconvert ch01-code-listing.ipynb --to python')
# # Versions
# In[22]:
get_ipython().magic(u'reload_ext version_information')
get_ipython().magic(u'version_information numpy')