Differences between revisions 1 and 2
Revision 1 as of 2007-04-09 12:58:02
Size: 4599
Comment:
Revision 2 as of 2007-04-09 13:56:24
Size: 5041
Editor: DavidBoddie
Comment: Added some trademark information.
Deletions are marked like this. Additions are marked like this.
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 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 registered trademark of Trolltech ASA and/or its subsidiaries in 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|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".

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

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