Download Python - 4D Solutions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python:
Overview and Advanced Topics
Kirby Urner
4D Solutions
Python World
3rd Party
Add-Ons
Standard
Library
Core
Applications
March 28, 2005
GIS in Action 2005
2
Topics to Cover
Overview
 Core Python
 “Batteries Included” (adding
power)
 Python as Glue Language
(talking to other apps)

March 28, 2005
GIS in Action 2005
3
Part 1: Overview
What is Python?
 Community
 On Ramps
 Development Environments
 Design Philosophy
 Evolution

March 28, 2005
GIS in Action 2005
4
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 Rossum
Python Tutorial
March 28, 2005
GIS in Action 2005
5
What is Python?








Interpreted:
Interactive:
OO:
Modular:
Extensible:
Portable:
High Level:
Free:
March 28, 2005
compiles to byte codes
shell mode
“everything is an object”
Standard Library & 3rd party
code new modules in C/C++
Windows, Unix, Linux, Mac
built-in dynamic data types
including for commercial use
GIS in Action 2005
6
Python - why settle for snake oil when you can have
the whole snake?
— From Usenet posting by Mark Jackson, June 1998
March 28, 2005
GIS in Action 2005
7
Interpreted
Not much compile time checking
 No type declarations
 Lots of freedom at runtime
 “Compiling” to byte codes: .pyc, .pyo
 Python VM required (a .dll in Windows)
 No compile/link loop – reload instead
 Rapid development, relatively
slow execution (but frequently “plenty
fast”)

March 28, 2005
GIS in Action 2005
8
Interactive
Many GUI shell options (plus
non-GUI shell)
 Shell mode as work bench
 Jython: interactive access
to Java classes
 Learning / testing time is
drastically reduced

March 28, 2005
GIS in Action 2005
9
Object Oriented
Ideas from C++, Modula-3, SmallTalk
etc.
 Variable names = aliases or references
to objects; assignment results in
multiple aliases to the same object

>>> a = [1,2,3]; b = a; b[1]=0
>>> a
[1, 0, 3]
March 28, 2005
GIS in Action 2005
10
Modular
Users save code in modules
 Standard Library consists of modules
 Modules may be grouped in packages
 Scripts are modules (.py files)
 Problem of namespace collisions is
handled with module prefixes
(like in Java)

March 28, 2005
GIS in Action 2005
11
Extensible
Wrap existing C/C++ libraries to
make them importable as Python
modules (e.g. wxPython, win32all)
 Prototype in Python then rewrite
speed-critical parts in C/C++ and
import
 Create Python bindings for your
applications i.e. export an API

March 28, 2005
GIS in Action 2005
12
Portable
Language accommodates many
platform difference (e.g. path name
separators
 .pyc byte codes run on any CPython
VM
 The language may be stripped down
to run on cell phones, PDAs
 Python VMs may be written in other
languages besides C

March 28, 2005
GIS in Action 2005
13
High Level
Powerful built-in types:
list, tuple, dictionary (also set)
string, unicode
 Native long integers (i.e. multiprecision), complex numbers, fixed
precision decimals (new in 2.4)
 Built-in and user-defined Exceptions
with try… except… raise… finally
 Canned parsers, protocol servers etc.

March 28, 2005
GIS in Action 2005
14
Free and Open Source
license is a built-in function.
Call it, i.e. enter license() for some
fascinating reading
 Some older versions are more
restricted
 Although current Pythons are GPL
compatible, you are not required to
release the source of derivative works,
only to provide a summary of changes

March 28, 2005
GIS in Action 2005
15
Community
Guido van Rossum is BDFL
 Language evolves via PEPs
 Mail Lists: technical, tutorial, sigs
 Web sites: add ons, tutorials
 Documentation: python.org, HTML
 comp.lang.python (very active)
 Subcultures: Numeric, Zope/Plone,
wxPython etc.

March 28, 2005
GIS in Action 2005
16
On Ramps
O’Reilly:
Learning Python
Programming in Python
Python in a Nutshell
 Apress:
Dive Into Python (on-line edition!)
 Guido’s Tutorial in Python docs
 Python Programming: An Intro to CS

March 28, 2005
GIS in Action 2005
17
Development Environments
Free and Open Source
 Commercial
 Experimental
 Graphical
 A lot of developers use a text editor
of choice (vi, emacs, eclipse)
 Platform specific or more generic

March 28, 2005
GIS in Action 2005
18
Design Philosophy
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
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.
Now is better than never.
Although never is often better than *right* now.
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.
Namespaces are one honking great idea -- let's do more of those!
March 28, 2005
GIS in Action 2005
19
Evolution
Change to division operator:
/ vs. //
 Addition of new types:
set, Decimal
 Type / Class unification:
newstyle vs. classic classes
 List comprehensions, generators
 from __future__ import …

March 28, 2005
GIS in Action 2005
20
Part 2: Nuts and Bolts
Invoking the interpreter
 Statements and Expressions
 Scoping
 Control Structures
 Functions and Generators
 Classes
 Modules
 Packages

March 28, 2005
GIS in Action 2005
21
D:\Python24>python -m test2 < inputs.txt
['D:\\Python24\\test2.py']
Enter text:
"Hello world"
D:\Python24>type test2.py
import sys
print sys.argv
a = raw_input("Enter text: ")
print
print a
D:\Python24>type inputs.txt
"Hello world"
Scoping
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 Python Tutorial by GVR
March 28, 2005
GIS in Action 2005
23
Scoping



Locals: lexically defined
Globals: per module
Built-ins: always available
>>> import math
>>> math.pi
3.1415926535897931
>>> from math import pi
>>> pi
3.1415926535897931
>>> from math import atan as arctan
March 28, 2005
GIS in Action 2005
24
>>>
>>>
>>>
4
>>>
a = 2
def f():
f()
return a + 2
def f():
a = a + 2
return a
# try to bind local variable
>>> f()
Traceback (most recent call last):
File "<pyshell#8>", line 1, in -toplevelf()
File "<pyshell#7>", line 2, in f
a = a + 2
UnboundLocalError: 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
>>> a
6
>>> globals() # built-in function
{'a': 6, 'f': <function f at 0x00C96F30>,
'__builtins__': <module '__builtin__' (builtin)>, '__name__': '__main__', '__doc__': None}
Control Structures
The usual things but…
no case or switch – other ways to
get the same effect
 break and continue
 no “GO TO” or labels
 for, while, if/elif
 Exception handling
try: except: else: finally: raise:

March 28, 2005
GIS in Action 2005
27
>>> pb = {'guido': 4127, 'kirby': 4127,
'jack': 4098}
>>> pb['marla'] = 4147 # add an entry
def invert(table):
index = {} # empty dictionary
for key in table.keys():
value = table[key]
if not index.has_key(value):
index[value] = [] # empty list
index[value].append(key)
return index
>>> inverted_pb = invert(pb)
>>> print inverted_pb
{4098: ['jack'], 4127: ['guido','kirby'],
4147: ['marla']}
>>> pb.get('jason',[]); pb.get('guido',[])
[]
4127
def invert(table):
"""Alternative invert function"""
index = {} # empty dictionary
for key,value in table.items():
index[value] = index.get(value,[])
index[value].append(key)
return index
>>> from tutor1 import invert
>>> help(invert)
Help on function invert in module tutor1:
invert(table)
Alternative invert function
>>> 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')]
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))
Functions and Generators
Functions are top level, meaning
you may pass them as arguments
to other functions
 Generators save state between
function calls. Write as a function
but with keyword ‘yield’ instead
of ‘return’

March 28, 2005
GIS in Action 2005
31
Classes


Classes support multiple inheritance
New-style classes inherit from
object, are of type ‘type’

Classic classes are of type Classtype

Static and class methods supported

Late binding means lots of flexibility

“Duck typing”
March 28, 2005
GIS in Action 2005
32
Modules




A module may double as a script
Sometimes the script does selftesting
A module is the logical unit of a
small solution space
Multiple modules organize in
packages
March 28, 2005
GIS in Action 2005
33
Packages




Packages = directories with
subdirectories
__init__ defines what’s imported or
importable
__all__ governs the behavior of
‘from xxx import *’
Packages may have subpackages
March 28, 2005
GIS in Action 2005
34
Part 3: GUI Programming


March 28, 2005
Cross-platform: wxPython, Tk,
GTK, Qt
Platform specific: MFC, .NET (?)
GIS in Action 2005
35
Python as Glue Language




win32all available for all versions
from python.org
ActiveState Python incorporates
Win32 features
Cross-platform GUI solutions
Future: Python .NET (IronPython) –
also somewhat cross-platform
March 28, 2005
GIS in Action 2005
36
Windows Integration




Using win32all extensions, Python
may operate as a COM client (e.g.
control MSFT Office applications)
Python may also operate as a COM
(or DCOM) server (e.g. callable from
VBA)
Other COM objects: DAO and ADO
for database access
MSF: Microsoft Foundation Classes
March 28, 2005
GIS in Action 2005
37