Download Strings - SciNeGHE 2010

Document related concepts
no text concepts found
Transcript
High Level Scripting
Part I
Gino Tosti
University & INFN Perugia
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Scripting Languages
¾ What is a script?
ƒ It is a small program able to automate a
repetitive and boring job;
ƒ It is a list of commands that can be executed
without user interaction
¾ Scripting languages are often used as "glue"
languages. That is they are used to glue together
languages
tools and programs which may not, themselves, be
written in the scripting language.
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Scripting Languages
¾ Dedicated to simple tasks:
ƒ awk
ƒ Sed
¾ Dedicated to drive specific
p
applications:
pp
ƒ Visual Basic (MS Office)
ƒ Javascript (Web)
ƒ Emacs Lisp (Emacs)
¾ General purpouse :
ƒ Python
ƒ Perl
ƒ Ruby
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
What is Python
¾ It is a simple programming language
¾
ƒ "interpreted"
p
(rather than compiled
p
into machine code)
ƒ High level
ƒ Simple to learn and to use
ƒ Powerful and productive
ƒ Similar
l to pseudo-code
Moreover
ƒ open source (www.python.org)
ƒ multiplatform
ƒ Easy to be integrated with C/C++ and Java
¾ http://wiki.python.org/moin/LanguageComparisons
http://wiki python org/moin/LanguageComparisons
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Basic libraries
ƒ Numpy (http://numpy.scipy.org/
(http://numpy scipy org/ )
ƒ Pyfits
(http://www.stsci.edu/resources/software_hardware/py
fits )
ƒ Matplotlib (http://matplotlib.sourceforge.net/ )
ƒ Scipy (http://www.scipy.org/;
h
http://www.scipy.org/Download)
//
i
/D
l d)
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Overview
¾Running Python and Output
¾Data Types
¾Input and File I/O
¾Control Flow
¾Functions
¾Classes
More here: http://docs.python.org/tutorial/
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Hello World
•Open a terminal window and type “python”
• Or open a Python IDE like IDLE
•At
At the prompt type
type:
¾>>> print 'hello
world!'
¾'hello world!'
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Python Overview
¾ Programs are composed of modules
¾ Modules contain statements
¾ Statements contain expressions
¾ Expressions create and process objects
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
The Python Interpreter
•Python is an interpreted
language
•The interpreter provides an
interactive environment to
play
p
y with the language
g g
•Results of expressions are
printed
d on the
h screen
06/09/2010
¾>>> 3 + 7
¾10
¾>>> 3 < 15
¾True
¾>>> 'print'
¾'print'
¾>>> print 'print '
¾Print
¾>>>
SciNeGhe 2010 - Data Analysis
Tutorial
Output: The print Statement
•Elements separated by
commas print with a
space between
b t
th
them
•A comma at the end of
the statement (print
‘hello’,) will not print a
newline
li character
h
t
06/09/2010
¾>>> print 'hello'
¾hello
¾>>> p
print 'hello', ’Ciao’
¾hello Ciao
SciNeGhe 2010 - Data Analysis
Tutorial
Variables
¾ Are not declared, just assigned
¾ The variable is created the first time you assign
it a value
¾ Are references to objects
¾ Type information is with the object, not the
reference
¾ Everything in Python is an object
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Naming Rules
¾Names are case sensitive and cannot start
with a number.
number They can contain letters
letters,
numbers, and underscores.
bob
Bob
_bob
_2_bob_
bob_2
BoB
¾There are some reserved words:
and, assert, break, class, continue,
d f d
def,
del,
l elif,
lif else,
l
except,
t exec,
finally, for, from, global, if,
import, in, is, lambda, not, or,
pass, print,
i t raise,
i
return,
t
t
try,
while
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Numbers: Integers
¾Integer – the
equivalent of a C long
¾Long Integer – an
unbounded integer
value.
l
Newer
N
versions
i
of Python will
automatically convert
to a long integer if you
overflow
rf w a normal
n rma one
n
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
¾>>> 132224
¾132224
¾>>> 132323 ** 2
¾17509376329L
¾>>>
Numbers: Floating Point
¾int(x) converts x to
an integer
¾float(x) converts x to
a floating point
¾The interpreter
shows
a lot of digits,
including the variance
i fl
in
floating
ti point
i t
¾To avoid this use
“print”
print
06/09/2010
¾>>> 1.23232
¾1.2323200000000001
¾>>> print 1.23232
¾1.23232
¾>>> 1.3E7
¾13000000.0
¾>>> int(2.0)
int(2 0)
¾2
¾>>> float(2)
¾2.0
SciNeGhe 2010 - Data Analysis
Tutorial
Numbers: Complex
¾ Built into Python
¾ Same operations are
supported as integer and
float
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
¾>>> x = 3 + 2j
¾>>> y = -1j
1j
¾>>> x + y
¾(3+1j)
¾>>> x * y
¾
¾(2-3j)
Math Module
¾ Many math functions in the math module
$ python
>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos',
'cosh'
cosh , 'degrees'
degrees , 'e'
e , 'exp'
exp , 'fabs'
fabs , 'factorial'
factorial , 'floor'
floor , 'fmod'
fmod ,
'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10',
'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan',
'tanh', 'trunc’]]
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
String Literals
¾ Strings are immutable
¾ There is no char type like
in C++ or Java
¾>>> x = 'hello'
hello
¾>>> x = x + ' there'
¾>>> x
¾'hello there'
¾ + is overloaded to do
concatenation
i
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Strings
¾Can use single or double quotes, and three
double quotes for a multi-line string
¾>>> 'this is a string'
¾'this
¾
this is a string
string'
¾>>> "this is a string"
¾'this is a string’
¾>>> """this is a
¾... long string"""
¾'this is a \nlong string’
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Substrings and Methods
¾>>> s = '012345'
¾>>> s[3]
¾'3'
¾>>> s[1:4]
¾'123'
¾>>> s[2:]
¾'2345'
¾>>> s[:4]
¾'0123'
¾>>> s[-2]
¾'4'
06/09/2010
•len(String)
len(String) – returns the number
of characters in the String
•str(Object) – returns a String
representation of the Object
¾>>> len(x)
¾
l ( )
¾6
¾>>> str(10.3)
¾'10
¾
10.3
3'
SciNeGhe 2010 - Data Analysis
Tutorial
String Formatting
¾Similar to C’s printf
¾<formatted
f
tt d string>
t i
% <elements
l
t tto iinsert>
t
¾Can usually just use %s for everything, it
will convert the object to its String
representation.
¾>>> "One, %d, three" % 2
¾'One, 2, three'
¾>>> "%d, two, %s" % (1,3)
¾'11, ttwo, 3'
¾
3
¾>>> "%s two %s" % (1, 'three')
¾'1 two three'
¾
¾>>>
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Data Types
¾ Integers: 2323, 3234L
¾ Floating
Fl ti P
Point:
i t 32
32.3,
3 3.1E2
3 1E2
¾ Complex: 3 + 2j, 1j
¾ Lists: l = [ 11,2,3]
2 3]
¾ Tuples: t = (1,2,3)
¾ Dictionaries: d = {{‘hello’ : ‘there’,, 2 : 15}}
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Lists
¾Ordered collection
of
f data
d t
¾Data can be
different types
¾Lists are mutable
¾Same subset
operations as
Strings
06/09/2010
¾>>> x = [1,'hello', (3 + 2j)]
¾>>> x
¾[1, 'hello', (3+2j)]
¾>>> x[2]
¾(3+2j)
¾>>> x[0:2]
[0 2]
¾[1, 'hello']
SciNeGhe 2010 - Data Analysis
Tutorial
Lists: Modifying Content
¾ x[i] = a reassigns the
ith element to the
value a
¾ Since x and y point to
the same list object,
both are changed
¾ Append also modifies
the list
06/09/2010
¾>>> x = [1,2,3]
¾>>> y = x
¾>>> x[1] = 15
¾>>> x
¾[1, 15, 3]
¾>>> y
¾[1,
[ , 15,, 3]]
¾>>> x.append(12)
¾>>> y
¾[1, 15, 3, 12]
SciNeGhe 2010 - Data Analysis
Tutorial
Lists: not all are in-place changes
¾ Append modifies
the list and returns
None (the python
version of null)
¾ List addition
returns a new list
06/09/2010
¾>>> x = [1,2,3]
¾>>> y = x
¾>>> z = x.append(12)
¾>>> z == None
¾True
T
¾>>> y
¾[1, 2, 3, 12]
¾>>> x = x + [9
[9,10]
10]
¾>>> x
¾[1, 2, 3, 12, 9, 10]
¾>>> y
¾[1, 2, 3, 12]
¾>>>
SciNeGhe 2010 - Data Analysis
Tutorial
Tuples
¾Tuples are immutable
versions of lists
06/09/2010
¾>>> x = (1,2,3)
¾>>> x[1:]
¾(2, 3)
¾>>> y = (2)
¾>>> y
¾(2)
¾>>>
SciNeGhe 2010 - Data Analysis
Tutorial
Dictionaries
¾ A set of key-value pairs
¾ Dictionaries
Di ti
i are mutable
t bl
¾>>> d = {1 : 'hello', 'two' : 42, ’b' : [1,2,3]}
¾>>> d
¾{1: 'h
hello
ll ', 'ttwo':: 42,
42 ’b':
b : [1,
[1 2
2, 3]}
¾>>> d[’b']
¾[1, 2, 3]
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Dictionaries: Add/Modify
¾Entries can be changed by assigning to that
entry
¾Assigning to a key that does not exist adds
an entry
¾>>> d
¾{1: 'hello', 'two': 42, ’b': [1, 2, 3]}
¾>>> d['two'] = 99
¾>>> d
¾{1: 'hello', 'two': 99, ’b': [1, 2, 3]}
¾>>> d[7] = 'new
new entry
entry'
¾>>> d
¾{1: 'hello', 7: 'new entry', 'two': 99, ’b': [1, 2, 3]}
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Dictionaries: Deleting Elements
¾ The del method deletes an element from a
dictionary
¾>>> d
¾{1: 'hello', 2: 'there', 10: 'world'}
¾>>> del(d[2])
¾>>> d
¾{1: 'hello'
hello , 10: 'world'}
world }
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Copying Dictionaries and Lists
¾ The built-in list
function will copy
a list
¾ The dictionary
has a method
called copy
06/09/2010
¾>>> l1 = [1]
¾>>> l2 = list(l1)
¾>>> l1[0] = 22
¾>>> l1
¾[22]
¾>>> l2
¾[1]
SciNeGhe 2010 - Data Analysis
Tutorial
¾>>> d = {1 : 10}
¾>>> d2 = d.copy()
¾>>> d[1] = 22
¾>>> d
¾{1: 22}
¾>>> d2
¾{1 10}
¾{1:
Input
¾ The raw_input(string) method returns a line of
user input as a string
¾ The
Th parameter
t is
i used
d as a promptt
¾ The string can be converted by using the
conversion methods int(string), float(string), etc.
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Input: Example
¾print "What's your name?"
¾name = raw_input(">")
p
¾print "What year were you born?"
¾birthyear = int(raw_input(">"))
¾print "Hi %s! You are %d years old!" % (name, 2010 - birthyear)
¾python input.py
¾What's your name?
¾Albert
¾What year were you born?
¾>1990
¾Hi Michael! You are 20 years old!
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Files: Input
i
input
t = open(‘data’,
(‘d t ’ ‘r’)
‘ ’)
O
Open
th
the file
fil for
f input
i
t
S = input.read()
i
d()
Readd whole
h l file
fil into
i
one String
S = input.read(N)
Reads N bytes
(N >= 1)
Returns a list of line
strings
L = input.readlines()
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Files: Output
output = open(‘data’, ‘w’)
Open the file for
writing
output.write(S)
Writes the string S to
file
output.writelines(L)
Writes each of the
strings in list L to file
output.close()
Manual close
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Boolean Expressions
¾ Compound boolean
expressions short circuit
¾ and and or return one of
the elements in the
expression
¾ Note
N te that
th t when
hen None
N ne is
returned the interpreter
does not print anything
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
¾>>> True and False
¾False
¾>>> False or True
¾True
¾>>> 7 and 14
¾14
¾>>> None and 2
¾>>> None or 2
¾2
If Statements
¾x = 22
¾if x < 15 :
¾ print 'first clause'
¾elif
l f x < 25 :
¾ print 'second clause'
¾else :
¾ print 'the else'
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
No Braces
¾ Python uses indentation instead of braces to
determine the scope of expressions
¾ All lines must be indented the same amount to be
partt of
f th
the scope ((or iindented
d t d more if partt of
f an
inner scope)
¾ This forces the programmer to use proper
indentation since the indenting is part of the
program!
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
While Loops
¾x = 1
¾while x < 10 :
¾ print x
¾ x=x+1
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Loop Control Statements
break
Jumps out of the closest
enclosing loop
continue
Jumps to the top of the closest
enclosing loop
pass
Does nothing,
D
thi empty
t statement
t t
t
placeholder
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
The Loop Else Clause
¾ The optional else clause runs only if the loop exits
normally (not by break)
¾x = 1
¾while
¾
hil x < 3 :
¾ print x
¾ x=x+1
¾else
¾else:
¾ print 'hello'
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
For Loops
¾Similar to perl/basic for loops, iterating
through a list of values
¾for
f x in [1,7,13,2] :
¾ print x
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Function Basics
¾def max(x,y) :
¾ if x < y :
¾
return x
¾ else :
¾
return y
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Function Not-So-Basics
¾ Functions are first class objects
o Can be assigned to a variable
o Can be passed as a parameter
o Can be returned from a function
• Functions are treated like any other variable in
python, the def statement simply assigns a
function to a variable
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Functions as Variables
¾ Functions
are objects
¾ The
Th same
reference
rules hold
for them as
for other
objects
06/09/2010
¾>>> x = 10
¾>>> x
¾10
¾>>> def x () :
¾...
... print
pr nt 'hello'
h o
¾>>> x
¾<function x at 0x619f0>
¾>>> x()
¾hello
¾>>> x = ’b'
¾>>> x
¾’b'
¾
b
SciNeGhe 2010 - Data Analysis
Tutorial
Functions: As Parameters
¾def foo(f, a) :
¾ return f(a)
¾>>> from funcasparam import *
¾>>> foo(bar, 3)
¾9
¾def bar(x) :
¾ retun x * x
•The function foo takes two parameters and
applies the first as a function with the second
as its parameter
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Functions: In Functions
¾ Since they are like any other object, you can have
functions inside functions
¾def foo (x,y) :
¾ def bar (z) :
¾
return z * 2
¾
06/09/2010
return bar(x) + y
SciNeGhe 2010 - Data Analysis
Tutorial
Functions Returning Functions
¾def foo (x) :
¾ def bar(y) :
¾
return x + y
¾
return bar
b
¾# main
¾f = foo(3)
¾print f
¾print f(2)
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Parameters: Defaults
¾Can assign default
values to
parameters
¾Theyy are
overridden if a
parameter is given
f them
for
h
¾The type of the
d f lt d
default
doesn’t
’t
limit the type of a
parameter
06/09/2010
¾>>> def foo(x = 3) :
¾... print x
¾
¾...
¾>>> foo()
¾3
¾>>> foo(
foo(10)
0)
¾10
¾>>> foo('hello')
¾hello
SciNeGhe 2010 - Data Analysis
Tutorial
Parameters: Named
¾ Can specify the
name of the
parameter to set
¾ Any positional
arguments
g
must
come before
named ones in a
call
06/09/2010
¾>>> def foo (a,b,c) :
¾... print a, b, c
¾
¾...
¾>>> foo(c = 10, a = 2, b = 14)
¾2 14 10
¾>>> foo(3,
( , c = 2,, b = 19))
¾3 19 2
SciNeGhe 2010 - Data Analysis
Tutorial
Anonymous Functions
¾ The lambda returns
a function
¾ The
Th body
b d can only
l be
b
a simple expression,
not complex
p
statements
06/09/2010
¾>>> f = lambda x,y : x + y
¾>>> f(2,3)
¾5
¾>>> l = ['one', lambda x : x * x, 3]
¾>>> l[1](4)
¾16
SciNeGhe 2010 - Data Analysis
Tutorial
Classes
¾ A collection of data and methods that act on that
data
¾ But
B t in
i python
th f
functions
ti
are basically
b i ll jjustt d
data!
t !
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Class Syntax
¾Every method in a
class takes a selfpointer as the first
parameter (self as
convention) like this
in java or C++
¾__init__ is a builtin
function that you
override as the
constructor
06/09/2010
¾class myclass :
¾
¾ def __init__(self, val) :
¾
self.x = val
¾
¾
def print1(self) :
print self.x
¾>>> from classbasic import *
¾>>> x = myclass(3)
y
( )
¾>>> x.print1()
¾3
SciNeGhe 2010 - Data Analysis
Tutorial
Class Method Calls
¾ Self is automatically added as the first parameter
when a method is called on an instance of a class
¾ Internally self must be explicitly passed
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Classes as Objects
¾ Classes exist as objects and contain all of there
own variables
¾ Name resolution starts looking in the instance of
the class for a variable, then walks up
p the
inheritance tree
¾ Variables don’t exist in the instance until they are
assigned there
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
Classes
¾>>> class
l ss m
myclass
l ss :
¾... x = 10
¾...
¾>>> a = myclass()
¾
¾>>> b = myclass()
¾>>> a.x, b.x, myclass.x
¾(10, 10, 10)
¾>>> a.x = 15
¾>>> a.x, b.x, myclass.x
¾(15, 10, 10)
06/09/2010
¾>>> m
myclass.x
l ss x = 20
¾>>> a.x, b.x, myclass.x
¾(15, 20, 20)
¾>>> a.x = myclass.x
¾
¾>>> a.x, b.x, myclass.x
¾(20, 20, 20)
¾>>> myclass.x = 99
¾>>> a.x, b.x, myclass.x
¾(20, 99, 99)
SciNeGhe 2010 - Data Analysis
Tutorial
Modules: Imports
import mymodule
Brings all elements of
mymodule in,
in but must
refer to as
mymodule.<elem>
from mymodule import x
Imports x from
mymodule right into
this namespace
Imports all elements of
mymodule into this
namespace
from mymodule import *
Standard modules
http://docs python org/modindex html
http://docs.python.org/modindex.html
Error Capture
¾ Check for type assignment errors, items not in a
list etc
list,
etc.
¾ Try & Except
try:
a block of code that might have an error
except:
code
d to execute if an error occurs iin ""try””
PyROOT
¾ export
LD_LIBRARY_PATH=$ROOTSYS/lib:$PYTHOND
LD
LIBRARY PATH=$ROOTSYS/lib:$PYTHOND
IR/lib:$LD_LIBRARY_PATH
¾ export
PYTHONPATH=$ROOTSYS/lib:$PYTHONPATH
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial
PyROOT
from ROOT import gROOT, TCanvas, TF1
gROOT.Reset()
p with Formula', 200, 10, 700,
c1 = TCanvas(( 'c1', 'Example
500 )
fun1 = TF1( 'fun1', 'abs(sin(x)/x)', 0, 10 )
c1 SetGridx()
c1.SetGridx()
c1.SetGridy()
fun1.Draw()
c1.Update()
raw_input()
06/09/2010
SciNeGhe 2010 - Data Analysis
Tutorial