Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
20120314 changa-python-workshop
1. Nom du fichier – à compléter Management Presentation
Python
Opensource Object Oriented scripting language
Python
Opensource Object Oriented scripting language
1
2. Nom du fichier – à compléter Management Presentation
Agenda of workshop.
Introduction to Python
Features of Python
Python in Enterprise
Who use Python (They speak about Python)
Rapid application Development using Python with OpenERP and
Django.
Installation of Python on Windows and Linux
Setup Development Enviroments using Eclipse
Step to Python
String
Number
Statements & Control Flow
3. Nom du fichier – à compléter Management Presentation
Agenda.
Data Structure
• List, Tuple, Dict
Sorting
Object Oriented
• Class, Object, Inheritence, Polymorphism
Modules
Errors and Exceptions Handling
Input / Output
Python Quiz
4. Nom du fichier – à compléter Management Presentation
Introduction to Python
Python is developed by Guido van Rossum, named the language
after the BBC show "Monty Python's Flying Circus".
5. Nom du fichier – à compléter Management Presentation
Introduction to Python.
Python is an easy to learn, powerful programming language. It has
efficient high-level data structures and a simple but effective
approach to object-oriented
programming.
Python's elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and
rapid application development in many areas on most platforms.
6. Nom du fichier – à compléter Management Presentation
Features of Python.
Simple
Flexible.
Easy to Learn
Free and Open Source
High-level Language
Platform Independent.
Dynamic Type.
Extensive Libraries.
Object Oriented.
Interpreted.
Scalable.
7. Nom du fichier – à compléter Management Presentation
Python in Enterprise
Frameworks, Web Development and MNCs.
8. Nom du fichier – à compléter Management Presentation
Python in Enterprise.
Server, Social Network, shopping sites.
Games & Graphics.
9. Nom du fichier – à compléter Management Presentation
What User Says ?
YouTube.com
• "Python is fast enough for our site and allows us to produce maintainable
features in record times, with a minimum of developers," said Cuong Do,
Software Architect, YouTube.com.
Google
• "Python has been an important part of Google since the beginning, and remains
so as the system grows and evolves. Today dozens of Google engineers use
Python, and we're looking for more people with skills in this language." said Peter
Norvig, director of search quality at Google, Inc.
Industrial Light & Magic
• "Python plays a key role in our production pipeline. Without it a project the size
of Star Wars: Episode II would have been very difficult to pull off. From crowd
rendering to batch processing to compositing, Python binds all things together,"
said Tommy Burnette, Senior Technical Director, Industrial Light & Magic.
10. Nom du fichier – à compléter Management Presentation
What User Says ?
University of Maryland
• "I have the students learn Python in our undergraduate and graduate
Semantic Web courses. Why? Because basically there's nothing else with the
flexibility and as many web libraries," said Prof. James A. Hendler.
11. Nom du fichier – à compléter Management Presentation
Rapid application Development using Python
• OpenERP Module
• Web Application using DJango
12. Nom du fichier – à compléter Management Presentation
Starting with Python.
Instalation on Linux
• If you are using a Linux distribution such as Ubuntu, Fedora, OpenSUSE it is
most likely you already have Python installed on your system.
• To test if you have Python already installed on your Linux box, open a shell
program (like console or gnome-terminal) and enter the command python -V
as shown below.
Instalation on Windows
• download the latest python version from the website and install it on your
system.
• Set the environment variable path.
Setup Development Enviroments using Eclipse.
• http://www.easyeclipse.org/site/distributions/python.html
13. Nom du fichier – à compléter Management Presentation
First step to Python.
Using The Interpreter Prompt
• Start the interpreter on the command line by entering python at the shell
prompt.
• For Windows users, you can run the interpreter in the command line if you
have set the PATH variable appropriately.
• Ex.
print('Hello World')
Using Source file.
Indentation
14. Nom du fichier – à compléter Management Presentation
Python Literals & Numbers.
Literal Constants
• An example of a literal constant is a number like 5, 1.23, 9.25e-3 or a string
like 'This is a string' or "It's a string!". It is called a literal because it is literal -
you use its value literally.
Numbers
• Numbers in Python are of three types - integers, floating point and complex
numbers.
• Ex. 3.23 and 52.3E-4
15. Nom du fichier – à compléter Management Presentation
Python Strings.
Strings
• Single Quotes
• Double Quotes
• Triple Quotes
• Escape Sequences
Raw Strings
Strings Are Immutable
String Literal Concatenation
16. Nom du fichier – à compléter Management Presentation
Control Flow statements
The if statement.
• The if statement is used to check a condition and if the condition is true, we
run a block of statements (called the if-block), else we process another block
of statements (called the else-block). The else clause is optional.
• Example
17. Nom du fichier – à compléter Management Presentation
Control Flows Cont...
The while Statement
• The while statement allows you to repeatedly execute a block of statements as long as a condition is
true. A while statement is an example of what is called a looping statement. A while statement can
have an optional else clause.
• Example
number = 23
running = True
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
running = False # this causes the while loop to stop
elif guess < number:
print('No, it is a little higher than that.')
else:
print('No, it is a little lower than that.')
else:
print('The while loop is over.')
# Do anything else you want to do here
18. Nom du fichier – à compléter Management Presentation
Python Control Flow...
The for loop
• The for..in statement is another looping statement which iterates over a
sequence of objects i.e. go through each item in a sequence, sequence is just
an ordered collection of items.
• The for loop also have optional else statement.
• Example
19. Nom du fichier – à compléter Management Presentation
Python Control Flow...
The break Statement
• The break statement is used to break out of a loop statement i.e. stop the
execution of a looping statement, even if the loop condition has not become
False or the sequence of items has been completely iterated over.
• Example
The continue Statement
• The continue statement is used to tell Python to skip the rest of the
statements in the current loop block and to continue to the next iteration of
the loop.
• Example
20. Nom du fichier – à compléter Management Presentation
Python Functions
Functions are reusable pieces of programs. They allow you to give a
name to a block of statements and you can run that block using
that name anywhere in your program and any number of times.
Functions are defined using the def keyword.
This is followed by an identifier name for the function followed by a
pair of parentheses which may enclose some names of variables
and the line ends with a colon.
Example
21. Nom du fichier – à compléter Management Presentation
Python Function Cont...
Local Variables
Using The global Statement
Default Argument Values
Keyword Arguments
The return Statement
DocStrings
• Python has a nifty feature called documentation strings, usually referred to
by its shorter name docstrings. DocStrings are an important tool that you
should make use of since it helps to document the program better and
makes it easier to understand.
22. Nom du fichier – à compléter Management Presentation
Modules
You have seen how you can reuse code in your program by defining
functions once. What if you wanted to reuse a number of functions
in other programs that you write?
The answer is modules.
There are various methods of writing modules, but the simplest
way is to create a file with a .py extension that contains functions
and variables.
A module can be imported by another program to make use of its
functionality.
23. Nom du fichier – à compléter Management Presentation
Making Your Own Modules
Creating your own modules is easy, you've been doing it all along!
This is because every Python program is also a module. You just
have to make sure it has a .py extension.
Example of Module
24. Nom du fichier – à compléter Management Presentation
Packages
Packages are just folders of modules with a special __init__.py file
that indicates to Python that this folder is special because it
contains Python modules.
25. Nom du fichier – à compléter Management Presentation
Python Data Structures
Data structures are basically just that - they are structures which
can hold some data together. In other words, they are used to
store a collection of related data.
List
• A list is a data structure that holds an ordered collection of items i.e. you can
store a sequence of items in a list.
• The list of items should be enclosed in square brackets so that Python
understands that you are specifying a list. Once you have created a list, you
can add, remove or search for items in the list. Since we can add and remove
items, we say that a list is a mutable data type i.e. this type can be altered.
• Example
o [1,2,3, 'a']
26. Nom du fichier – à compléter Management Presentation
Data Structure cont...
Tuple
• Tuples are used to hold together multiple objects. Think of them as similar to
lists, but without the extensive functionality that the list class gives you. One
major feature of tuples is that they are immutable like strings i.e. you cannot
modify tuples.
• Tuples are defined by specifying items separated by commas within an
optional pair of parentheses.
• Tuples are usually used in cases where a statement or a user-defined
function can safely assume that the collection of values i.e. the tuple of
values used will not change.
• Example
o (1,2,3)
27. Nom du fichier – à compléter Management Presentation
Python Data Structure cont...
Dictionary
• A dictionary is like an address-book where you can find the address or
contact details of a person by knowing only his/her name i.e. we associate
keys (name) with values (details).
• Note that the key must be unique just like you cannot find out the correct
information if you have two persons with the exact same name.
• Example
o {'a': 1, 'b':2}
28. Nom du fichier – à compléter Management Presentation
Python Data Structure
Set
• Sets are unordered collections of simple objects. These are used when the
existence of an object in a collection is more important than the order or
how many times it occurs.
• Example
o bri = set(['brazil', 'russia', 'india'])
o
29. Nom du fichier – à compléter Management Presentation
Sorting
There are lots of way to sort the data in python.
Each data structure have its own sorting mechanism.
List Sort
numlist=[1, 2.1, 2, 1.1, 1.3, 1.8, 1.9, 2.4, 2.8, 2.5, 2.8, 2.4, 2.1, 2.3, 1.1, 1.3,
1.3, 1.2, 1.2, 3, 3.1, 2.5, 3.5]
numlist.sort()
print (numlist)
• Custom Sorting With key=
strs = ['ccc', 'aaaa', 'd', 'bb']
print sorted(strs, key=len)
print sorted(strs, key=str.lower)
30. Nom du fichier – à compléter Management Presentation
Sorting cont...
sort() method
mylist = ["b", "C", "A"]
mylist.sort()
Dictonary sorting
import operator
x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))
31. Nom du fichier – à compléter Management Presentation
Object Oriented Programming with Python
Organizing your program which is to combine data and
functionality and wrap it inside something called an object. This is
called the object oriented programming paradigm.
Classes and objects are the two main aspects of object oriented
programming.
Class contains data and methods.
The self
• Class methods have only one specific difference from ordinary functions -
they must have an extra first name that has to be added to the beginning of
the parameter list, but you do not give a value for this parameter when you
call the method, Python will provide it. This particular variable refers to the
object itself, and by convention, it is given the name self.
32. Nom du fichier – à compléter Management Presentation
Classes
The simplest class possible is shown in the following example.
33. Nom du fichier – à compléter Management Presentation
Object Methods
We have already discussed that classes/objects can have methods
just like functions except that we have an extra self variable.
Example
34. Nom du fichier – à compléter Management Presentation
The __init__method
There are many method names which have special significance in
Python classes. We will see the significance of the __init__ method
now.
The __init__ method is run as soon as an object of a class is
instantiated. The method is useful to do any initialization you want
to do with your object.
Example
35. Nom du fichier – à compléter Management Presentation
Class And Object Variables
There are two types of fields - class variables and object variables
which are classified depending on whether the class or the object
owns the variables respectively.
Class variables are shared - they can be accessed by all instances of
that class. There is only one copy of the class variable and when
any one object makes a change to a class variable, that change will
be seen by all the other instances.
Object variables are owned by each individual object/instance of
the class. In this case, each object has its own copy of the field i.e.
they are not shared and are not related in anyway to the field by
the same name in a different instance.
Example
36. Nom du fichier – à compléter Management Presentation
Inheritance
One of the major benefits of object oriented programming is reuse
of code and one of the ways this is achieved is through the
inheritance mechanism.
Inheritance can be best imagined as implementing a type and
subtype relationship between classes.
Simple inheritance.
Multiple inheritance.
Multi level Inheritance.
37. Nom du fichier – à compléter Management Presentation
Object oriented concepts...
Method overloading.
Method overwriting.
Polymorphism
38. Nom du fichier – à compléter Management Presentation
Errors and Exceptions Handling
Exception
• Exceptions occur when certain exceptional situations occur in your program.
For example, what if you are going to read a file and the file does not exist?
Or what if you accidentally deleted it when the program was running? Such
situations are handled using exceptions.
• We will try to read input from the user. Press ctrl-d and see what happens.
39. Nom du fichier – à compléter Management Presentation
Exception cont...
Errors
• Consider a simple print function call. What if we misspelt print as Print?
40. Nom du fichier – à compléter Management Presentation
Handling Exceptions
We can handle exceptions using the try..except statement.
We put all the statements that might raise exceptions/errors inside
the try block and then put handlers for the appropriate
errors/exceptions in the except clause/block.
The except clause can handle a single specified error or exception,
or a parenthesized list of errors/exceptions. If no names of errors
or exceptions are supplied, it will handle all errors and exceptions.
If any error or wxception is not handeled then default python
handler will called.
You can also have an else clause associated with a try..except
block. The else clause is executed if no exception occurs.
41. Nom du fichier – à compléter Management Presentation
Raising Exceptions
You can raise exceptions using the raise statement by providing the
name of the error/exception and the exception object that is to be
thrown.
The error or exception that you can arise should be class which
directly or indirectly must be a derived class of the Exception class.
Example
42. Nom du fichier – à compléter Management Presentation
Try ..Finally
Suppose you are reading a file in your program. How do you ensure
that the file object is closed properly whether or not an exception
was raised? This can be done using the finally block.
Example
43. Nom du fichier – à compléter Management Presentation
Input / Output
Up to now we have seen how to take input from user and display it
using input, raw_input and print statements.
Another common type of input/output is dealing with files. The
ability to create, read and write files is essential to many programs.
Files
• You can open and use files for reading or writing by creating an object of the
file class and using its read, readline or write methods appropriately to read
from or write to the file.
• The ability to read or write to the file depends on the mode you have
specified for the file opening.
• Then finally, when you are finished with the file, you call the close method to
tell Python that we are done using the file.
44. Nom du fichier – à compléter Management Presentation
Input/Output.
Methods of File object.
• read()
• read_line()
• readlines()
• write(stringToWrite)
• seek(seekingByte)
• close()
45. Nom du fichier – à compléter Management Presentation
Input/Output
Pickle
• when you want to save more complex data types like lists, dictionaries, or
class instances, things get a lot more complicated.
• Python provides a standard module called pickle.
• This is an amazing module that can take almost any Python object and
convert it to a string representation; this process is called pickling.
• Reconstructing the object from the string representation is called unpickling.
46. Nom du fichier – à compléter Management Presentation
Input/Output
If you have an object x, and a file object f that’s been opened for
writing, the simplest way to pickle the object takes only one line of
code:
• pickle.dump(x, f)
To unpickle the object again, if f is a file object which has been
opened for reading:
• x = pickle.load(f)
47. Nom du fichier – à compléter Management Presentation
Questions ?
48. Nom du fichier – à compléter Management Presentation
Contact Us
Write email at education@openerp.co.in
call us on 91 79 400 500 48