Python:Python:Overview and Advanced TopicsOverview and Advanced Topics
Kirby UrnerKirby Urner
4D Solutions4D Solutions
March 28, 2005 2GIS in Action 2005
Python WorldPython World
CoreCore
Standard Standard LibraryLibrary
33rdrd Party Party Add-OnsAdd-Ons
ApplicationsApplications
March 28, 2005 3GIS in Action 2005
Topics to CoverTopics to Cover
OverviewOverview Core PythonCore Python ““Batteries Included” (adding Batteries Included” (adding
power)power) Python as Glue Language Python as Glue Language
(talking to other apps)(talking to other apps)
March 28, 2005 4GIS in Action 2005
Part 1: OverviewPart 1: Overview
What is Python?What is Python? CommunityCommunity On RampsOn Ramps Development EnvironmentsDevelopment Environments Design PhilosophyDesign Philosophy EvolutionEvolution
March 28, 2005 5GIS in Action 2005
By the way, the language is named after the BBC show “Monty Python's Flying Circus” and has nothing to do with nasty reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!
Guido van RossumPython Tutorial
March 28, 2005 6GIS in Action 2005
What is Python?What is Python?
Interpreted: Interpreted: compiles to byte codescompiles to byte codes Interactive: Interactive: shell modeshell mode OO: OO: “everything is an object”“everything is an object” Modular: Modular: Standard Library & 3Standard Library & 3rdrd party party Extensible: Extensible: code new modules in C/C++code new modules in C/C++ Portable: Portable: Windows, Unix, Linux, MacWindows, Unix, Linux, Mac High Level: High Level: built-in dynamic data typesbuilt-in dynamic data types Free: Free: including for commercial useincluding for commercial use
March 28, 2005 7GIS in Action 2005
Python - why settle for snake oil when you can have the whole snake? — From Usenet posting by Mark Jackson, June 1998
March 28, 2005 8GIS in Action 2005
InterpretedInterpreted Not much compile time checkingNot much compile time checking No type declarationsNo type declarations Lots of freedom at runtimeLots of freedom at runtime ““Compiling” to byte codes: .pyc, .pyoCompiling” to byte codes: .pyc, .pyo Python VM required (a .dll in Windows)Python VM required (a .dll in Windows) No compile/link loop – reload insteadNo compile/link loop – reload instead Rapid development, relatively Rapid development, relatively
slow execution (but frequently “plenty slow execution (but frequently “plenty fast”)fast”)
March 28, 2005 9GIS in Action 2005
InteractiveInteractive
Many GUI shell options (plus Many GUI shell options (plus non-GUI shell)non-GUI shell)
Shell mode as work benchShell mode as work bench Jython: interactive access Jython: interactive access
to Java classesto Java classes Learning / testing time is Learning / testing time is
drastically reduceddrastically reduced
March 28, 2005 10GIS in Action 2005
Object OrientedObject Oriented
Ideas from C++, Modula-3, SmallTalk Ideas from C++, Modula-3, SmallTalk etc.etc.
Variable names = aliases or references Variable names = aliases or references to objects; assignment results in to objects; assignment results in multiple aliases to the multiple aliases to the same same objectobject
>>>>>> a = [1,2,3]; b = a; b[1]=0a = [1,2,3]; b = a; b[1]=0
>>>>>> aa
[1, 0, 3][1, 0, 3]
March 28, 2005 11GIS in Action 2005
ModularModular
Users save code in modulesUsers save code in modules Standard Library consists of modulesStandard Library consists of modules Modules may be grouped in packagesModules may be grouped in packages Scripts are modules (.py files)Scripts are modules (.py files) Problem of namespace collisions is Problem of namespace collisions is
handled with module prefixes handled with module prefixes (like in Java)(like in Java)
March 28, 2005 12GIS in Action 2005
ExtensibleExtensible
Wrap existing C/C++ libraries to Wrap existing C/C++ libraries to make them importable as Python make them importable as Python modules (e.g. wxPython, win32all)modules (e.g. wxPython, win32all)
Prototype in Python then rewrite Prototype in Python then rewrite speed-critical parts in C/C++ andspeed-critical parts in C/C++ andimportimport
Create Python bindings for yourCreate Python bindings for yourapplications i.e. export an APIapplications i.e. export an API
March 28, 2005 13GIS in Action 2005
PortablePortable Language accommodates many Language accommodates many
platform difference (e.g. path nameplatform difference (e.g. path nameseparatorsseparators
.pyc byte codes run on any CPython.pyc byte codes run on any CPythonVMVM
The language may be stripped downThe language may be stripped downto run on cell phones, PDAsto run on cell phones, PDAs
Python VMs may be written in otherPython VMs may be written in otherlanguages besides Clanguages besides C
March 28, 2005 14GIS in Action 2005
High LevelHigh Level
Powerful built-in types:Powerful built-in types:list, tuple, dictionary (also set)list, tuple, dictionary (also set)string, unicodestring, unicode
Native long integers (i.e. multi-Native long integers (i.e. multi-precision), complex numbers, fixedprecision), complex numbers, fixedprecision decimals (new in 2.4)precision decimals (new in 2.4)
Built-in and user-defined ExceptionsBuilt-in and user-defined Exceptionswith try… except… raise… finallywith try… except… raise… finally
Canned parsers, protocol servers etc.Canned parsers, protocol servers etc.
March 28, 2005 15GIS in Action 2005
Free and Open SourceFree and Open Source
licenselicense is a built-in function. is a built-in function. Call it, i.e. enter license() for some Call it, i.e. enter license() for some fascinating readingfascinating reading
Some older versions are more Some older versions are more restrictedrestricted
Although current Pythons are GPLAlthough current Pythons are GPLcompatible, you are not required to compatible, you are not required to release the source of derivative works,release the source of derivative works,only to provide a summary of changesonly to provide a summary of changes
March 28, 2005 16GIS in Action 2005
CommunityCommunity
Guido van Rossum is BDFLGuido van Rossum is BDFL Language evolves via PEPsLanguage evolves via PEPs Mail Lists: technical, tutorial, sigsMail Lists: technical, tutorial, sigs Web sites: add ons, tutorialsWeb sites: add ons, tutorials Documentation: python.org, HTMLDocumentation: python.org, HTML comp.lang.python (very active)comp.lang.python (very active) Subcultures: Numeric, Zope/Plone,Subcultures: Numeric, Zope/Plone,
wxPython etc.wxPython etc.
March 28, 2005 17GIS in Action 2005
On RampsOn Ramps
O’Reilly:O’Reilly: Learning PythonLearning Python Programming in PythonProgramming in Python Python in a NutshellPython in a Nutshell
Apress:Apress: Dive Into PythonDive Into Python (on-line edition!) (on-line edition!)
Guido’s Tutorial in Python docsGuido’s Tutorial in Python docs Python Programming: An Intro to CSPython Programming: An Intro to CS
March 28, 2005 18GIS in Action 2005
Development EnvironmentsDevelopment Environments
Free and Open SourceFree and Open Source CommercialCommercial ExperimentalExperimental GraphicalGraphical A lot of developers use a text editorA lot of developers use a text editor
of choice (vi, emacs, eclipse)of choice (vi, emacs, eclipse) Platform specific or more genericPlatform specific or more generic
March 28, 2005 19GIS in Action 2005
Design PhilosophyDesign Philosophy>>> import this>>> import this
The Zen of Python, by Tim PetersThe Zen of Python, by Tim Peters
Beautiful is better than ugly.Beautiful is better than ugly.
Explicit is better than implicit.Explicit is better than implicit.
Simple is better than complex.Simple is better than complex.
Complex is better than complicated.Complex is better than complicated.
Flat is better than nested.Flat is better than nested.
Sparse is better than dense.Sparse is better than dense.
Readability counts.Readability counts.
Special cases aren't special enough to break the rules.Special cases aren't special enough to break the rules.
Although practicality beats purity.Although practicality beats purity.
Errors should never pass silently.Errors should never pass silently.
Unless explicitly silenced.Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.Although that way may not be obvious at first unless you're Dutch.
Now is better than never.Now is better than never.
Although never is often better than *right* now.Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!Namespaces are one honking great idea -- let's do more of those!
March 28, 2005 20GIS in Action 2005
EvolutionEvolution
Change to division operator:Change to division operator:/ vs. /// vs. //
Addition of new types:Addition of new types:set, Decimalset, Decimal
Type / Class unification:Type / Class unification:newstyle vs. classic classesnewstyle vs. classic classes
List comprehensions, generatorsList comprehensions, generators from __future__ import …from __future__ import …
March 28, 2005 21GIS in Action 2005
Part 2: Nuts and BoltsPart 2: Nuts and Bolts
Invoking the interpreterInvoking the interpreter Statements and ExpressionsStatements and Expressions ScopingScoping Control StructuresControl Structures Functions and GeneratorsFunctions and Generators ClassesClasses ModulesModules PackagesPackages
D:\Python24>python -m test2 < inputs.txtD:\Python24>python -m test2 < inputs.txt
['D:\\Python24\\test2.py']['D:\\Python24\\test2.py']
Enter text: Enter text:
"Hello world""Hello world"
D:\Python24>type test2.pyD:\Python24>type test2.py
import sysimport sys
print sys.argvprint sys.argv
a = raw_input("Enter text: ")a = raw_input("Enter text: ")
printprint
print aprint a
D:\Python24>type inputs.txtD:\Python24>type inputs.txt
"Hello world""Hello world"
March 28, 2005 23GIS in Action 2005
ScopingScoping
A namespace is a mapping from names to objects...
Examples of namespaces are:
• the set of built-in names (functions such as abs(), and built-in exception names);
• the global names in a module;
• and the local names in a function invocation.
from the from the Python TutorialPython Tutorial by GVR by GVR
March 28, 2005 24GIS in Action 2005
ScopingScoping
Locals: lexically definedLocals: lexically defined Globals: per module Globals: per module Built-ins: always available Built-ins: always available
>>> import math>>> math.pi3.1415926535897931>>> from math import pi>>> pi3.1415926535897931>>> from math import atan as arctan
>>> a = 2>>> def f(): return a + 2>>> f()4>>> def f(): a = a + 2 # try to bind local variable return a
>>> f()Traceback (most recent call last): File "<pyshell#8>", line 1, in -toplevel- f() File "<pyshell#7>", line 2, in f a = a + 2UnboundLocalError: local variable 'a' referenced before assignment
>>> def f(): global a # explicit binding to a global a = a + 2 return a
>>> a = 2>>> f()4>>> f()6>>> a6>>> globals() # built-in function{'a': 6, 'f': <function f at 0x00C96F30>, '__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None}
March 28, 2005 27GIS in Action 2005
Control StructuresControl Structures
The usual things but…The usual things but…no case or switch – other ways to no case or switch – other ways to get the same effectget the same effect
break and continuebreak and continue no “GO TO” or labelsno “GO TO” or labels for, while, if/eliffor, while, if/elif Exception handlingException handling
try: except: else: finally: raise:try: except: else: finally: raise:
def invert(table): index = {} # empty dictionaryfor key in table.keys():
value = table[key] if not index.has_key(value):
index[value] = [] # empty list index[value].append(key) return index
>>> pb = {'guido': 4127, 'kirby': 4127, 'jack': 4098} >>> pb['marla'] = 4147 # add an entry
>>> inverted_pb = invert(pb) >>> print inverted_pb {4098: ['jack'], 4127: ['guido','kirby'], 4147: ['marla']}
def invert(table):"""Alternative invert function""" index = {} # empty dictionaryfor key,value in table.items():
index[value] = index.get(value,[])index[value].append(key) return index
>>> pb.get('jason',[]); pb.get('guido',[])[]4127
>>> from tutor1 import invert>>> help(invert)Help on function invert in module tutor1:
invert(table) Alternative invert function
def getedges(faces): """Extract edges from the faces list""" edges = set() for f in faces: pairs = zip( f , f[1:] + (f[0],) ) for p in pairs: edges.add(tuple(sorted(p))) return sorted(list(edges))
>>> faces = [('A','H','C','F'), ('A','H','B','G'), ('B','E','C','H'), ('B','E','D','G'), ('D','G','A','F'), ('C','E','D','F')]
>>> getedges(faces)
[('A','F'),('A','G'),('A','H'),('B','E'),
('B','G'),('B','H'),('C','E'),('C','F'),
('C','H'),('D','E'),('D','F'),('D','G')]
March 28, 2005 31GIS in Action 2005
Functions and GeneratorsFunctions and Generators
Functions are top level, meaning Functions are top level, meaning you may pass them as argumentsyou may pass them as argumentsto other functionsto other functions
Generators save state between Generators save state between function calls. Write as a functionfunction calls. Write as a functionbut with keyword ‘yield’ instead but with keyword ‘yield’ instead of ‘return’of ‘return’
March 28, 2005 32GIS in Action 2005
ClassesClasses
Classes support multiple inheritanceClasses support multiple inheritance
New-style classes inherit from New-style classes inherit from object, are of type ‘type’object, are of type ‘type’
Classic classes are of type ClasstypeClassic classes are of type Classtype
Static and class methods supportedStatic and class methods supported
Late binding means lots of flexibilityLate binding means lots of flexibility
““Duck typing”Duck typing”
March 28, 2005 33GIS in Action 2005
ModulesModules
A module may double as a scriptA module may double as a script
Sometimes the script does self-Sometimes the script does self-testingtesting
A module is the logical unit of a A module is the logical unit of a small solution spacesmall solution space
Multiple modules organize in Multiple modules organize in packagespackages
March 28, 2005 34GIS in Action 2005
PackagesPackages
Packages = directories with Packages = directories with subdirectoriessubdirectories
__init__ defines what’s imported or__init__ defines what’s imported orimportableimportable
__all__ governs the behavior of ‘from __all__ governs the behavior of ‘from xxx import *’xxx import *’
Packages may have subpackagesPackages may have subpackages
March 28, 2005 35GIS in Action 2005
Part 3: GUI ProgrammingPart 3: GUI Programming
Cross-platform: wxPython, Tk, Cross-platform: wxPython, Tk, GTK, QtGTK, Qt
Platform specific: MFC, .NET (?)Platform specific: MFC, .NET (?)
March 28, 2005 36GIS in Action 2005
Python as Glue LanguagePython as Glue Language
win32all available for all versions win32all available for all versions from python.orgfrom python.org
ActiveState Python incorporates ActiveState Python incorporates Win32 featuresWin32 features
Cross-platform GUI solutionsCross-platform GUI solutions Future: Python .NET (IronPython) – Future: Python .NET (IronPython) –
also somewhat cross-platformalso somewhat cross-platform
March 28, 2005 37GIS in Action 2005
Windows IntegrationWindows Integration
Using win32all extensions, Python Using win32all extensions, Python may operate as a COM client (e.g. may operate as a COM client (e.g. control MSFT Office applications)control MSFT Office applications)
Python may also operate as a COM Python may also operate as a COM (or DCOM) server (e.g. callable from (or DCOM) server (e.g. callable from VBA)VBA)
Other COM objects: DAO and ADO Other COM objects: DAO and ADO for database accessfor database access
MSF: Microsoft Foundation ClassesMSF: Microsoft Foundation Classes