Revision 2 as of 2007-04-09 13:56:24

Clear message

Python as a Glue Language

Introduction

The Python|reg| language interpreter can be used as a glue language to connect software components. Components can then be manipulated by Python scripts and combined in new ways.

What can you do with scripting access to an existing system?

  • Providing adaptability
  • Scripting small tasks
  • Testing
  • Education and initial learning

Jython for Java Components

For systems written in Java, Jython (http://www.jython.org) is an implementation of Python written in pure Java that provides automatic access to Java classes from both scripts and an interactive prompt.

The following small script demonstrates using Swing from Jython:

XXX write example

IronPython for CLR Components

IronPython, an implementation of Python written in C#, provides automatic access to CLR/.NET assemblies. IronPython runs on both Microsoft's .NET and on Novell's Mono.

The following small script demonstrates using XXX from IronPython:

C/C++ Systems

The most widely used Python interpreter is the C implementation available from http://www.python.org and included in MacOS X and many Linux distributions. A Microsoft Windows version is available from http://www.python.org/. A variety of tools exists to interface between Python and C code.

ctypes

The ctypes package is a foreign-function interface included with Python 2.5 and later versions that can load shared libraries (.dylib files on MacOS X, .so files on Linux, DLLs on Windows) and call arbitrary library functions.

The following example uses the PAM authentication library to XXX:

XXX write example

For more information about ctypes, refer to the ctypes section in the Python documentation (2.5 version).

Pyrex

Pyrex (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/) is a compiler that translates a Python-like language into C code for an extension module.

The following example: XXX

XXX example

SWIG for C/C++ libraries across languages

SWIG, the Simple Wrapper Interface Generator (http://www.swig.org), parses C/C++ header files and custom interface descriptions, generating C code for an extension wrapping the C functions and data types. SWIG can use the same input to generate wrappers for several different language environments; supported languages other than Python include Perl, Tcl, Ruby, PHP, Java, and Common Lisp.

The following example XXX:

SIP for C++ libraries

SIP (http://www.riverbankcomputing.co.uk/sip/) parses interface specifications to create Python bindings for C and C++ libraries. Originally written for wrapping Trolltech's Qt libraries, SIP is now used for other projects as well.

The following SIP example wraps a C++ class called Word, making the class constructor and the reverse() method available as a Python module called word.

// Define the SIP wrapper to the word library.

%Module word 0

class Word {

%TypeHeaderCode
#include <word.h>
%End

public:
    Word(const char *w);

    char *reverse() const;
};

Python's C API

The Python interpreter has a documented C API for writing extension modules. Writing simple wrappers atop a C library is a straightforward task

The following example from Python's source code wraps the is_term_resized() function provided by the curses screen-handling library:

static PyObject *
PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
{
  int lines;
  int columns;
  int result;

  if (!PyArg_ParseTuple(args,"ii:is_term_resized", &lines, &columns))
    return NULL;
  result = is_term_resized(lines, columns);
  if (result == TRUE) {
    Py_INCREF(Py_True);
    return Py_True;
  } else {
    Py_INCREF(Py_False);
    return Py_False;
  }
}

Embedding Python

Docutils System Messages

System Message: ERROR/3 (<string>, line 9); backlink

Undefined substitution referenced: "reg".

System Message: ERROR/3 (<string>, line 168); backlink

Undefined substitution referenced: "reg".

Unable to edit the page? See the FrontPage for instructions.