author | Nishanth <nishanth@fossee.in> |
Tue, 21 Sep 2010 15:40:40 +0530 | |
changeset 174 | b50fa22ab6b8 |
parent 136 | 7f8b6a9fb61d |
child 212 | c3172a51b555 |
permissions | -rw-r--r-- |
131 | 1 |
Hello friends and welcome to the tutorial on Additional Features of IPython |
2 |
||
3 |
{{{ Show the slide containing title }}} |
|
4 |
||
5 |
{{{ Show the slide containing the outline slide }}} |
|
6 |
||
7 |
In this tutorial, we shall look at additional features of IPython that help us |
|
8 |
to retreive the commands that we type on the interpreter and then save them |
|
9 |
into a file and run it. |
|
10 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
11 |
Let us start ipython with pylab loaded, by typing |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
12 |
:: |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
13 |
|
131 | 14 |
$ ipython -pylab |
15 |
||
16 |
on the terminal |
|
17 |
||
18 |
{{{ shit to terminal and type ipython -pylab }}} |
|
19 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
20 |
We shall first make a plot and then view the history and save it. |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
21 |
:: |
131 | 22 |
|
23 |
x = linspace(-2*pi, 2*pi, 100) |
|
24 |
plot(x, xsinx(x)) |
|
25 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
26 |
xsin(x) is actually x * sin(x) |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
27 |
:: |
131 | 28 |
|
29 |
plot(x, x*sin(x)) |
|
30 |
plot(x, sin(x)) |
|
31 |
xlabel("x") |
|
32 |
ylabel("$f(x)$") |
|
33 |
title("x and xsin") |
|
34 |
||
35 |
We now have the plot. Let us look at the commands that we have typed in. The |
|
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
36 |
history can be retreived by using =%hist= command. Type |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
37 |
:: |
131 | 38 |
|
39 |
%hist |
|
40 |
||
41 |
As you can see, it displays a list of recent commands that we typed. Every |
|
42 |
command has a number in front, to specify in which order and when it was typed. |
|
43 |
||
44 |
Please note that there is a % sign before the hist command. This implies that |
|
45 |
%hist is a command that is specific to IPython and not available in vannila |
|
46 |
Python interpreter. These type of commands are called as magic commands. |
|
47 |
||
48 |
Also note that, the =%hist= itself is a command and is displayed as the most |
|
49 |
recent command. This implies that anything we type in is stored as history, |
|
50 |
irrespective of whether it is command or an error or IPython magic command. |
|
51 |
||
52 |
If we want only the recent 5 to be displayed, we pass the number as an argument |
|
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
53 |
to =%hist= command. Hence |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
54 |
:: |
131 | 55 |
|
56 |
%hist 5 |
|
57 |
||
58 |
displays the recent 5 commands, inclusive of the =%hist= command. |
|
59 |
The default number is 40. |
|
60 |
||
61 |
{{{ Pause here and try out the following exercises }}} |
|
62 |
||
63 |
%% 1 %% Read through the %hist documenatation and find out how can we list all |
|
64 |
the commands between 5 and 10 |
|
65 |
||
66 |
{{{ continue from paused state }}} |
|
67 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
68 |
As we can see from =%hist= documentation, |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
69 |
:: |
131 | 70 |
|
71 |
%hist 5 10 |
|
72 |
||
73 |
displays the commands from 5 to 10 |
|
74 |
||
75 |
Now that we have the history, we would like to save the required line of code |
|
76 |
from history. This is possible by using the =%save= command. |
|
77 |
||
78 |
Before we do that, let us first look at history and identify what lines of code |
|
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
79 |
we require.Type |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
80 |
:: |
131 | 81 |
|
82 |
%hist |
|
83 |
||
84 |
||
85 |
{{{ point to the lines }}} |
|
86 |
||
87 |
The first command is linspace. But second command is a command that gave us an |
|
88 |
error. Hence we do not need seconf. The commands from third to sixth are |
|
89 |
required. The seventh command although is correct, we do not need it since we |
|
90 |
are setting the title correctly in the eigthth command. |
|
91 |
||
92 |
So we need first third to sixth and the eigthth command for our program. |
|
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
93 |
Hence the syntax of =%save= is |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
94 |
:: |
131 | 95 |
|
96 |
%save /home/fossee/plot_script.py 1 3-6 8 |
|
97 |
||
98 |
{{{ point to the output of the command }}} |
|
99 |
||
100 |
The command saves first and then third to sixth and eighth lines of code into |
|
101 |
the specified file. |
|
102 |
||
103 |
The first argument to %save is the path of file to save the commands and the |
|
104 |
arguments there after are the commands to be saved in the given order. |
|
105 |
||
106 |
{{{ goto the file and open it and show it }}} |
|
107 |
||
108 |
{{{ Pause here and try out the following exercises }}} |
|
109 |
||
110 |
%% 2 %% change the label on y-axis to "y" and save the lines of code |
|
111 |
accordingly |
|
112 |
||
113 |
{{{ continue from paused state }}} |
|
114 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
115 |
we use the command =ylabel= on interpreter as |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
116 |
:: |
131 | 117 |
|
118 |
ylabel("y") |
|
119 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
120 |
and then do |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
121 |
:: |
131 | 122 |
|
123 |
%save /home/fossee/example_plot.py 1 3-6 10 |
|
124 |
||
125 |
Now that we have the required lines of code in a file, let us learn how to run |
|
126 |
the file as a python script. |
|
127 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
128 |
We use the IPython magic command =%run= to do this. Type |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
129 |
:: |
131 | 130 |
|
131 |
%run -i /home/fossee/plot_script.py |
|
132 |
||
133 |
The script runs but we do not see the plot. This happens because we are running |
|
134 |
a script and we are not in interactive mode anymore. |
|
135 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
136 |
Hence on your terminal type |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
137 |
:: |
131 | 138 |
|
139 |
show() |
|
140 |
||
141 |
to show the plot. |
|
142 |
||
143 |
{{{ Pause here and try out the following exercises }}} |
|
144 |
||
145 |
%% 3 %% Use %hist and %save and create a script that has show in it and run it |
|
146 |
to produce and show the plot. |
|
147 |
||
148 |
{{{ continue from paused state }}} |
|
149 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
150 |
We first look at the history using |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
151 |
:: |
131 | 152 |
|
153 |
%hist 20 |
|
154 |
||
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
155 |
Then save the script using |
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
156 |
:: |
131 | 157 |
|
158 |
%save /home/fossee/show_included.py 1 3-6 8 10 13 |
|
159 |
%run -i /home/fossee/show_included.py |
|
160 |
||
161 |
We get the desired plot. |
|
162 |
||
163 |
The reason for including a -i after run is to tell the interpreter that if any |
|
164 |
name is not found in script, search for it in the interpreter. Hence all these |
|
165 |
sin, plot, pi and show which are not available in script, are taken from the |
|
166 |
interpreter and used to run the script. |
|
167 |
||
168 |
{{{ Show summary slide }}} |
|
169 |
||
170 |
This brings us to the end of the tutorial. |
|
171 |
we have looked at |
|
136
7f8b6a9fb61d
added a newline before :: so that a colon does not appear in html
nishanth
parents:
131
diff
changeset
|
172 |
|
131 | 173 |
* Retreiving history using =%hist= command |
174 |
* Vieweing only a part of history by passing an argument to %hist |
|
175 |
* saving the required lines of code in required order using %save |
|
176 |
* using %run -i command to run the saved script |
|
177 |
||
178 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
|
179 |
||
180 |
#[Nishanth]: Will add this line after all of us fix on one. |
|
181 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
182 |
||
183 |
Hope you have enjoyed and found it useful. |
|
184 |
Thankyou |
|
185 |
||
186 |
.. Author : Nishanth |
|
187 |
Internal Reviewer 1 : |
|
188 |
Internal Reviewer 2 : |
|
189 |
External Reviewer : |