using python modules/slides.org
author Anoop Jacob Thomas<anoop@fossee.in>
Tue, 12 Oct 2010 17:15:11 +0530
changeset 309 9d8fd5ea64b2
permissions -rw-r--r--
added slides for using python modules.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     1
#+LaTeX_CLASS: beamer
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     2
#+LaTeX_CLASS_OPTIONS: [presentation]
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     3
#+BEAMER_FRAME_LEVEL: 1
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     4
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     5
#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     6
#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     7
#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     8
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
     9
#+LaTeX_CLASS: beamer
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    10
#+LaTeX_CLASS_OPTIONS: [presentation]
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    11
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    12
#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    13
#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    14
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    15
#+LaTeX_HEADER: \usepackage{listings}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    16
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    17
#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries,
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    18
#+LaTeX_HEADER:  commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    19
#+LaTeX_HEADER:  showstringspaces=false, keywordstyle=\color{blue}\bfseries}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    20
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    21
#+TITLE: Using python modules
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    22
#+AUTHOR: FOSSEE
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    23
#+EMAIL:     
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    24
#+DATE:    
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    25
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    26
#+DESCRIPTION: 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    27
#+KEYWORDS: 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    28
#+LANGUAGE:  en
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    29
#+OPTIONS:   H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    30
#+OPTIONS:   TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    31
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    32
* Outline
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    33
  - Running python scripts from command line
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    34
  - Importing python modules
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    35
  - Importing scipy \& pylab modules
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    36
  - About python standard library.
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    37
* Running Python script from command line
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    38
  - Create a script, open text editor and type the following
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    39
    : print "hello world!"
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    40
    : print 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    41
  - Save the script as ~hello.py~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    42
* Running Python script from command line (cont'd)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    43
  - Run the script
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    44
    : $ python hello.py
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    45
  /Syntax :/ *python filename*
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    46
* Four plot problem
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    47
  #+begin_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    48
    \begin{center}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    49
      \includegraphics[scale=0.4]{four_plot}    
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    50
    \end{center}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    51
  #+end_latex   
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    52
* Fix ~linspace()~ problem
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    53
  : from scipy import *
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    54
* Fix ~plot()~ problem
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    55
  : from pylab import *
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    56
* Better way of fixing
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    57
  : from scipy import linspace
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    58
  instead of
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    59
  : from scipy import *
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    60
    ~*~ means import all functions from name-space ~scipy~.
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    61
* Instead of ~*~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    62
  :  from scipy import linspace, pi, sin
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    63
  :  from pylab import plot, legend, annotate
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    64
  :  from pylab import xlim, ylim, title, show
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    65
  Is better than, ~from scipy import *~ \& ~from pylab import *~.
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    66
* Another Fix
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    67
  #+begin_src python
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    68
    import scipy
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    69
    import pylab
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    70
    x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    71
    pylab.plot(x, x, 'b')
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    72
    pylab.plot(x, -x, 'b')
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    73
    pylab.plot(x, scipy.sin(x), 'g', linewidth=2)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    74
    pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    75
    pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)'])
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    76
    pylab.annotate('origin', xy = (0, 0))
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    77
    pylab.xlim(-5*scipy.pi, 5*scipy.pi)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    78
    pylab.ylim(-5*scipy.pi, 5*scipy.pi)
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    79
  #+end_src
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    80
* Exercise 1
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    81
  Write a python script to plot a sine wave from 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    82
  #+begin_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    83
    $-2\Pi$
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    84
  #+end_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    85
  to 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    86
  #+begin_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    87
    $2\Pi$
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    88
  #+end_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    89
  .
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    90
* What is a module?
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    91
  Module is simply a file containing Python definitions and
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    92
  statements. Definitions from a module can be imported into other
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    93
  modules or into the main module.
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    94
* Python standard library
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    95
  Python has a very rich standard library of modules.
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    96
  - Few libraries
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    97
    - Math: ~math~, ~random~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    98
    - Internet access: ~urllib2~, ~smtplib~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
    99
    - System, Command line arguments: ~sys~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   100
    - Operating system interface: ~os~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   101
    - regular expressions: ~re~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   102
    - compression: ~gzip~, ~zipfile~, ~tarfile~
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   103
  - More information
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   104
    - [[http://docs.python.org/library]]
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   105
* Summary
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   106
  - Running scripts from command line
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   107
  - Learned about modules
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   108
    - importing modules
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   109
  - Python standard library
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   110
* Thank you!
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   111
#+begin_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   112
  \begin{block}{}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   113
  \begin{center}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   114
  This spoken tutorial has been produced by the
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   115
  \textcolor{blue}{FOSSEE} team, which is funded by the 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   116
  \end{center}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   117
  \begin{center}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   118
    \textcolor{blue}{National Mission on Education through \\
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   119
      Information \& Communication Technology \\ 
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   120
      MHRD, Govt. of India}.
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   121
  \end{center}  
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   122
  \end{block}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   123
#+end_latex
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   124
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
diff changeset
   125