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