additional_ipython.rst
changeset 234 2b88724a7ee0
parent 233 ab748264f726
child 235 80e4016d747a
equal deleted inserted replaced
233:ab748264f726 234:2b88724a7ee0
     1 .. Author              : Nishanth
       
     2    Internal Reviewer 1 : 
       
     3    Internal Reviewer 2 : 
       
     4    External Reviewer   :
       
     5 
       
     6 .. Prerequisites: Embellinshing plots
       
     7 
       
     8 Script
       
     9 ======
       
    10 
       
    11 
       
    12 Hello friends and welcome to the tutorial on Additional Features of IPython
       
    13 
       
    14 {{{ Show the slide containing title }}}
       
    15 
       
    16 {{{ Show the slide containing the outline slide }}}
       
    17 
       
    18 In this tutorial, we shall look at additional features of IPython that help us
       
    19 to retreive the commands that we type on the interpreter and then save them
       
    20 into a file and run it.
       
    21 
       
    22 Let us start ipython with pylab loaded, by typing
       
    23 ::
       
    24 
       
    25     $ ipython -pylab
       
    26 
       
    27 on the terminal
       
    28 
       
    29 {{{ shit to terminal and type ipython -pylab }}}
       
    30 
       
    31 We shall first make a plot and then view the history and save it.
       
    32 ::
       
    33 
       
    34     x = linspace(-2*pi, 2*pi, 100)
       
    35     plot(x, xsinx(x))
       
    36 
       
    37 xsin(x) is actually x * sin(x)
       
    38 ::
       
    39 
       
    40     plot(x, x*sin(x))
       
    41     plot(x, sin(x))
       
    42     xlabel("x")
       
    43     ylabel("$f(x)$")   
       
    44     title("x and xsin")
       
    45 
       
    46 We now have the plot. Let us look at the commands that we have typed in. The
       
    47 history can be retreived by using =%hist= command. Type
       
    48 ::
       
    49 
       
    50     %hist
       
    51 
       
    52 As you can see, it displays a list of recent commands that we typed. Every
       
    53 command has a number in front, to specify in which order and when it was typed.
       
    54 
       
    55 Please note that there is a % sign before the hist command. This implies that 
       
    56 %hist is a command that is specific to IPython and not available in vannila 
       
    57 Python interpreter. These type of commands are called as magic commands.
       
    58 
       
    59 Also note that, the =%hist= itself is a command and is displayed as the most
       
    60 recent command. This implies that anything we type in is stored as history, 
       
    61 irrespective of whether it is command or an error or IPython magic command.
       
    62 
       
    63 If we want only the recent 5 to be displayed, we pass the number as an argument
       
    64 to =%hist= command. Hence
       
    65 ::
       
    66 
       
    67     %hist 5 
       
    68 
       
    69 displays the recent 5 commands, inclusive of the =%hist= command.
       
    70 The default number is 40.
       
    71 
       
    72 {{{ Pause here and try out the following exercises }}}
       
    73 
       
    74 %% 1 %% Read through the %hist documenatation and find out how can we list all
       
    75         the commands between 5 and 10
       
    76 
       
    77 {{{ continue from paused state }}}
       
    78 
       
    79 As we can see from =%hist= documentation,
       
    80 ::
       
    81 
       
    82     %hist 5 10
       
    83 
       
    84 displays the commands from 5 to 10
       
    85 
       
    86 Now that we have the history, we would like to save the required line of code
       
    87 from history. This is possible by using the =%save= command.
       
    88 
       
    89 Before we do that, let us first look at history and identify what lines of code
       
    90 we require.Type
       
    91 ::
       
    92 
       
    93     %hist
       
    94 
       
    95 
       
    96 {{{ point to the lines }}}
       
    97 
       
    98 The first command is linspace. But second command is a command that gave us an
       
    99 error. Hence we do not need seconf. The commands from third to sixth are 
       
   100 required. The seventh command although is correct, we do not need it since we
       
   101 are setting the title correctly in the eigthth command.
       
   102 
       
   103 So we need first third to sixth and the eigthth command for our program.
       
   104 Hence the syntax of =%save= is
       
   105 ::
       
   106 
       
   107     %save /home/fossee/plot_script.py 1 3-6 8
       
   108 
       
   109 {{{ point to the output of the command }}}
       
   110 
       
   111 The command saves first and then third to sixth and eighth lines of code into
       
   112 the specified file.
       
   113 
       
   114 The first argument to %save is the path of file to save the commands and the
       
   115 arguments there after are the commands to be saved in the given order.
       
   116 
       
   117 {{{ goto the file and open it and show it }}}
       
   118 
       
   119 {{{ Pause here and try out the following exercises }}}
       
   120 
       
   121 %% 2 %% change the label on y-axis to "y" and save the lines of code
       
   122         accordingly
       
   123 
       
   124 {{{ continue from paused state }}}
       
   125 
       
   126 we use the command =ylabel= on interpreter as
       
   127 ::
       
   128 
       
   129     ylabel("y")
       
   130 
       
   131 and then do
       
   132 ::
       
   133 
       
   134     %save /home/fossee/example_plot.py 1 3-6 10
       
   135 
       
   136 Now that we have the required lines of code in a file, let us learn how to run
       
   137 the file as a python script.
       
   138 
       
   139 We use the IPython magic command =%run= to do this. Type
       
   140 ::
       
   141 
       
   142    %run -i /home/fossee/plot_script.py
       
   143 
       
   144 The script runs but we do not see the plot. This happens because we are running
       
   145 a script and we are not in interactive mode anymore.
       
   146 
       
   147 Hence on your terminal type
       
   148 ::
       
   149 
       
   150     show()
       
   151 
       
   152 to show the plot.
       
   153 
       
   154 {{{ Pause here and try out the following exercises }}}
       
   155 
       
   156 %% 3 %% Use %hist and %save and create a script that has show in it and run it
       
   157         to produce and show the plot.
       
   158 
       
   159 {{{ continue from paused state }}}
       
   160 
       
   161 We first look at the history using
       
   162 ::
       
   163 
       
   164     %hist 20
       
   165 
       
   166 Then save the script using
       
   167 ::
       
   168 
       
   169     %save /home/fossee/show_included.py 1 3-6 8 10 13
       
   170     %run -i /home/fossee/show_included.py
       
   171 
       
   172 We get the desired plot.
       
   173 
       
   174 The reason for including a -i after run is to tell the interpreter that if any
       
   175 name is not found in script, search for it in the interpreter. Hence all these
       
   176 sin, plot, pi and show which are not available in script, are taken from the
       
   177 interpreter and used to run the script.
       
   178 
       
   179 {{{ Pause here and try out the following exercises }}}
       
   180 
       
   181 %% 4 %% Run the script without using the -i option. Do you find any difference?
       
   182 
       
   183 {{{ continue from paused state }}}
       
   184 
       
   185 We see that it raises nameerror saying the name linspace is not found.
       
   186 
       
   187 {{{ Show summary slide }}}
       
   188 
       
   189 This brings us to the end of the tutorial.
       
   190 we have looked at 
       
   191 
       
   192  * Retreiving history using =%hist= command
       
   193  * Vieweing only a part of history by passing an argument to %hist
       
   194  * saving the required lines of code in required order using %save
       
   195  * using %run -i command to run the saved script
       
   196 
       
   197 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   198 
       
   199 #[Nishanth]: Will add this line after all of us fix on one.
       
   200 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   201 
       
   202 Hope you have enjoyed and found it useful.
       
   203 Thankyou
       
   204  
       
   205 Questions
       
   206 =========
       
   207 
       
   208  1. How do you retrieve the recent 5 commands
       
   209 
       
   210     a. ``%hist``
       
   211     #. ``%hist -5``
       
   212     #. ``%hist 5``
       
   213     #. ``%hist 5-10``
       
   214 
       
   215     Answer: ``%hist 5``
       
   216     
       
   217  2. If there were 20 commands typed and ``%hist`` is used. How many commands 
       
   218     will be displayed.
       
   219 
       
   220     a. 10
       
   221     #. 20
       
   222     #. 21
       
   223     #. 19
       
   224 
       
   225     Answer: 21
       
   226 
       
   227  3. is ``%hist`` considered as a command
       
   228 
       
   229     a. True
       
   230     #. False
       
   231 
       
   232     Answer: True
       
   233 
       
   234  4. how do you retreive the commands from 20 to 50 (inclusive of 20 and 50)
       
   235 
       
   236     a. ``%hist 20 50``
       
   237     #. ``%hist 19 50``
       
   238     #. ``%hist 19 51``
       
   239     #. ``%hist 21 50``
       
   240 
       
   241     Answer: ``%hist 20 50``
       
   242 
       
   243  5. What does the ``%hist 2 5 7`` command do
       
   244 
       
   245     a. lists the second, fifth and seventh commands
       
   246     #. lists the commands from 2 to 5 and the seventh command
       
   247     #. raises an error
       
   248     #. lists the commands 2 to 7
       
   249 
       
   250     Answer: raises an error
       
   251 
       
   252  6. How many commands are displayed when lot of coomands were typed and 
       
   253     ``%hist`` is used.
       
   254 
       
   255     a. 20
       
   256     #. 10
       
   257     #. 50
       
   258     #. 40
       
   259 
       
   260     Answer: 40
       
   261 
       
   262  7. How do you save the lines 2 3 4 5 7 9 10 11
       
   263 
       
   264     a. ``%save filepath 2-5 7 9-11``
       
   265     #. ``%save filepath 2-11``
       
   266     #. ``%save filepath``
       
   267     #. ``%save 2-5 7 9 10 11``
       
   268 
       
   269     Answer: ``%save filepath 2-5 7 9-11``
       
   270 
       
   271  8. You are working in /home/user. Where is the file saved when you do
       
   272     ``%save hello.py 1-3`` 
       
   273 
       
   274     a. /home/user/hello.py
       
   275     #. /hello.py
       
   276     #. /home/hello.py
       
   277     #. /home/user/ipython/hello.py
       
   278 
       
   279     Answer: /home/user/hello.py
       
   280 
       
   281  9. Which lines are saved by the command ``%save filepath 2-5 7 1`` and in
       
   282     which order
       
   283 
       
   284     a. 2 3 4 5 7 1
       
   285     #. 1 2 3 4 5 6 7
       
   286     #. 2 5 7 1
       
   287     #. 1 2 5 7
       
   288 
       
   289  10. What happens when ``%save filepath line_numbers`` is used and a file
       
   290      already exists in that path.
       
   291 
       
   292     a. It is overwritten
       
   293     #. The commands are added to the file
       
   294     #. It raises an error
       
   295     #. A prompt to confirm overwriting is displayed 
       
   296 
       
   297     Answer: A prompt to confirm overwriting is displayed 
       
   298 
       
   299  11. Read through the documentation of ``%hist`` and find its alternative name
       
   300 
       
   301     Answer: ``%history``
       
   302 
       
   303  12. Are ``%run /home/user/saved.py`` and ``%run /home/user/saved`` the same
       
   304 
       
   305    a. Yes
       
   306    #. No
       
   307 
       
   308    Answer: Yes
       
   309 
       
   310  13. The file hello.py contains only one command ``x = x + 1``. What happens
       
   311      when you do ``%run hello.py``
       
   312 
       
   313     Answer: Raises a nameerror
       
   314 
       
   315   14. The file hello.py contains only one command ``x = x + 1``. If value of x
       
   316       is 5 and what does ``%run -i hello.py`` do.
       
   317 
       
   318     a. raises an error
       
   319     #. increments value of x by 1
       
   320     #. Does nothing
       
   321     
       
   322     Answer: increments the value of x by 1