In our examination of the Python language so far we've looked at the basics, some of the advanced stuff and a few architectural elements. This week we'll look at Python in practice.
Back in the first part of this series on Python we ran up the interpreter and talked about running IDLE, the Python user interface (really a glorified command-line environment). Python also can be run directly from the system command line. You also can configure all sorts of operating parameters, such as where modules are loaded from, what Python file should be run automatically on start-up and what debugging options should be used.
How you set these parameters will depend on which operating system you are running. On the Macintosh there's a separate utility for editing the configuration file; under Windows and Unix you specify the parameters or file containing them on the command line.
Now remember: Python is a script language - the Python interpreter reads a plain text source file and translates it to byte code or intermediate language. This byte code is saved in a file of type .pyc in the same subdirectory as the source file (its file type will be .py).
Python is smart enough to keep the interpreted byte-code version and use it instead of the source version for subsequent programs, as long as the date and time stamp on the original .py file aren't later than the .pyc file. The .pyc files are platform-independent so a common directory can be shared over a network.
Another form of byte-code file Python creates are files of the type .pyo, which are optimized by removing certain control statements. This type also can cause problems and prevent successful execution.
Although .pyc and .pyo files run no faster than starting with the original source file, you lose the load time it takes for the interpreter to read the source file and translate it to byte code (which can be significant).
As it happens, you can use the .pyc and .pyo files for distribution because it is not easy to reverse-engineer these formats.
In the first part we mentioned that Python can be used as a Common Gateway Interface interpreter. Under Microsoft's Internet Information Server you can install Python through the Internet Services Manager. Open the properties of the individual server you want to change, or, if you want to apply the service to all Web servers, use the WWW Service Master properties. On the Home Directory tab under the Application Settings click the Configuration button.
This shows the defined file handlers. Click the Add button and enter the following:
Executable: "path\python.exe" -u "%s" "%s"
Change the path to match the path to the Python executable and then, optionally, add similar entries for the extension .pyc and, if you plan to use it, .pyo. The quotes are important. Also note that the switch "-u" is required to use unbuffered binary input and output (this means each character is sent and received immediately as a full eight-bit character - buffering must not be used in a CGI environment).
But wait! There's more!
Spyce is a server-side language that provides Python-based dynamic HTML generation a la JSP or ASP. Spyce is Python embedded in HTML. It also can be used as a command-line utility for static text pre-processing or as a Web server proxy.
Spyce consists of a small core module with additional modules, including access to HTTP requests, generation of HTTP responses and error handling. A preliminary release of workflow automation also is available that provides state machine-based application design.
Other Python-based projects include:
SimPy: A freeware process-based discrete-event simulation language based on standard Python that provides all the components of a simulation model.
Mailman A free Web-integrated mailing list manager that offers built-in archiving, automatic bounce processing, content filtering, digest delivery and spam filtering.
Zope (DocFinder: 6337): An open source application server that provides content management, portals and custom applications written in Python, and includes a built-in Web server and search engine.
Python is a remarkable language that definitely should be considered for projects ranging from routine network management tasks and applications support to driving serious enterprise solutions.
If you're using Python, let us know! Brag away at firstname.lastname@example.org.