Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2007-04-09 12:58:02
Size: 4599
Comment:
Revision 4 as of 2007-04-09 14:16:45
Size: 5082
Editor: DavidBoddie
Comment: I hate marking everything up with trademark symbols.
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
.. |reg| unicode:: U+00AE
Line 28: Line 29:
For systems written in Java, Jython (http://www.jython.org) is an implementation of Python written in pure For systems written in Java\ |reg|, Jython (http://www.jython.org) is an implementation of Python written in pure
Line 44: Line 45:
IronPython runs on both Microsoft's .NET and on Novell's Mono. IronPython runs on both Microsoft\ |reg| .NET and on Novell\ |reg| Mono.
Line 54: Line 55:
available from http://www.python.org and included in MacOS X
and many Linux distributions. A Microsoft Windows
available from http://www.python.org and included in Mac OS X\ |reg|
and many Linux distributions. A Microsoft Windows\ |reg|
Line 107: Line 108:
Originally written for wrapping Trolltech's Qt libraries, SIP is now
used for other projects as well.
Originally written for wrapping the Qt\ |reg| libraries from Trolltech\ |reg|, SIP is now used for other projects as well.
Line 116: Line 116:
 // Define the SIP wrapper to the word library.  // Define the SIP wrapper to the word library.
Line 118: Line 118:
 %Module word 0  %Module word 0
Line 120: Line 120:
 class Word {  class Word {
Line 122: Line 122:
 %TypeHeaderCode
 #include <word.h>
 %End
 %TypeHeaderCode
 #include <word.h>
 %End
Line 126: Line 126:
 public:
  Word(const char *w);
 public:
  Word(const char *w);
Line 129: Line 129:
  char *reverse() const;
 };
  char *reverse() const;
 };
Line 143: Line 143:
 static PyObject *
 PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
 {
  int lines;
  int columns;
  int result;
 static PyObject *
 PyCurses_Is_Term_Resized(PyObject *self, PyObject *args)
 {
  int lines;
  int columns;
  int result;
Line 150: Line 150:
  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;
 
}
 }
  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;
 
}
 }
Line 169: Line 169:
Python\ |reg| is a registered trademark of the Python Software Foundation. Python is a registered trademark of the Python Software Foundation.
Line 171: Line 171:
Java Java is a registered trademark of Sun Microsystems, Inc. in the United States and other countries.
Line 173: Line 173:
MacOS X Mac OS is a trademark of Apple Inc., registered in the U.S. and other countries.
Line 175: Line 175:
Mono Mono is a trademark of Novell, Inc. in the United States and other countries.
Line 179: Line 179:
Qt Qt is a trademark of Trolltech in Norway, the United States and other countries.
Line 183: Line 183:
Windows Windows is a registered trademark of Microsoft Corporation in the United States and other countries.

Python as a Glue Language

Introduction

The Python® 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® .NET and on Novell® 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 Mac OS 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 the Qt® libraries from Trolltech®, 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

AdvocacyWritingTasks/GlueLanguage (last edited 2008-11-15 14:00:19 by localhost)

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