The SoCal Piggies had their twelfth meeting at USC (Salvatori Computer Science Center) on March 23rd 2006 at 7:00 PM. Seven Piggies attended: Daniel Arbuckle, Steve Williams, Grig Gheorghiu, Howard Golden, Diane Trout, Titus Brown, and Mark Kohler.

Diane Trout presented an [http://www.socal-piggies.org/presentations/diane/2006_03_23/boost.html overview of Boost Python], which is a library that represents -- in Diane's words -- "Yet another way of making C/C++ code available to python".

[http://boost.org/libs/python/doc/index.html Boost.Python] is just one of the free, peer-reviewed, portable C++ source libraries offered by the [http://boost.org/ Boost project]. Interestingly enough, Python is the only dynamic/interpreted language for which a Boost library is available.

Why would you want to use Boost.Python and not Pyrex or SWIG? Diane answered that question for us by pointing out that Pyrex has poor support for C++ and is intended for C library integration, while SWIG's handling of C++ is functional, but hackish. SWIG handles wrapping C++ by converting C++ calls into C functions, and then it produces a C++ shadow class that uses the C representation. In contrast, Boost.Python gets rid of shadow classes by using C++ template magic. Those interested in how exactly this is accomplished would do well to read the [http://boost.org/libs/python/doc/internals.html Boost.Python internals section]. One more thing that's worth mentioning is that Boost.Python also allows you to go in the other direction and seamlessly expose Python classes, functions and objects to C++.

Diane proceeded to show us examples of C++ code that gets translated to Python. See her [http://www.socal-piggies.org/presentations/diane/2006_03_23/boost.html slides], and also the [http://boost.org/libs/python/doc/tutorial/doc/html/index.html Boost.Python tutorial] for more details. Here's how to wrap the canonical "hello, world":

Here's the C++ function:

char const* greet()
{
   return "hello, world";
}

Here's the Boost.Python wrapper you need to write:

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}

After building the wrapper as a shared library, you can import and use it in your Python code:

>>> import hello
>>> print hello.greet()
hello, world

Pretty nifty stuff.

One question for Diane was whether the Boost guys unit test their code. It turns out that not only do they do it, but they also offer a unit test library as part of the Boost collection: the [http://www.boost.org/libs/test/doc/index.html Boost Test Library]. Check it out if you need a unit test framework for your C++ code.

After Diane's presentation, it came the time to talk of many things -- of cabbages, and kings, and whether pigs have wings. Some other subjects that were discussed:

Many thanks to Diane for presenting, and to Daniel for providing the room at USC.

Proposed agenda for the next meeting:

TwelfthMeeting (last edited 2008-12-11 16:41:16 by 65)