Developer Data Modeling Mistakes: From Postgres to NoSQL
Python introduction
1. An introduction to Python
The language
Marcelo Araujo
Jul 2012, Taipei
marcelo@qnap.com
Monday, July 16, 12
2. { Goals of this talk?
! A brief introduction of Python language.
! Get you interested in learning Python.
! Shows that it is a powerful language.
And of course.....
Monday, July 16, 12
4. Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> What we gonna see?
! What is Python?
! Who uses Python?
! Interactive prompt, memory, basic syntax, data types, strings,
flow control statements, functions, classes, exceptions, importing,
file I/O, list, tuple, dictionary, cast, help, dir.
! CPython, Pypy, Jython and IronPython.
! Python Frameworks.
! Your first Python program.
Monday, July 16, 12
5. { What is Python?
Python is a remarkably powerful dynamic programming
languate that is used in a wide variety of application
domains.
! Very clear, readable syntax.
! Strong introspection capabilities.
! Intuitive object orientation.
! Natural expression of procedural code.
! Full modularity, supporting hierarchical packages.
! Exception-based error handling.
! Very high level dynamic data types.
! Extensive standard libraries and third party modules.
! Extensions and modules easily written in C, C++ or (Java or .NET).
! Embeddable within applications as a scripting interface.
Monday, July 16, 12
6. { Who created Python?
! Guido van Rossum.
! Python was released early of 1990s.
! Based on: ABC, C, Bourne Shell, Modula-3,
Perl, Haskell and Lisp.
! Currently he works for Google.
! ... Half of his working time is to improve
Python.
! Python is not related with the snake, but yes with a British show
called Monty Python’s Flying Circus.
Monday, July 16, 12
8. { The interactive python
! Python has an interactive prompt that able you write code.
! You can obtain help.
! Have access to the docs.
! Test your code and ideas any time.
Monday, July 16, 12
9. { The interactive python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The ZEN of Python by Tim Peters.
PEP - 020.
It is the principles of Python
*PEP - Python Enhancement Proposal.
http://www.python.org/dev/peps/
Monday, July 16, 12
10. { The interactive python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name = 'Marcelo'
>>> fname = “Araujo”
>>> age = 31
>>> print 'My name is ', name, fname, ', I'm %i years old' % (age)
My name is Marcelo Araujo , I'm 31 years old
>>> print “I’d like be %i years old” % (age - 9)
I’d like be 22 years old
>>> type(name)
<type ‘str’>
>>> type(fname)
<type ‘str’>
>>> type(age)
<type ‘int’>
Monday, July 16, 12
11. { Reserved words.
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
Like in any other computer
language.
Monday, July 16, 12
12. { The interactive python - Variable
! The first assignment to a variable creates it.
! Variable types don’t need to be declared.
! Python figures out the variable types on its own.
! Python has a garbage collector.
>>> name = ‘Marcelo’
>>> type(name), id(name)
(<type 'str'>, 4483079264)
>>> memoryview(name)
<memory at 0x10b3d3f28>
>>> del name
>>> print name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
Monday, July 16, 12
13. { The interactive python - Memory
>>> a = ‘marcelo’
>>> b = a a = ‘marcelo’
>>> id(a), id(b)
(4482266376, 4482266376) name obj
>>> memoryview(a), memoryview(b)
(<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)
>>> a = 'araujo'
>>> id(a), id(b)
(4483897624, 4482266376)
>>> memoryview(a), memoryview(b)
(<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)
}
buffer method()
0x10b3d3f28
0x10b928050 Mem
0x10b3d3e90
Monday, July 16, 12
14. { The interactive python - Memory
>>> a = ‘marcelo’
>>> b = a a = ‘marcelo’
>>> id(a), id(b)
(4482266376, 4482266376) name obj
>>> memoryview(a), memoryview(b)
(<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)
>>> a = 'araujo'
>>> id(a), id(b)
(4483897624, 4482266376)
>>> memoryview(a), memoryview(b)
(<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)
}
OID buffer method()
a 0x10b3d3f28
OID 0x10b928050 Mem
0x10b3d3e90
b
Monday, July 16, 12
15. { Basic syntax
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3
4 class Name: Class Name
5
6 def __init__(self, name=None): Method and attribute
7 self.name = name Attribute
8
9
10 class FName(Name):
}
11
12 def __init__(self, fname=None):
Indentation Style
13 self.fname = fname
14
15 def output(self):
16 print self.name, self.fname
17
}
18 if __name__ == '__main__':
19 inst_name = FName(fname='Araujo') main()
20 inst_name.name = 'Marcelo'
21 inst_name.output()
Monday, July 16, 12
16. { Basic syntax - Indentation
! Indentation is important.
! No brackets. {}
! No dot and comma. ;
>>> a = 1
>>> b = 2
>>> if a > b:
... print 'a is bigger than b'
... else:
... print 'b is bigger than a'
...
b is bigger than a
Code readable and more elegant.
Monday, July 16, 12
17. { Basic data types
Numbers: int, long, float, complex.
Strings: str and unicode.
List and Tuples: list and tuple.
Dictionary: dict
Files: file
Boolean: bool(True, False)
Sets: set, frozenset
Null: None
Note: int usually 32bits, long all your memory,
float usually 32bits.
Monday, July 16, 12
18. { Basic data types
! It is possible to cast types.
>>> a = 1 >>> a = float(a)
>>> type(a) >>> type(a)
<type 'int'> <type 'float'>
Operators
! Arithmetic. ! Logical.
! +, -, *, /, %, ** and // ! and, or, not
! Comparison. ! Membership.
! ==, !=, <>, >, <, >=, <= ! in, not in
! Assignment. ! Identity.
! =, +=, -=, *=, /=, %=, ! is, is not
**= , //=
! Bitwise.
! &, |, ^, ~, <<, >>
Monday, July 16, 12
19. { Basic data types - Strings
! String is powerful in Python. (Immutable)
>>> a = 'Marcelo'
>>> print a[0]
'M'
>>> a[0] = 'm'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
! String has a set of METHODS.
>>> print a.upper(), a.lower(), a.partition('c')
MARCELO marcelo ('Mar', 'c', 'elo')
>>> a.replace('c', 'C')
'MarCelo'
! How change the string?
>>> a = 'Marcelo' >>> a = 'm' + a[1:]
>>> id(a) >>> print a
4483079264 marcelo
>>> id(a)
4483500336
Monday, July 16, 12
20. { Basic data types - Special Types.
! Lists. (Mutable)
>>> a = [1, 2 3, 4, 5]
>>> print a
[1, 2, 3, 4, 5]
>>> a[0] = 0
>>> print a
[0, 2, 3, 4, 5]
! Tuples. (Immutable)
>>> a = (1, 2, 3, 4, 5, 6)
>>> a[0] = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
! Dictionary. (Mutable only the ITEM)
>>> a = {'Name' : 'Marcelo', ‘Age’ : 31}
>>> print a['Name']
Marcelo
Monday, July 16, 12
21. { Flow control statement
if guess == number: while True:
# do something # do something
elif guess < number: # break when done
# do something else break
else: else:
# do something else # do something in the end
for i in range(1, 10): for i in range(1, 10):
# do something # do something
print i if i == 5:
else: pass
# do something else in else:
# the end print i
Monday, July 16, 12
22. { More about List.
! List sort using “for”. Methods
1 a = [1, 3, 5, 4, 2]
2 b = [0 for i in range(5)] a.append()
3 size_a = len(a)
4 a.pop()
5 for i in range(size_a): a.remove(<value>)
6 print i, a[i]
7 b[a[i] - 1] = a[i] a.index(<value>)
8 a.count(<value>)
9 print a
10 print b
! Sort the list in a python way. dir() and help()
>>> a = [1, 3, 5, 2, 4]
>>> a.sort() >>> dir(list)
>>> print a >>> help(list)
[1, 2, 3, 4, 5]
Monday, July 16, 12
23. { More about List.
! Different than string, list, tuple and dictionary point to the
same OBID.
>>> import copy
>>> a = [1, 2, 3]
>>> a = [1, 2, 3]
>>> b = a
>>> b = copy.copy(a)
>>> a.append(4)
>>> a.append(4)
>>> print a, b
>>> print a, b
[1, 2, 3, 4] [1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3]
>>> id(a), id(b)
>>> id(a), id(b)
(4529798896, 4529798896)
(4528943688, 4529135344)
! List full fill. ! List comprehension.
>>> a = [] >>> a = [i for i in range(1,6)]
>>> for i in range(1,6): >>> print a
... a.append(i) [1, 2, 3, 4, 5]
...
>>> print a
[1, 2, 3, 4, 5]
Monday, July 16, 12
24. { More about Dictionary.
! Dictionary is useful, we have a key for a specific item.
>>> register = {'Name' : 'Marcelo', 'Age' : 31, 'Dep' : 'R&D'}
>>> print register
{'Dep': 'R&D', 'Age': 31, 'Name': 'Marcelo'}
! Access the dictionary.
>>> for key, item in register.iteritems():
... print key + ':' + 't' + str(item)
...
Dep:
R&D
Age:
31
Name:
Marcelo
Note: Key is an unique value and immutable.
Monday, July 16, 12
25. { Function/Method
! Function is a block of organized, reusable code, that perform
a single related action.
! Better modularity for your application.
! Python gives you many built-in functions like print().
Ex: 1 Ex: 2
>>> def say_hello(name=None): >>> def say_bye(name):
... if name == None: ... print 'Good bye %s' % (name)
... print 'Hello nobody!' ...
... else: >>> say_bye()
... print 'Hello %s' % (name) Traceback (most recent call last):
... File "<stdin>", line 1, in <module>
>>> name = 'Marcelo' TypeError: say_bye() takes exactly 1
>>> say_hello(name) argument (0 given)
Hello Marcelo
>>> say_hello()
Hello nobody!
Monday, July 16, 12
26. { Function/Method
! How pass an undefined number of args to a function?
>>> def hello_all(say=None, *names):
... if say == None:
... say = 'Hello'
... elif say == 'Morning':
... say = 'Good morning'
... else:
... say = 'Aloha'
... for name in names:
... print say + ' ' + name
...
>>> name = 'Marcelo'
>>> name1 = 'Bob'
>>> name2 = 'Kevin'
>>> hello_all(‘Aloha’, name, name1, name2)
Aloha Marcelo
Aloha Bob
Aloha Kevin
Monday, July 16, 12
27. { Function/Method
! We could use return.
>>> def big_number(n1=0, n2=0):
... bigone = None
... if n1 > n2:
... bigone = n1
... elif n1 < n2:
... bigone = n2
... else:
... bigone = 'same'
... return bigone
...
>>> answer = big_number(10, 20)
>>> print answer
20
Note: Alway a function return something, if not
defined, it will return None.
Monday, July 16, 12
28. { Classes/Objects - OOP Terminology
! Class: A user-defined prototype for an object that defines a
set of attributes.
! Class variable: A variable that is shared by all instances of a
class.
! Function overloading: The assignment of more than one
behavior to a particular function/method.
! Instance variable: A variable that is defined inside a method
and belongs only to the current instance of a class.
! Inheritance: The transfer of the characteristics of a class to
other classes.
! Instance: An individual object of a certain class.
! Instantiantion: The creation of an instance of a class.
! Method: A special kind of function that is defined in a class.
! Object: An unique instance of a data structure.
Monday, July 16, 12
29. { Class example
1 class Employee: Class Name
2 """ Common base class for all employees"""
3 empCount = 0 Global Value of Class
4
5 def __init__(self, name, position): Class constructor
6 self.name = name
7 self.position = position
8 Employee.empCount += 1
9
10 def displayEmployee(self): NEXT SLIDE (><)
11 print "Name: ", self.name, "ttPosition: ", self.position
12
13 if __name__ == '__main__':
14 emp1 = Employee("Marcelo", "R&D") Instantiation the class
15 emp2 = Employee("Bob", "Q&A")
16
17 emp1.displayEmployee()
18 emp2.displayEmployee()
19
20 print 'Total Employee: %d' % (Employee.empCount) Instance the empCount Obj
Monday, July 16, 12
30. { self, self, self, self.....?
! “self” is a polemic decision on the project.
! It is part of PEP-8.
! Do you remember the Python ZEN?
! BETTER EXPLICIT THAN IMPLICIT
Ex: 1 Ex: 2
↶
>>> class Person: >>> class Person:
... def set_name(person, name): ... def set_name(self, name):
... person.name = name ... self.name = name
... ...
>>> woman = Person() >>> woman = Person()
>>> woman.set_name('Janny') >>> woman.set_name('Janny')
>>> print woman.name >>> print woman.name
Janny Janny
Monday, July 16, 12
31. { Class inheritance
1 class Employee_salary: New Class
2 def salary(self, value=0):
3 self.value = value
4 print "[Salary: %s]" % (self.value)
5
6 class Employee(Employee_salary): Inheritance
< ..............code snipped...................>
18 if __name__ == '__main__':
19 emp1 = Employee("Marcelo", "R&D") Instantiation the class
20 emp2 = Employee("Bob", "Q&A")
21
22 emp1.displayEmployee()
23 emp1.salary(100) Call method salary()
24 emp2.displayEmployee()
25 emp2.salary(200)
26
27 print 'Total Employee: %d' % (Employee.empCount)
Monday, July 16, 12
32. { How about exception?
! It help us to handle situations that disrupts the normal flow
of the program.
try: >>> try:
... 10 / 0
# do something ... except ZeroDivisionError:
except: ... print "Ooops, invalid."
... else:
# do exception ... print "We're good, no exception!"
else: ... finally:
# do something else ... print "We're done with that."
...
finally: Ooops, invalid.
# just do We're done with that.
Monday, July 16, 12
33. { More about exception.
! We can check multiples exceptions.
>>> try:
... a / 0
... except ZeroDivisionError:
... print “Not possible make the division!”
... except TypeError:
... print “Unsupported type.”
... else:
... print 'We pass in all exceptions!'
... finally:
... print 'Do something'
...
Unsupported type.
Do something
Built-in Exceptions list:
http://docs.python.org/library/exceptions.html
Monday, July 16, 12
34. { More exception with raise.
! Use raise to catch problems.
>>> def verify(value):
... if value == 10:
... print "Wow you have: %s" % (value)
...
>>> try:
... verify()
... except:
... raise
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: verify() takes exactly 1 argument (0 given)
Monday, July 16, 12
35. { More exception with raise.
! We can change the error message.
>>> def verify(value):
... if not value.isdigit():
... raise ValueError, "My useful message!"
... else:
... return value
>>> try:
... verify('A')
... except ValueError:
... raise
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 3, in verify
ValueError: My useful message!
Monday, July 16, 12
36. { Import/Modules
! Every Python code is a module by default.
! Modules help you to organize your software.
! Python comes with batteries. Powerful standard library.
STDL:
socket, select, SocketServer, BaseHTTPServer, asyncore,
asynchat, xmlrpclib, SimpleXMLRPCServer, urllib, httplib,
ftplib, smtpd, smtplib, poplib, impalib, json, getopt,
optparse, argparse, fileinput, cmd, readline, subprocess,
threading, multiprocessing, Queue, anydbm, pickle,
shelve, sqlite3 ...... there are more
Monday, July 16, 12
37. { Import/Modules
What I did?
! I moved all classes to another file called cemployee.py.
1 class Employee_salary:
2 def salary(self, value=0):
3 self.value = value
4 print "[Salary: %s]" % (self.value)
5
6 class Employee(Employee_salary):
7 """ Common base class for all employees"""
8 empCount = 0
9
10 def __init__(self, name, position):
11 self.name = name
12 self.position = position
13 Employee.empCount += 1
14
15 def displayEmployee(self):
16 print "Name: ", self.name, "ttPosition: ", self.position
Monday, July 16, 12
38. { Import/Modules
What I did?
! Now my employee.py looks like.
1 import cemployee
2
3 if __name__ == '__main__':
4 emp1 = cemployee.Employee("Marcelo", "R&D")
5 emp2 = cemployee.Employee("Bob", "Q&A")
6
7 emp1.displayEmployee()
8 emp1.salary(100)
9 emp2.displayEmployee()
10 emp2.salary(200)
11
12 print 'Total Employee: %d' % (cemployee.Employee.empCount)
Monday, July 16, 12
39. { Import/Modules
! I can give a friendly name for the module.
>>> import cemployee as myclass
>>> emp1 = myclass.Employee("Marcelo", "R&D")
>>> emp1.displayEmployee()
Name: Marcelo
Position: R&D
! I also can import only some classes from a module.
>>> from cemployee import Employee_salary as salary
>>> a = salary()
>>> a.salary(100)
[Salary: 100]
! More one example.
>>> import os
>>> os.system('uname -m')
x86_64
0
>>> from os import system as sys
>>> sys('uname -m')
x86_64
0
Monday, July 16, 12
40. { CPython, Jython, IronPython and PyPy?
Cpython
! Used to binding C/C++ code.
>>> from ctypes import *
>>> libc = CDLL("libc.so")
>>> size = c_uint(0)
>>> buf = create_string_buffer(size.value)
>>> libc.sysctlbyname("kern.hostname", buf, byref(size), None, 0)
0
>>> print buf.value
controllerA.qnap.com
Jython
! Used to binding Java.
! You can pack a jar to run over the JVM.
! Swing and any other JAVA library can be used.
Monday, July 16, 12
41. { CPython, Jython, IronPython and PyPy?
IronPython
! Used to binding .NET.
! Microsoft have interesting on IronPython.
PyPy
! It is a compliant alternative implementation of the Python
2.7.2
! They claim be faster than CPython.
More info at: http://speed.pypy.org/
Monday, July 16, 12
42. { Python Frameworks?
Django
! High-level Python Web framework.
! Rapid development and clean, pragmatic design.
! Automatic admin interface.
! Cache system.
! Template system.
! Internationalization by default.
! MTV (Model, Template,View).
Monday, July 16, 12
43. { Python Frameworks?
Web2Py
! Inspired by Ruby on Rails.
! Focuses on rapid development.
! MVC.
! Support packed applications.
TurboGears
! Scale it out.
! SQLAlchemy - Object Relational Mapper
Twisted Matrix
! It is an event-driven networking engine.
! It supports many network protocols like: SMTP, POP3, IMAP,
SSHv2, DNS and so on.
Monday, July 16, 12
44. { Python Frameworks?
Example web-server using Cherrypy:
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import cherrypy
4
5 class HelloWorld(object):
6
7 def index(self):
8 return "Hello World!"
9 index.exposed = True
10
11 if __name__ == '__main__':
12 cherrypy.quickstart(HelloWorld())
Monday, July 16, 12
45. { Your first python code.
The output:
sh-3.2# python first.py
Our first python code!
The result is: 5
Monday, July 16, 12
46. { Your first python code.
Now is time to play:
a = 10 print ‘Impossible do that!’
finally: result = a / b
b=2 result = None
except:
% (result)
print ‘Our first python code!’
print ‘The result is: %s’ try:
Monday, July 16, 12
47. { Your first python code.
How it supposed to be?
1 a = 10
2b=2
3 result = None
4
5 try:
6 result = a / b
7 except:
8 print 'Impossible do that!'
9 finally:
10 print 'Our first python code!'
11
12 print 'The result is: %s' % (result)
Monday, July 16, 12
48. { Thank you.
Thanks, have a good weekend guys!
marcelo@qnap.com
Monday, July 16, 12