additional_ipython/script.rst
changeset 414 f76622c8cbd9
parent 327 28a54b57049d
child 416 06ac45f4de88
equal deleted inserted replaced
407:abfd357603c2 414:f76622c8cbd9
    37 
    37 
    38     $ ipython -pylab
    38     $ ipython -pylab
    39 
    39 
    40 on the terminal
    40 on the terminal
    41 
    41 
    42 {{{ shit to terminal and type ipython -pylab }}}
    42 {{{ shift to terminal and type ipython -pylab }}}
    43 
    43 
    44 We shall first make a plot and then view the history and save it.
    44 We shall first make a plot and then view the history and save it.
    45 ::
    45 ::
    46 
    46 
    47     x = linspace(-2*pi, 2*pi, 100)
    47     x = linspace(-2*pi, 2*pi, 100)
    64 
    64 
    65 As you can see, it displays a list of recent commands that we typed. Every
    65 As you can see, it displays a list of recent commands that we typed. Every
    66 command has a number in front, to specify in which order and when it was typed.
    66 command has a number in front, to specify in which order and when it was typed.
    67 
    67 
    68 Please note that there is a % sign before the hist command. This implies that 
    68 Please note that there is a % sign before the hist command. This implies that 
    69 %hist is a command that is specific to IPython and not available in vannila 
    69 %hist is a command that is specific to IPython and not available in the vannila 
    70 Python interpreter. These type of commands are called as magic commands.
    70 Python interpreter. These type of commands are called as magic commands.
    71 
    71 
    72 Also note that, the =%hist= itself is a command and is displayed as the most
    72 Also note that, the =%hist= itself is a command and is displayed as the most
    73 recent command. This implies that anything we type in is stored as history, 
    73 recent command. We should not that anything we type in is stored as history, 
    74 irrespective of whether it is command or an error or IPython magic command.
    74 irrespective of whether it is command or an error or IPython magic command.
    75 
    75 
    76 If we want only the recent 5 to be displayed, we pass the number as an argument
    76 If we want only the recent 5 commands to be displayed, we pass the number as an argument
    77 to =%hist= command. Hence
    77 to =%hist= command. Hence
    78 ::
    78 ::
    79 
    79 
    80     %hist 5 
    80     %hist 5 
    81 
    81 
    97 displays the commands from 5 to 10
    97 displays the commands from 5 to 10
    98 
    98 
    99 Now that we have the history, we would like to save the required line of code
    99 Now that we have the history, we would like to save the required line of code
   100 from history. This is possible by using the =%save= command.
   100 from history. This is possible by using the =%save= command.
   101 
   101 
   102 Before we do that, let us first look at history and identify what lines of code
   102 Before we do that, let us first look at history and identify what lines of code we require.Type
   103 we require.Type
       
   104 ::
   103 ::
   105 
   104 
   106     %hist
   105     %hist
   107 
   106 
   108 
   107 
   109 {{{ point to the lines }}}
   108 {{{ point to the lines }}}
   110 
   109 
   111 The first command is linspace. But second command is a command that gave us an
   110 The first command is linspace. But second command is a command that gave us an
   112 error. Hence we do not need seconf. The commands from third to sixth are 
   111 error. Hence we do not need second command. The commands from third to sixth are 
   113 required. The seventh command although is correct, we do not need it since we
   112 required. The seventh command although is correct, we do not need it since we
   114 are setting the title correctly in the eigthth command.
   113 are setting the title correctly in the eigthth command.
   115 
   114 
   116 So we need first third to sixth and the eigthth command for our program.
   115 So we need first third to sixth and the eigthth command for our program.
   117 Hence the syntax of =%save= is
   116 Hence the syntax of =%save= is
   152 We use the IPython magic command =%run= to do this. Type
   151 We use the IPython magic command =%run= to do this. Type
   153 ::
   152 ::
   154 
   153 
   155    %run -i /home/fossee/plot_script.py
   154    %run -i /home/fossee/plot_script.py
   156 
   155 
   157 The script runs but we do not see the plot. This happens because we are running
   156 The script runs but we do not see the plot. This happens because when we are running
   158 a script and we are not in interactive mode anymore.
   157 a script and we are not in interactive mode anymore.
   159 
   158 
   160 Hence on your terminal type
   159 Hence on your terminal type
   161 ::
   160 ::
   162 
   161 
   167 {{{ Pause here and try out the following exercises }}}
   166 {{{ Pause here and try out the following exercises }}}
   168 
   167 
   169 %% 3 %% Use %hist and %save and create a script that has show in it and run it
   168 %% 3 %% Use %hist and %save and create a script that has show in it and run it
   170         to produce and show the plot.
   169         to produce and show the plot.
   171 
   170 
       
   171 
   172 {{{ continue from paused state }}}
   172 {{{ continue from paused state }}}
   173 
   173 
   174 We first look at the history using
   174 We first look at the history using
   175 ::
   175 ::
   176 
   176 
   179 Then save the script using
   179 Then save the script using
   180 ::
   180 ::
   181 
   181 
   182     %save /home/fossee/show_included.py 1 3-6 8 10 13
   182     %save /home/fossee/show_included.py 1 3-6 8 10 13
   183     %run -i /home/fossee/show_included.py
   183     %run -i /home/fossee/show_included.py
       
   184     show()    
   184 
   185 
   185 We get the desired plot.
   186 We get the desired plot.
   186 
   187 
   187 The reason for including a -i after run is to tell the interpreter that if any
   188 The reason for including a -i after run is to tell the interpreter that if any
   188 name is not found in script, search for it in the interpreter. Hence all these
   189 name is not found in script, search for it in the interpreter. Hence all these
   193 
   194 
   194 %% 4 %% Run the script without using the -i option. Do you find any difference?
   195 %% 4 %% Run the script without using the -i option. Do you find any difference?
   195 
   196 
   196 {{{ continue from paused state }}}
   197 {{{ continue from paused state }}}
   197 
   198 
   198 We see that it raises nameerror saying the name linspace is not found.
   199 We see that it raises NameError saying that the name linspace is not found.
   199 
   200 
   200 {{{ Show summary slide }}}
   201 {{{ Show summary slide }}}
   201 
   202 
   202 This brings us to the end of the tutorial.
   203 This brings us to the end of the tutorial.
   203 we have looked at 
   204 we have looked at 
   207  * saving the required lines of code in required order using %save
   208  * saving the required lines of code in required order using %save
   208  * using %run -i command to run the saved script
   209  * using %run -i command to run the saved script
   209 
   210 
   210 {{{ Show the "sponsored by FOSSEE" slide }}}
   211 {{{ Show the "sponsored by FOSSEE" slide }}}
   211 
   212 
   212 #[Nishanth]: Will add this line after all of us fix on one.
   213 
   213 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   214 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   214 
   215 
   215 Hope you have enjoyed and found it useful.
   216 Hope you have enjoyed and found it useful.
   216 Thank you!
   217 Thank you!
   217  
   218