Python scripts and modules

Python in an interpretted language with dynamic typing, good support for high level data types, and good support for interaction with components developed in other languages.

It's a great tool for prototyping systems, and (once built) it is relatively easy to recode individual components in other languages for greater efficiency (if desired - in many cases the python implementation is adequate).

Using the interpretter

On *nix systems the python interpretter is usually located at /usr/local/bin/python, while on Windows it is usually installed in C:\Python24.

Python statements can then be typed directly for interpretation. (Type in a block of statements, then ^D when you're done and want them interpretted. You'll see a >>> prompt when the interpretter is waiting for a new command, or a ... line continuation prompt when in the midst of typing a command.)

To leave the interpretter use ^D (*nix) or ^Z (Windows).

Writing Python Scripts

To store Python code in a file and execute it directly, make the file executable (chmod u+x filename) and begin the file with the following line:

#! /usr/bin/env python

Including code from other Python scripts (Modules)

If you have a file (e.g. mystuff.py) containing Python code you wish to include in a variety of places, you use the import mystuff statement to access the module. (Note you import the filename minus the .py extension.)

Then, to access functions that are in the imported file you use the module name like an object and the specific function like a method, e.g.

import mystuff

# assuming mystuff.py contains a function named myfunction
x = mystuff.myfunction(3, 4, 5)
Packages are essentially groups of modules and submodules all packaged together. With packages, you can import or refer to specific modules or functions with statements such as import somemodule.somesubmodule.somesubsubmodule.

Note that there is an extensive collection of standard Python modules available for you to include, here's a quick list of a few of them: cgi, cookielib, datetime, email, hashlib, HTMLParser, math, os, pydoc, random, re (regular expressions), socket, sys, StringIO, tarfile, threading, wave, xml.dom