Python Programming
Python Programming
Python Programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Python</strong> and Regular Expression<br />
Introduction<br />
DDM 8.0 Advanced<br />
© 2008 Hewlett-Packard Development Company, L.P.<br />
The information contained herein is subject to change without notice.<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
ABOUT THIS CHAPTER<br />
Objectives<br />
After completing this chapter, you will be able to:<br />
Know what is <strong>Python</strong>/Jython<br />
Be familiar with some of the <strong>Python</strong> semantics used in<br />
UCMDB scripting<br />
Having a foundation in <strong>Python</strong> to modify a Discovery<br />
Script<br />
Understand and manipulate Regular Expression<br />
2<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> and Jython<br />
© 2008 Hewlett-Packard Development Company, L.P.<br />
The information contained herein is subject to change without notice.<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
ABOUT PYTHON AND JYTHON<br />
More about <strong>Python</strong><br />
Once upon a time:<br />
Origin and philosophy<br />
• First released by Guido van Rossum in 1991<br />
• Philosophy:<br />
– An easy and intuitive language just as powerful as major<br />
competitors<br />
– General purpose very-high-level language<br />
– Open source, so anyone can contribute to its development<br />
– Code that is as understandable as plain English<br />
– Suitability for everyday tasks, allowing for short development<br />
times<br />
4<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
ABOUT PYTHON AND JYTHON<br />
More about <strong>Python</strong><br />
<strong>Python</strong> features:<br />
Core syntax and semantics are minimalist<br />
Multi-paradigm programming language:<br />
• Traditional Procedural Style<br />
• Object oriented Style<br />
Fully dynamic type system<br />
Automatic memory management<br />
Production Implementations:<br />
• <strong>Python</strong> (aka C<strong>Python</strong> or Classic <strong>Python</strong>)<br />
• Jython (Java Virtual Machine compliant)<br />
5<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
ABOUT PYTHON AND JYTHON<br />
More about <strong>Python</strong><br />
Community and Interesting links:<br />
Community:<br />
• Open, community-based development model managed by<br />
the non-profit <strong>Python</strong> Software Foundation<br />
• Various parts of the language have formal specifications<br />
and standards, however the language as a whole is not<br />
formally specified<br />
Links:<br />
• http://www.python.org<br />
• http://www.python.org/doc<br />
• http://en.wikibooks.org/wiki/<strong>Python</strong>_<strong>Programming</strong><br />
6<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
ABOUT PYTHON<br />
Recommended Books<br />
• Good Introductory Book<br />
• Syntax<br />
• Language <strong>Programming</strong> Ref<br />
• Another good intro book<br />
• Syntax, programming<br />
• Debugging<br />
• Good Reference Book<br />
• <strong>Programming</strong> Examples<br />
• Sample Code<br />
7<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
ABOUT PYTHON AND JYTHON<br />
What is Jython?<br />
<strong>Python</strong> vs. Jython:<br />
Standard is C<strong>Python</strong> implementation.<br />
Jython (formerly J<strong>Python</strong>), is an implementation of<br />
the <strong>Python</strong> written in Java, which offers extra features:<br />
• Based on Java 1.2 or better<br />
• Import and use any Java class<br />
• Includes almost all of the modules in the standard <strong>Python</strong><br />
distribution . The DDM Jython is based on an earlier<br />
version of <strong>Python</strong> so some libraries may not work.<br />
• Can convert <strong>Python</strong> source code into Java bytecode,<br />
which can be fully utilized by a Java program<br />
Links:<br />
• http://www.jython.org<br />
8<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Installing standalone Jython<br />
• http://www.jython.org<br />
9<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Jython<br />
Language Basics<br />
© 2008 Hewlett-Packard Development Company, L.P.<br />
The information contained herein is subject to change without notice.<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Basic Syntax<br />
<strong>Python</strong> is Case Sensitive<br />
Do not mix tabs and spaces<br />
• In UCMDB, we use the tab or 4 spaces, but the latter is not recommended.<br />
Scope and namespace<br />
• Global<br />
• Local (Functions)<br />
Comments<br />
• # This symbol will comment the rest of line<br />
• """ This is a comment on many<br />
lines"""<br />
Print command<br />
11<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
<strong>Python</strong> programs are composed of logical lines of code<br />
each made up of one or more physical lines.<br />
•Physical lines may end with comments<br />
•Comments are designated by a # and go to the end of the physical line<br />
•A line with just whitespace is called blank line<br />
•The end of a physical line ends most statements<br />
•Not terminated with a delimiter<br />
•If a line has no comment and ends with a \ then it continues to the next line<br />
•An open parenthesis ( open braket [ or brace { can cause lines to span<br />
multiple physical lines as well<br />
•Block structures are expressed by indentation<br />
12<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Tokens – A component in a logical line.<br />
Token types are<br />
• Identifiers<br />
• Keywords<br />
• Operators<br />
• Delimiters<br />
• Literals<br />
13<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Identifiers – A name to identify a variable, function, class,<br />
module, or object.<br />
• Starts with a letter A to Z or a to z or underscore(_)<br />
• Can be followed by zero or more letters, underscores and<br />
digits (0-9)<br />
• Case sensitive<br />
• Punctuation characters not allowed (@$%^&*!)<br />
Examples:<br />
Abc<br />
This_is_a_class<br />
C<br />
b<br />
14<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Keywords – Identifiers that are reserved for special<br />
syntactic use. In <strong>Python</strong> version 2.1 they are:<br />
and finally pass<br />
assert for print<br />
break from raise<br />
Class global return<br />
Continue if try<br />
Def import while<br />
Del in yield<br />
Elif<br />
Else<br />
Except<br />
Exec<br />
is<br />
lambda<br />
not<br />
or<br />
15<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Operators – Non-alphanumeric characters that are<br />
used to perform operations.<br />
+ ><br />
* & >=<br />
/ | <br />
% ^ !=<br />
** ~ ==<br />
// <<br />
16<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Delimiters – The following symbols and<br />
combinations are used in expressions, lists,<br />
dictionaries, statements and strings.<br />
( ) &=<br />
[ ] |=<br />
{ } ^=<br />
, : >>=<br />
. ‘
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Strings<br />
Single quotes or double quotes<br />
>>> 'spam eggs'<br />
'spam eggs'<br />
>>> 'doesn\'t'<br />
"doesn't"<br />
>>> "doesn't"<br />
"doesn't"<br />
>>> '"Yes," he said.'<br />
'"Yes," he said.'<br />
>>> "\"Yes,\" he said."<br />
'"Yes," he said.'<br />
>>> '"Isn\'t," she said.'<br />
'"Isn\'t," she said.'<br />
18<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Strings<br />
Strip function<br />
19<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Strings<br />
Strings can be subscripted (indexed)<br />
• the first character of a string has subscript (index) 0<br />
20<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Strings<br />
Assigning to an indexed position<br />
• Assigning to an indexed position in the string results in an<br />
error<br />
>>> word[0] = 'x'<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
TypeError: object doesn't support item<br />
assignment<br />
Positions can be inferred<br />
21<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Strings<br />
Length of a String<br />
22<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Lists<br />
Compound data types<br />
>>> a = ['spam', 'eggs', 100, 1234]<br />
>>> a<br />
['spam', 'eggs', 100, 1234]<br />
23<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with List<br />
List Indexes<br />
• list indices start at 0<br />
• lists can be sliced, concatenated and so on<br />
24<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Lists<br />
Assignment<br />
• Assignment to slices is also possible, and this can even change the size of<br />
the list or clear it entirely. In the example below list a[I : j] . When you<br />
slice the ith item is included but the jth item is excluded.<br />
25<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with List<br />
Assignment (cont’d)<br />
• Examples<br />
26<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with List<br />
Length of a list<br />
• The built-in function len() also applies to lists<br />
>>> len(a)<br />
8<br />
27<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Working with Lists<br />
Nesting lists<br />
• It is possible to nest lists (lists inside of another list)<br />
>>> q = [2, 3]<br />
>>> p = [1, q, 4]<br />
>>> len(p)<br />
3<br />
>>> p[1]<br />
[2, 3]<br />
>>> p[1][0]<br />
2<br />
>>> p[1].append('xtra') # See section 5.1<br />
>>> p<br />
[1, [2, 3, 'xtra'], 4]<br />
>>> q<br />
[2, 3, 'xtra']<br />
28<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Control Flow syntax<br />
Operators<br />
Operator<br />
function<br />
< less than<br />
greater than<br />
>= greater than or equal to<br />
== equal<br />
!= not equal<br />
another way to say not equal<br />
29<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Control Flow syntax<br />
If statement:<br />
>>> x = -6<br />
>>> if x > 0:<br />
... print "Positive“<br />
... elif x == 0:<br />
... print "Zero“<br />
... else:<br />
... print "Negative“<br />
...<br />
'Negative'<br />
30<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Control Flow syntax<br />
For statements:<br />
>>> # Measure some strings:<br />
... a = ['cat', 'window', 'defenestrate']<br />
>>> for x in a:<br />
... print x, len(x)<br />
...<br />
cat 3<br />
window 6<br />
defenestrate 12<br />
31<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Basics<br />
Control Flows syntax<br />
While statements:<br />
>>> x = 5<br />
>>> while x > 0:<br />
… print x<br />
… x = x – 1<br />
…<br />
5<br />
4<br />
3<br />
2<br />
1<br />
32<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Less Basic<br />
33<br />
Range function<br />
Built-in function if you need to iterate over a sequence of numbers<br />
>>> range(10)<br />
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br />
>>> range(3, 10, 3)<br />
[3, 6, 9]<br />
>>> range(-10, -100, -30)<br />
[-10, -40, -70]<br />
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']<br />
>>> for i in range(len(a)):<br />
... print i, a[i]<br />
...<br />
0 Mary<br />
1 had<br />
2 a<br />
3 little<br />
4 lamb<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Less Basic<br />
Defining Functions<br />
Example:<br />
def sum2numbers(nb1, nb2):<br />
sum = nb1+nb2<br />
return sum<br />
print "The sum of ", 3, " and ", 4, " is ", sum2numbers(3,4)<br />
The keyword def introduces a function definition<br />
Followed by the function name, the parenthesized list of formal<br />
parameters and end by a colon<br />
Body of the function starts at the next line, and must be indented<br />
(use tab)<br />
Can return or not return a value<br />
34<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Less Basic<br />
Defining Functions<br />
Default Argument Values:<br />
def func(argV = 3):<br />
print argV<br />
argV = 4<br />
func()<br />
Result:<br />
3<br />
def sum3numbers(nb1, nb2=2, nb3=3):<br />
sum = nb1+nb2+nb3<br />
return sum<br />
print sum3numbers(3,nb3=4)<br />
35<br />
Result:<br />
9<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Less Basic<br />
Error Handling/Catching Exceptions<br />
try, except:<br />
try:<br />
print 1/0<br />
except ZeroDivisionError:<br />
print "You can't divide by zero, you silly.“<br />
Result:<br />
You can't divide by zero, you silly.<br />
All built-in <strong>Python</strong> exceptions:<br />
• http://www.python.org/doc/current/lib/module-exceptions.html<br />
36<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
<strong>Python</strong> <strong>Programming</strong><br />
Less Basic<br />
Error Handling/Catching Exceptions<br />
What is mainly used in UCMDB: sys.exc_info()<br />
• Example:<br />
stacktrace = traceback.format_exception(sys.exc_info()[0],<br />
sys.exc_info()[1], sys.exc_info()[2])<br />
• This function returns a tuple (type, value, traceback) of three values that<br />
give information about the exception that is currently being handled :<br />
– type gets the exception type of the exception being handled<br />
– value gets the exception parameter<br />
– traceback gets a traceback object which encapsulates the call stack at<br />
the point where the exception originally occurred.<br />
37<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
© 2008 Hewlett-Packard Development Company, L.P.<br />
The information contained herein is subject to change without notice.<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
Regular Expressions<br />
What is a Regular Expression?<br />
A string that is used to describe or match a set of strings, according to<br />
certain syntax rules.<br />
What can it be used for? Example:<br />
• Everyday, you are receiving a message (among others) from<br />
your application similar to this one:<br />
13/11/2007 08:01 Good morning Dr Jones. You have 3<br />
new messages.<br />
• How can we easily retrieve the number of messages for Dr<br />
Jones and the date and time, knowing that the date and time<br />
will change, that we might have other messages with a<br />
different name and that we are only retrieving the last<br />
message?<br />
39<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
Basics<br />
Basic Concepts<br />
Alternation<br />
• A vertical bar separates alternatives. For example, gray|grey can match<br />
"gray" or "grey".<br />
Grouping<br />
• Parentheses are used to define the scope and precedence of the<br />
operators (among other uses). For example, gray|grey and gr(a|e)y are<br />
equivalent patterns which both describe the set of "gray" and "grey".<br />
Quantification<br />
• ? : zero or one. For example, colou?r matches both "color" and<br />
"colour".<br />
• * : zero or more. For example, ab*c matches "ac", "abc", "abbc",<br />
"abbbc", and so on.<br />
• + : one or more. For example, ab+c matches "abc", "abbc", "abbbc",<br />
and so on, but not "ac".<br />
40<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
Basics<br />
Syntax (non-exhaustive)<br />
. : (Period) Any single character except newline.<br />
• a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c"<br />
[ ] : A single character that is contained within the brackets<br />
• For example, [abc] matches "a", "b", or "c“<br />
• [a-z] specifies a range which matches any lowercase letter from "a" to<br />
"z“<br />
• can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", and "z"<br />
[^ ] : A single character that is not contained within the brackets<br />
( ) : Defines a marked subexpression<br />
• The string matched within the parentheses can be recalled later<br />
{m,n} : Matches the preceding element at least m and not more than n<br />
times.<br />
• For example, a{3,5} matches only "aaa", "aaaa", and "aaaaa".<br />
41<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
Basics<br />
Syntax (non-exhaustive)<br />
What would be matched with these?<br />
• .at<br />
• [hc]at<br />
• [^b]at<br />
Answer:<br />
• Any three-character string ending with "at", including "hat",<br />
"cat", and "bat".<br />
• "hat" and "cat".<br />
• All strings matched by .at except "bat".<br />
42<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
Basics<br />
Syntax (non-exhaustive), useful ones:<br />
[A-Za-z0-9] : Alphanumeric characters<br />
[A-Za-z] : Alphabetic characters<br />
[A-Za-z]+ : A series of alphabetic characters<br />
[ \t] : Space and tab<br />
[0-9] or \d : Digits<br />
[!"#$%&'()*+,-./:;?@[\\\]_`{|}~] : Punctuation characters<br />
[ \t\r\n\v\f] : Whitespace characters<br />
43<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
Regular Expressions<br />
Debugging<br />
<strong>Python</strong> Gui debugger<br />
Kodos<br />
• Free<br />
• OpenSource<br />
• Works with <strong>Python</strong><br />
• http://kodos.sourceforge.net/a<br />
bout.html<br />
44<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
DEMO OF THE KODOS<br />
TOOL<br />
© 2008 Hewlett-Packard Development Company, L.P.<br />
The information contained herein is subject to change without notice.<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.
CHAPTER SUMMARY<br />
Summary<br />
<strong>Python</strong>/Jython is a user-friendly and intuitive language,<br />
however it requires a strict semantic<br />
Regular expressions are an important feature, used almost<br />
regularly in DDM scripting.<br />
46<br />
HP CONFIDENTIAL - ENABLEMENT ONLY, NOT FOR CUSTOMER USE.