Content
What is Python?
Python is a very high level, object oriented, dynamically typed, programming language. Created by Guido von Rossum and first released in 1991, Python aims to increase programmer efficiency, sometimes at the expense of program efficiency.Python programs are compiled into byte code and interpreted. However, the compilation does not need to be explicitly invoked. The Python interpreter will re-compile the source file (generally with extension .py) to byte code file with extension .pyc when the source file is newer then the byte code file.
Python's syntax is minimalistic, easy to read, write and maintain. The absence of syntactical help characters or words, such as { and }, begin/end or "$*&', makes the source code compact. Compared to C or Java, Python programs can be written with less lines of code.
Python is available for all major operating systems. It can be downloaded from the Python home page at http://www.python.org/. The correct version is 2.4.1.
The directory $NVOSS_HOME/python contains scripts setup.sh and setup.csh to setup enviroment variables. After running setup.sh (for bash users) and setup.csh (for csh users), the enviroment variable PYTHONPATH will include the directory $NVOSS_HOME/python/lib and $NVOSS_HOME/python/samples.
Hello World!
One simple way to run Python is in interactive mode.$ python Python 2.4.1 (#1, May 27 2005, 18:02:40) [GCC 3.3.3 (cygwin special)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> print 2+3 5 >>>The example above shows Python in interactive mode. After the greetings, the Python prompt '>>>' indicates that the system is ready to accept Python code. In this case, the command was "print 2+3", and the result is printed, followed by another prompt. To quit press Ctrl-D.
print "Hello World!"Create a file containing the line as shown above and save it as hello.py. Then execute the Python program with:
$ python hello.py Hello World!
A real Python program
Perhaps the most visible feature of Python is the use of white space to structure the program. Instead of using { and } or begin/end to describe logical blocks, Python uses identation. Program lines having the same number of space or tab characters at the beginning of the line belong to the same logical block.
""" This is an example. wordCount.py
"""
import sys
table = {}
for line in file (sys.argv[1]):
for word in line.split ():
try:
table[word] += 1
except:
table[word] = 1
wc = 0
for word,cnt in table.items():
print word, cnt
wc += cnt
print "Total %d words" % wc
The source of the example can be found in the directory
$NVOSS_HOME/python/tutorial. To execute enter:$ python wordCount.py test.txt This 10 a 8 is 10 another 2 test. 10 Total 40 wordsThe first two lines of the example above show that the triple quote marks start and end of comments. The next line shows that the module sys is imported. See http://docs.python.org/modindex.html for a list of available modules.
A dictionary (also known as associative array or map) is initialized and assigned to the variable table. The built_in function file() returns an object that is an iterable. Each iteration of the for-loop will read the next line in the file. The built-in method split() takes a string and returns an list of words in the string. In this example, white space assumed to be the separator between words.
Python promotes the programming philosopy known as EAFP (easier to ask for forgiveness than permission). In the example, the value of the element table[word] incremented by 1 regardless whether the element exists. If that fails, then a new entry in the table is defined and initialized with 1.
The last for-loop shows a multiple assignment. The method item() returns a key-value pair, which is assigned to word and cnt respectively. The content is then printed to standard output.
Reading XML
For this example, we will use the VOTable module that is included in the NVO summer school package at $NVOSS_HOME/python/lib/VOTable.py. The class VOXML in the VOTable module implements a XML reader. The method parse() reads a XML file and converts it into an internal data structure. The method getContent() retrieves the content of selected node. It can be seen as a very simple version of XPATH.""" readXML.py """ from VOTable import * import sys rxml = VOXML () rxml.parse (sys.argv[1]) try: print rxml.getContent (sys.argv[2]) except: print "not found"To execute enter:
$ python readXML.py $NVOSS_HOME/java/data/abell.xml /VOTABLE/RESOURCE/DESCRIPTION HEASARC Browse data servicePlease send inquiries to mailto:request@athena.gsfc.nasa.gov
SOAP with SOAPpy
SOAPpy is a Python module that implements the SOAP protocol for both client and server side. SOAPpy was chosen for the NVO summer school because it is written in Python and can run on any platform that runs Python. It also offers good interoperability with other SOAP implementations.
""" sesame.py
"""
import VOTable
import sys
from StringIO import StringIO
from SOAPpy import SOAPConfig,WSDL,Types
class sesame:
def __init__ (self):
self.wsdl = 'http://cdsws.u-strasbg.fr/axis/services/Sesame?wsdl'
self.config = SOAPConfig ()
self.config.namespaceStyle = '2001'
self.config.typed = 1
self.config.buildWithNamespacePrefix = False
self.proxy = WSDL.Proxy (self.wsdl, config = self.config, noroot = 1)
self.config.argsOrdering = {'sesame': ('name', 'resultType', 'all', 'service')}
def resolve (self, name):
res = self.proxy.sesame (name=name, resultType='xpi',
all=Types.booleanType (1), service='SNVA')
self.xml = VOTable.VOXML (StringIO (res))
return self.xml.root.Sesame.Resolver
if __name__ == '__main__':
ses = sesame ()
res = ses.resolve (sys.argv[1])
for r in res:
print r.name, '*************************'
try:
print "RA=%s DEC=%s " % (r.jradeg.content, r.jdedeg.content)
except:
try:
print "RA=%s DEC=%s " % (r.jradeg[0].content, r.jdedeg[0].content)
except:
pass
try:
for a in r.alias:
print a
except:
pass
The Sesame name resolver service ( http://cdsweb.u-strasbg.fr/cdsws/name_resolver.gml) expects 4 parameters: name of object to be resolved, type of output, flag to indicate whether to include all identifiers and order of databases to search. In the example, the name is passed as method parameter. The service returns an XML including all identifiers of the object from searches in Simbad, NED and Vizier databases.
$ python sesame.py 'ngc234' Simbad ************************* RA=10.8851667 DEC=+14.3424444 NGC 234 UGC 463 IRAS F00409+1404 IRAS 00409+1404 LEDA 2600 [M98c] 004055.6+140410 MCG+02-02-028 QDOT B0040553+140410 Z 0040.9+1404 Z 434 - 32 Z 435 - 1 Ned ************************* VizieR ************************* RA=010.85 DEC=+14.34
MySQL Interface
Python provides a standardized API for database access. The MySQLdb module implements the DBAPI 2.0 and is available at http://sourceforge.net/projects/mysql-python. In the next example, we will use the SPOCS database, which was presented in another tutorial.
""" spocs.py
"""
import sys
import copy
import MySQLdb
class spocsDB:
def __init__ (self):
""" Connects to mysql server and gets a cursor
"""
self.db = MySQLdb.connect (host = 'localhost',
user = 'nvoss', passwd = '', db = 'spocs')
if not self.db:
print 'null db'
self.cursor = self.db.cursor (MySQLdb.cursors.DictCursor)
def query (self, qstr):
self.cursor.execute (qstr);
return self.cursor.fetchall ()
if __name__ == '__main__':
spocs = spocsDB ()
res = spocs.query (' '.join (sys.argv[1:]))
for row in res:
print row
The host name, user name and password may be different depending on your configuration. After instantiating the MySQLdb object, a cursor object is retrieved. MySQL does not implement cursors. However, MySQLdb provides a transparent software layer that emulates cursors.
$ python spocs.py select Ni,Na,Fe from spocs limit 3
{'Ni': 0.0, 'Fe': 0.0, 'Na': 0.0}
{'Ni': -0.42999999999999999, 'Fe': -0.44, 'Na': -0.34999999999999998}
{'Ni': -0.059999999999999998, 'Fe': 0.080000000000000002, 'Na': -0.20000000000000001}
Useful Links
| Python package | Link | |
|---|---|---|
| Numerical Python | http://numeric.scipy.org | |
| SciPy | http://www.scipy.org | Built on Numerical Python |
| Numarray | http://www.stsci.edu/resources/software_hardware/numarray |
A re-implementation of Numerical See PyRAF,PyFITS links |
| The stsci-python package | http://www.stsci.edu/resources/software_hardware/pyraf/stsci_python | |
| Tkinter | http://wiki.python.org/moin/TkInter | Interface to Tcl/Tk as graphic front end mostly bundled with Python installation package |
| Python Image Library | http://www.pythonware.com/products/pil/ | Handles many graphic formats |
| SWIG | http://www.swig.org/ | Simplified Wrapper and Interface Generator |
Shui Hung Kwok
NVO summer school 2005
