16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

370 CHAPTER 17 ■ EXTENDING PYTHON<br />

/* An initialization function for the module (the name is<br />

significant): */<br />

PyMODINIT_FUNC initpalindrome() {<br />

Py_InitModule("palindrome", PalindromeMethods);<br />

}<br />

Most of the added stuff in Listing 17-6 is <strong>to</strong>tal boilerplate. Where you see palindrome, you<br />

could insert the name of your module, while where you see is_palindrome, insert the name of<br />

your function. If you have more functions, simply list them all in the PyMethodDef array. One<br />

thing is worth noting, though: The name of the initialization function must be initmodule,<br />

where module is the name of your module; otherwise, <strong>Python</strong> won’t find it.<br />

So, let’s compile! You do this just like in the section on SWIG, except that there is only one<br />

file <strong>to</strong> deal with now. Here is an example using gcc (remember <strong>to</strong> add -fPIC in Solaris):<br />

gcc -I$PYTHON_HOME -I$PYTHON_HOME/Include -shared palindrome2.c -o palindrome.so<br />

Again, you should have a file called palindrome.so, ready for your use. Put it somewhere in<br />

your PYTHONPATH (such as the current direc<strong>to</strong>ry) and away we go:<br />

>>> from palindrome import is_palindrome<br />

>>> is_palindrome('foobar')<br />

0<br />

>>> is_palindrome('deified')<br />

1<br />

And that’s it. Now go play. (But be careful; remember the Waldi Ravens quote from the<br />

Introduction.)<br />

A Quick Summary<br />

Extending <strong>Python</strong> is a huge subject. The tiny glimpse provided by this chapter included the<br />

following:<br />

Extension philosophy. <strong>Python</strong> extensions are useful mainly for two things: for using<br />

existing (legacy) code, or for speeding up bottlenecks. If you’re writing your own code<br />

from scratch, try <strong>to</strong> pro<strong>to</strong>type it in <strong>Python</strong>, find the bottlenecks, and fac<strong>to</strong>r them out as<br />

extensions if needed. Encapsulating potential bottlenecks beforehand can be useful.<br />

Jython and Iron<strong>Python</strong>. Extending these implementations of <strong>Python</strong> is quite easy: You<br />

simply implement your extension as a library in the underlying implementation (Java for<br />

Jython and C# or some other .NET language for Iron<strong>Python</strong>) and immediately the code is<br />

usable in your <strong>Python</strong>.<br />

Extension approaches. There are plenty of <strong>to</strong>ols for extending or speeding up your code:<br />

for making the incorporation of C code in<strong>to</strong> your <strong>Python</strong> program easier, for speeding up<br />

common operations such as numeric array manipulation, or for speeding up <strong>Python</strong> itself.<br />

Such <strong>to</strong>ols includ SWIG, Psyco, Pyrex, Weave, NumPy, ctypes, subprocess, and modula<strong>to</strong>r.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!