Python Programming

Python Cookbook

Tools

Get these tools and set them up. Python has distribution for Mac, Windows, and Unix variants so portability is not an issue at all (at least, haven’t seen any issue that I can think of atm)
Component and tools used

The following components are required:

  • Python Runtime Environment
    Python Download page ( http://www.python.org/download/ ) – pick your distribution!
  • Console / Command line application
    Windows users who need to code on python remotely may also need terminal client like PuTTY
  • PyDev (Eclipse)
    If you are working on python locally in your computer, whether that be Mac, Windows, or Linux, then you should use PyDev.
    Point your eclipse to this update site (http://pydev.sourceforge.net/updates). If lost, quick instruction can be found In the PyDev download page.

Hello World

In Java and C, coding is done in 3 stage: writing the source, compiling it into binary, and then running it. In python, it works more like a script (bash, perl). That is, you write your python source, then you run it through an interpreter. Here’s Hello World in Python:

def getHelloWorldStr(param):
   str = "Hello World, %s " % param
   return str
 
def main():
   """
   multi-line comments
   """
   print "%s" % getHelloWorldStr('python')
 
if __name__ == "__main__":
    main()

Create a new file and save it as helloworld.py
to run it do the following command

$ python helloworld.py

If you’re using pydev / Eclipse, right click and run it as python code. Same way as how you would run a Java code.

Because it works like a script, the main() method isn’t really needed. But it’s a good practice to have a main method, as a lot of programmer sill immediately understand that the main() method is the entry point to your program, the starting point of everythng.

if __name__ == "__main__":
    main()

Basically tells python that the main() method is the main method.

System arguments (aka argv[])

Accessing system argument is done through the package sys:

import sys
print sys.argv[0] # prints the command line argument 0

String Handling

Common string operation in python, full reference can be found here:
ref: http://docs.python.org/lib/string-methods.html

# Suppose you have this string
str = "the quick brown fox jumps over the lazy dog"

Find a substring

if "lazy" in str:
 print "the str %s has 'lazy' in the string" % str

Index of substring

# Finds the left index of
idxStart = str.find('lazy') # got 35

Obtain substring

# substring extraction
ex1 = str[ 0:19 ] # 'the quick brown fox'
ex1 = str[ :19 ] # 'the quick brown fox'
ex2 = str[ 35:39 ] # 'lazy'
ex3 = str[ 35:43 ] # 'lazy dog'
ex3 = str[ 35: ] # 'lazy dog'

Number Handling

String to number conversion:

# suppose we have this numerical string
str = '0222'
# by default, its decimal
decNum = int(str)
# convert to base 2 number
binNum = int(str,2)
# convert to base 16 number
hexNum = int(str,16)

String to Float conversion

str = "-30.0"
floatNum = float(str)

Working with Dates

IMO, this is the best reference: http://pleac.sourceforge.net/pleac_python/datesandtimes.html

get timestamp

import datetime
now = datetime.datetime.now()

Working with Collections

Unlike Java, there’s no TreeMap, HashSet, Vectors, ArrayList etc in python. Python only have 3 data types, and these 3 handles virtually everything. They are:
Unlike Java, there’s no TreeMap, HashSet, Vectors, ArrayList etc in python. Python only have 3 data types, and these 3 handles virtually everything. They are:

Dictionary

This is the equivalent of a map in Java’s collection family:

# declaration
dict = {
   'key1':'value1',
   'key2':'value2',
   'key3':'value3'
}
# prints 'value2'
print dict['key2']
 
# empty declaration + assignment of key-value pair
emptyDict = {}
emptyDict['key4']='value4'
 
# looping through dictionaries (keys and values)
for key in dict:
   print dict[key]
 
# sorting keys and accessing their value in order
keys = dict.keys()
keys.sort()
for key in keys:
   print dict[key]
 
# looping their values directory (not in order)
for value in dict.values():
   print value
 
# getting both the keys and values at once
for key,value in dict.items():
   print "%s=%s" % (key,value)
 
# deleting an entry
del dict['key2']
 
# size of the dictionary
len(dict)

List

List is the equivalent of classes like Vector or ArrayList(in Java)

# declaration (empty list)
list = ['old','and']
 
# adding a record in
list.append('new')
 
# this will print 'old and new', each in a new line
for rec in list:
    print rec

Tuples

Tuples are like list, but they are immutable once created.

# tuple declaration
aTuple = ('an','immutable','tuple')

Reflection (sort of) and reference-loading

Using getattr is my weapon of choice. getattr uses 2 things to get a reference to the class or method you want to dynamically load.

ref = getattr(source,attrName)

attrName refers to the class/method/attr you want to load. So if you want to load a class, source is the modulcle reference. If you want to load a method, source is the class reference and so on. Examples:

Method loading from a class:

The class is first instantiated and referenced by variable ‘classRef’

# suppose there's this class
class MyClass:
 def myFunction(self, param1):
 return " helloWorld %s " % param1
 
# this will grab a reference to the function
classRef = MyClass()
funcName = 'myFunction'
funcRef = getattr(classRef ,funcName)
 
# this will call the function
result = funcRef('PRINTME')
print result

Class loading from a module:

Suppose the name of the module is mymodule.py

# suppose there's this class
class MyClass:
 def myFunction(self, param1):
 return " helloWorld %s " % param1
 
# dynamic module loading:
moduleRef = __import__('mymodule')
 
# class loading:
className = 'MyClass'
classRef = getattr(moduleRef, funcName)
 
# function loading:
funcName = 'myFunction'
funcObj = getattr(classRef ,funcName)
 
# this will call the function
result = funcObj('PRINTME')
print result

Non-dynamic Module loading:

You can also load a module non-dynamically. Via the import method! This is used if you knew the module to load beforehand

import mymodule
 
# suppose there's this class
class MyClass:
 def myFunction(self, param1):
 return " helloWorld %s " % param1
 
# class loading:
className = 'MyClass'
classRef = getattr(mymodule, funcName)

Shell integration

Executing shell command

This is basically doing a system call to the underlying runtime environment. Very useful for doing

import os
 
try : os.system('ls')
except:
 pass #handle exception

Obtaining environment variable

This is basically doing a system call to the underlying runtime environment. Very useful for doing

import os
 
for param in os.environ.keys():
    print "%20s %s" % (param,os.environ[param])
 
var value=os.environ['ENV_VARIABLE_NAME']

XML-RPC

xml-rpc is a very neat thing and it is very easy to do in Python. I am amazed at its simplicity and usefulness.

Server skeleton code

from SimpleXMLRPCServer import SimpleXMLRPCServer
class DynamicXMLRPCServer(SimpleXMLRPCServer):
"""
An XMLRPC server that will register all function starting with a cmd_
on initiation
"""
    def __init__(self, host, params):
        self.host=params['host']
        self.port=params['port']
        SimpleXMLRPCServer.__init__(self,(self.host, self.port))
        self.registerCommands()
 
    def registerCommands(self):
    """
    Loops through every name in the current context (self)
    and register it as xml-rpc function if the name begins with cmd_
    """
        for name in dir(self):
        if name[:4] == "cmd_":
            function=getattr(self,name)
            print "registering function: %s as %s " % ( name, name[4:] )
            self.register_function(function, name[4:])
 
    # example function
    def cmd_helloWorld(self):
        return "hello world"
 
# usage:
server = DynamicXMLRPCServer({'host':'localhost','port':8000})
print "Listening on port 8000..."
# At this point, you should get a message indicating that
# cmd_helloWorld is registered as 'helloWorld'
server.serve_forever()

Client skeleton code

This will invoke Hello world function via xml-rpc

import xmlrpclib
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
response = proxy.helloWorld()
print response

DocStrings

Docstrings (stands for documentation string) are really Python’s javadocs. However different from Javadocs, docstrings are fully qualified Python objects that can be worked on programmatically, as shown by this example below:

Example

class TestClass:
   """
   Documentation for this test class...
   """
 
   def helloWorld(self):
      """
      HELLO WORLD DOCSTRING
      """
      func=getattr(self,'helloWorld')
      docstr=func.__doc__ #<--- notice this?
      print docstr
 
# 'quickie' main block
test=TestClass()
test.helloWorld()

Number Handling