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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

358 CHAPTER 17 ■ EXTENDING PYTHON<br />

■Tip Encapsulate potential bottlenecks.<br />

You may find that you don’t need <strong>to</strong> replace the bottlenecks with C extensions (perhaps<br />

you suddenly got ahold of a faster computer), but at least the option is there.<br />

There is another situation that is a common use case for extensions as well: legacy code.<br />

There may be some existing code that you want <strong>to</strong> use, and it exists only in, say, C. You can then<br />

“wrap” this code (write a small C library that gives you a proper interface) and create a <strong>Python</strong><br />

extension library from your wrapper.<br />

In the following sections, I give you some starting points for extending both the classical C<br />

implementation of <strong>Python</strong>, either by writing all the code yourself or by using a <strong>to</strong>ol called SWIG,<br />

and for extending two other implementations: Jython and Iron<strong>Python</strong>. You will also find some<br />

hints about other options for accessing external code. Read on . . .<br />

WHAT ABOUT FORTRAN?<br />

While I won’t explicitly discuss how <strong>to</strong> connect <strong>Python</strong> with Fortran in this chapter, it is certainly possible.<br />

Fortran was the first “real” programming language (originally developed in 1954). Still, in some areas, Fortran<br />

is the language of choice for high-performance computing. If you want <strong>to</strong> (or, perhaps more likely, have <strong>to</strong>) use<br />

Fortran for your extensions, you should check out Pyfort (http://pyfortran.sf.net) and F2PY (http://<br />

cens.ioc.ee/projects/f2py2e).<br />

The Really Easy Way: Jython and Iron<strong>Python</strong><br />

If you happen <strong>to</strong> be running Jython or Iron<strong>Python</strong> (both mentioned in Chapter 1), extending<br />

your <strong>Python</strong> with native modules is quite easy. The reason for this is that Jython and Iron<strong>Python</strong><br />

give you direct access <strong>to</strong> modules and classes from the underlying languages (Java for Jython and<br />

C# and other .NET languages for Iron<strong>Python</strong>), so you don’t have <strong>to</strong> conform <strong>to</strong> some specific<br />

API (as you have <strong>to</strong> when extending C<strong>Python</strong>)—you simply implement the functionality you<br />

need, and, as if by magic, it will work in <strong>Python</strong>. As a case in point, you can access the Java standard<br />

libraries directly in Jython and the C# standard libraries directly in Iron<strong>Python</strong>.<br />

Listing 17-1 shows a simple Java class. You can compile this with some Java compiler, such<br />

as javac (freely downloadable from http://java.sun.com).<br />

Listing 17-1. A Simple Java Class (JythonTest.java)<br />

public class JythonTest {<br />

}<br />

public void greeting() {<br />

System.out.println("Hello, world!");<br />

}

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

Saved successfully!

Ooh no, something went wrong!