-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathipython.html
More file actions
483 lines (440 loc) · 19.8 KB
/
ipython.html
File metadata and controls
483 lines (440 loc) · 19.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IPython — Python4Astronomers 2.0 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap-astropy.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="top" title="Python4Astronomers 2.0 documentation" href="../index.html" />
<link rel="up" title="Core packages for analysis: IPython, NumPy, and SciPy" href="core.html" />
<link rel="next" title="A Crash Course in Scientific Python: 2D STIS Reduction" href="numpy_scipy.html" />
<link rel="prev" title="Core packages for analysis: IPython, NumPy, and SciPy" href="core.html" />
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,600' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip0").click(function(){
$(".panel0").slideToggle("normal");
});
$(".flip1").click(function(){
$(".panel1").slideToggle("normal");
});
$(".flip2").click(function(){
$(".panel2").slideToggle("normal");
});
$(".flip3").click(function(){
$(".panel3").slideToggle("normal");
});
$(".flip4").click(function(){
$(".panel4").slideToggle("normal");
});
$(".flip5").click(function(){
$(".panel5").slideToggle("normal");
});
$(".flip6").click(function(){
$(".panel6").slideToggle("normal");
});
$(".flip7").click(function(){
$(".panel7").slideToggle("normal");
});
$(".flip8").click(function(){
$(".panel8").slideToggle("normal");
});
$(".flip9").click(function(){
$(".panel9").slideToggle("normal");
});
});
</script>
<style type="text/css">
div.panel0,p.flip0
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip0
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel0
{
display:none;
}
div.panel1,p.flip1
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip1
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel1
{
display:none;
}
div.panel2,p.flip2
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip2
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel2
{
display:none;
}
div.panel3,p.flip3
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip3
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel3
{
display:none;
}
div.panel4,p.flip4
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip4
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel4
{
display:none;
}
div.panel5,p.flip5
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip5
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel5
{
display:none;
}
div.panel6,p.flip6
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip6
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel6
{
display:none;
}
div.panel7,p.flip7
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip7
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel7
{
display:none;
}
div.panel8,p.flip8
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip8
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel8
{
display:none;
}
div.panel9,p.flip9
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip9
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel9
{
display:none;
}
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18709417-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body role="document">
<div class="topbar">
<a class="brand" title="Documentation Home" href="../index.html"><span id="logotext1">python</span><span id="logotext2">4</span><span id="logotext3">astronomers</span></a>
<ul>
<li><a class="homelink" title="Astropy Homepage" href="http://www.astropy.org"></a></li>
</ul>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right">
<a href="numpy_scipy.html" title="A Crash Course in Scientific Python: 2D STIS Reduction">
next »
</a>
</li>
<li class="right">
<a href="core.html" title="Core packages for analysis: IPython, NumPy, and SciPy">
« previous
</a>
|
</li>
<li>
<a href="../index.html">Python4Astronomers 2.0 documentation</a>
»
</li>
<li><a href="core.html" accesskey="U">Core packages for analysis: IPython, NumPy, and SciPy</a> »</li>
<li>IPython</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="ipython">
<h1>IPython<a class="headerlink" href="#ipython" title="Permalink to this headline">¶</a></h1>
<p>IPython is an enhanced Python shell that creates a comprehensive environment
for interactive and exploratory computing. In this section we’ll learn the
basic features of IPython and how it benefits interactive analysis.</p>
<p>Before going further, open a new terminal window and change to your main Python
for Astronomers working directory. Then start IPython by typing “ipython
–matplotlib” at the command prompt:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>$ ipython --matplotlib
</pre></div>
</div>
<p>As we saw in the Introduction and Installation workshops, for interactive data
analysis IPython has a special <code class="docutils literal"><span class="pre">--matplotlib</span></code> command line option which makes
interactive plotting work better from a terminal window by allowing a plot to
come up without blocking subsequent terminal input.</p>
<p>In all of the tutorial examples we will start the session by importing the
core modules numpy and matplotlib as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
</pre></div>
</div>
<p>The abbreviations <code class="docutils literal"><span class="pre">np</span></code> and <code class="docutils literal"><span class="pre">plt</span></code> provide quick access to numpy and
matplotlib routines and are widely used in scientific code.</p>
<div class="admonition-reminder-what-does-import-do admonition">
<p class="first admonition-title">Reminder: What does <code class="docutils literal"><span class="pre">import</span></code> do?</p>
<p>In python only basic functionality is provided in the language itself. Most of the
commands we need are imported from other
<a class="reference external" href="http://docs.python.org/tutorial/modules.html">modules</a>. The
<a class="reference external" href="http://docs.python.org/reference/simple_stmts.html#import">import</a> statement
makes the functions in a module available:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">())</span> <span class="c1"># will fail</span>
<span class="c1"># need to import the time module first</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">()</span> <span class="c1"># prints the system time</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">as</span></code> variant of <code class="docutils literal"><span class="pre">import</span></code> simply saves some typing. <code class="docutils literal"><span class="pre">import</span> <span class="pre">numpy</span> <span class="pre">as</span> <span class="pre">np</span></code>
allows us to type <code class="docutils literal"><span class="pre">np</span></code> instead of <code class="docutils literal"><span class="pre">numpy</span></code> to call a numpy function:</p>
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
</pre></div>
</div>
</div>
<p>IPython with the <code class="docutils literal"><span class="pre">--matplotlib</span></code> command line option provides a Matlab-like environment
allowing very simple and direct commands like the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="keyboard-navigation-and-history">
<h2>Keyboard navigation and history<a class="headerlink" href="#keyboard-navigation-and-history" title="Permalink to this headline">¶</a></h2>
<p>One of the most useful features of IPython is the ability to edit and navigate
you command line history. This lets you quickly re-do commands, perhaps with a
slight variation based on seeing the last result. Try cut-n-pasting the above
lines in an IPython session. This should bring up a plot of a sine wave.</p>
<p>Now hit up-arrow once and get back the <code class="docutils literal"><span class="pre">plt.plot(x,</span> <span class="pre">y)</span></code> line. Hit the left-arrow
key (not backspace) once and type <code class="docutils literal"><span class="pre">**2</span></code> so that the line reads <code class="docutils literal"><span class="pre">plt.plot(x,</span>
<span class="pre">y**2)</span></code>. Now you can hit Return to see the new curve overlayed within the same
plot window. It is not necessary to forward-space to the end of the line, you
can hit Return with the cursor anywhere in the line.</p>
<p>Now say you want to change the <code class="docutils literal"><span class="pre">x</span></code> values slightly. One option is to just hit the
up-arrow 5 times, but a much faster way is to remember that the line started
with <code class="docutils literal"><span class="pre">x</span></code>, so type <code class="docutils literal"><span class="pre">x</span></code> and then start hitting up-arrow. Only lines that
start with <code class="docutils literal"><span class="pre">x</span></code> will be displayed and you are immediately at the
<code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">np.arange(0,</span> <span class="pre">10,</span> <span class="pre">0.2)</span></code> line. Now use the right-arrow and backspace to change <code class="docutils literal"><span class="pre">10</span></code> to
<code class="docutils literal"><span class="pre">15</span></code> and hit Return. Of course <code class="docutils literal"><span class="pre">y</span></code> needs to be recalculated, so hit <code class="docutils literal"><span class="pre">y</span></code>
then up-arrow, then Return. Finally <code class="docutils literal"><span class="pre">pl</span></code> up-arrow and Return. Nice and fast!</p>
<p>Bonus points: to speed up by another factor of several, use Ctrl-p (prev) instead of
up-arrow, Ctrl-n (next) instead of down-arrow, Ctrl-f (forward) instead of
right-arrow and Ctrl-b (back) instead of left-arrow. That way your fingers
never leave the keyboard home keys. Ctrl-a gets you to the beginning of the
line and Ctrl-e gets you to the end of the line. Triple bonus: on a Mac or
Windows machine re-map the Caps-lock key to be Control so it’s right next to
your left pinky. How often do you need Caps-lock?</p>
<p>Your command history is saved between sessions (assuming that you exit IPython
gracefully) so that when you start a new IPython you can use up-arrow to re-do
old commands. You can view your history within the current session by entering
<code class="docutils literal"><span class="pre">history</span></code>.</p>
</div>
<div class="section" id="linux-and-shell-commands">
<h2>Linux and shell commands<a class="headerlink" href="#linux-and-shell-commands" title="Permalink to this headline">¶</a></h2>
<p>A select set of useful linux commands are available from the IPython prompt.
These include <code class="docutils literal"><span class="pre">ls</span></code> (list directory), <code class="docutils literal"><span class="pre">pwd</span></code> (print working directory),
<code class="docutils literal"><span class="pre">cd</span></code> (change directory), and <code class="docutils literal"><span class="pre">rm</span></code> (remove file). Any shell command
can be executed by preceding it with an exclamation point ”!”.</p>
</div>
<div class="section" id="tab-completion">
<h2>Tab completion<a class="headerlink" href="#tab-completion" title="Permalink to this headline">¶</a></h2>
<p>IPython has a very useful tab completion feature that can be used both to
complete file names and to inspect python objects. As an example do:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>ls ~/<TAB>
</pre></div>
</div>
<p>This will list everything in your home directory. You can continue
this way searching through files or hit Return to complete the command.</p>
</div>
<div class="section" id="showing-data-values">
<h2>Showing data values<a class="headerlink" href="#showing-data-values" title="Permalink to this headline">¶</a></h2>
<p>So far we typed <code class="docutils literal"><span class="pre">print</span> <span class="pre">x</span></code> to look at the value of <code class="docutils literal"><span class="pre">x</span></code>. However,
most of the time for interactive analysis it is faster and better to simply
type <code class="docutils literal"><span class="pre">x</span></code> (or whatever the object name) followed by <Return>. This returns
the “representation” of the object, which is often a cleaner and more
informative than the “string” version that gets returned with <code class="docutils literal"><span class="pre">print</span></code>. In
many cases the “representation” of an object is the same as the Python
code used to create that object.</p>
<p>Try:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="s1">'value is </span><span class="si">%d</span><span class="s1">'</span> <span class="o">%</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
<span class="n">y</span>
<span class="k">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="further-resources">
<h2>Further resources<a class="headerlink" href="#further-resources" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://ipython.github.com/ipython-doc/stable/html/index.html">IPython docs page</a></li>
<li><a class="reference external" href="http://ipython.scipy.org/doc/rel-0.9.1/html/config/customization.html">IPython customization</a> :
E.g. to always import certain modules read <a class="reference external" href="http://ipython.scipy.org/doc/rel-0.9.1/html/config/customization.html#the-ipythonrc-approach">The ipythonrc approach</a>
which explains editing <code class="docutils literal"><span class="pre">~/.ipython/ipythonrc</span></code> and setting the
<code class="docutils literal"><span class="pre">import_mod</span></code> configuration.</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><h3>Page Contents</h3>
<ul>
<li><a class="reference internal" href="#">IPython</a><ul>
<li><a class="reference internal" href="#keyboard-navigation-and-history">Keyboard navigation and history</a></li>
<li><a class="reference internal" href="#linux-and-shell-commands">Linux and shell commands</a></li>
<li><a class="reference internal" href="#tab-completion">Tab completion</a></li>
<li><a class="reference internal" href="#showing-data-values">Showing data values</a></li>
<li><a class="reference internal" href="#further-resources">Further resources</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="core.html"
title="previous chapter">Core packages for analysis: IPython, NumPy, and SciPy</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="numpy_scipy.html"
title="next chapter">A Crash Course in Scientific Python: 2D STIS Reduction</a></p>
</div>
</div>
<div class="clearer"></div>
</div>
<footer class="footer">
<p class="pull-right">
<a href="../_sources/core/ipython.txt"
rel="nofollow">Page Source</a>
<a href="#">Back to Top</a></p>
<p>
© Copyright 2011-2015, Smithsonian Astrophysical Observatory under
terms of <a rel="license"
href="http://creativecommons.org/licenses/by/3.0/">CC
Attribution 3.0</a>.<br/>
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.1.
</footer>
</body>
</html>