Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Zpugdccherry 101105081729-phpapp01

1,082 views

Published on

  • Be the first to comment

  • Be the first to like this

Zpugdccherry 101105081729-phpapp01

  1. 1. Producing Python Wrappers for C++ Libraries Using SWIG Joshua L. Cherry, Ph.D. National Center for Biotechnology Information National Library of Medicine National Institutes of Health [email_address]
  2. 2. SWIG Simplified Wrapper and Interface Generator www.swig.org
  3. 3. Two Ways to Extend Python <ul><li>Write functions, classes, etc. in Python </li></ul><ul><li>Write C/C++ </li></ul><ul><ul><li>Use Python C API </li></ul></ul><ul><ul><li>Usually turn into shared library (dll) </li></ul></ul>
  4. 4. Why C/C++? <ul><li>Performance </li></ul><ul><li>Do things that couldn’t otherwise be done </li></ul><ul><li>Use already existing C/C++ code </li></ul>
  5. 5. <ul><li>double foo(int n); </li></ul>
  6. 6. <ul><li>double foo(int n); </li></ul><ul><li>#include <Python.h> </li></ul><ul><li>PyObject *wrap_foo(PyObject *dummy, PyObject *args) { </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  7. 7. <ul><li>double foo(int n); </li></ul><ul><li>#include <Python.h> </li></ul><ul><li>PyObject *wrap_foo(PyObject *dummy, PyObject *args) { </li></ul><ul><li>PyObject *resultobj; </li></ul><ul><li>int n; </li></ul><ul><li>double result; </li></ul><ul><li>PyArg_ParseTuple(args, “i&quot;, &n); </li></ul><ul><li>result = foo(n); </li></ul><ul><li>resultobj = PyFloat_FromDouble(result) </li></ul><ul><li>return resultobj; </li></ul><ul><li>} </li></ul>
  8. 8. <ul><li>double foo(int n); </li></ul><ul><li>int bar(int n, int m); </li></ul><ul><li>#include <Python.h> </li></ul><ul><li>PyObject *wrap_foo(PyObject *dummy, PyObject *args) { </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>PyObject *wrap_bar(PyObject *dummy, PyObject *args) { </li></ul><ul><li>PyObject *resultobj; </li></ul><ul><li>int n, m; </li></ul><ul><li>int result; </li></ul><ul><li>PyArg_ParseTuple(args, “ii&quot;, &n, &m); </li></ul><ul><li>result = bar(n, m); </li></ul><ul><li>resultobj = PyInt_FromLong(result) </li></ul><ul><li>return resultobj; </li></ul><ul><li>} </li></ul>
  9. 9. Some Things SWIG Does for You <ul><li>Full support for structs and classes, including inheritance </li></ul><ul><li>Support for overloaded functions and functions with default arguments </li></ul><ul><li>Exception handling that you specify </li></ul><ul><li>Numerous customization capabilities </li></ul>
  10. 10. Shared Library C++ Code Python Code Compile, link SWIG Interface Files (point at C++ headers) SWIG
  11. 11. Simple SWIG Interface File <ul><li>%module example </li></ul><ul><li>%include my_header1.hpp </li></ul><ul><li>%include my_header2.hpp </li></ul><ul><li>void some_function(double x, int n); </li></ul><ul><li>%{ </li></ul><ul><li>#include “my_header1.hpp” </li></ul><ul><li>#include “my_header2.hpp </li></ul><ul><li>void some_function(double x, int n); </li></ul><ul><li>%} </li></ul>
  12. 12. Some Customizations and Features <ul><li>Simple customization with %rename, %ignore, %extend, %pythoncode, etc. </li></ul><ul><li>Specialize C++ classes in Python with “directors” </li></ul><ul><li>Customize behavior by writing “typemaps” </li></ul><ul><li>SWIG Library: very useful library of SWIG code, e.g., exception handling, typemaps, stl support </li></ul>
  13. 13. %module example %include stl.i %template(vector_int) std::vector<int>; int prod(const std::vector<int>& vec); >>> import example >>> vec = example.vector_int() >>> vec.append(42) # append mapped to vector::push_back >>> vec.append(98) >>> vec.append(47) >>> len(vec) # __len__ mapped to vector::size 3 >>> example.prod(vec) # normal class usage 193452 >>> >>> example.prod([42, 98, 47]) # works because of &quot;in&quot; typemap 193452 >>> vec2 = example.vector_int([42, 98, 47]) STL Support Example
  14. 14. NCBI C++ Toolkit <ul><li>C++ for speed and programmer control </li></ul><ul><li>Vast: about 3200 classes, 44,000 functions </li></ul><ul><ul><li>General-purpose functionality </li></ul></ul><ul><ul><li>Mission-specific functionality </li></ul></ul><ul><ul><li>Includes handling of ASN.1 objects </li></ul></ul>
  15. 15. Scripting Languages <ul><li>E.g., Perl and Python </li></ul><ul><li>“High-level” </li></ul><ul><li>No (explicit) compilation necessary </li></ul><ul><li>Usually lots of functionality included, and more available separately </li></ul>
  16. 16. Why Make Scripting Interfaces? <ul><li>Large number of existing programmers </li></ul><ul><li>Easy to learn </li></ul><ul><li>Easy to use </li></ul><ul><li>Allow interactive use </li></ul><ul><li>Ability to combine Toolkit functions with scripting language functions </li></ul><ul><li>Embedding scripting languages in applications can make them customizable </li></ul>
  17. 17. Uses of Scripting Language Interfaces <ul><li>Writing end-product programs </li></ul><ul><li>Prototyping C++ programs </li></ul><ul><li>Interactive use for accomplishing things </li></ul><ul><li>Interactive use for exploring the C++ Toolkit </li></ul><ul><li>Introspection </li></ul>
  18. 18. The Interfaces <ul><li>SWIG generates code based on C++ headers and other information </li></ul><ul><li>Ancillary scripts necessary for proper behavior and customizations </li></ul><ul><li>Perl and Python supported </li></ul><ul><li>Interfaces largely mirror C++ API, but with important enhancements and customizations </li></ul>
  19. 19. Issues <ul><li>Languages are different </li></ul><ul><li>C++ templates must be instantiated at build time </li></ul><ul><li>SWIG far from perfect </li></ul><ul><li>Problems with the NCBI C++ Toolkit </li></ul><ul><li>Large size of monolithic wrapper </li></ul>
  20. 20. Enhancements and Customizations <ul><li>Easier ASN.1/XML reading and writing </li></ul><ul><li>Easy access to NCBI documentation </li></ul><ul><li>Transparent reference counting for reference-counted objects </li></ul><ul><li>Other add-on features </li></ul>
  21. 21. Josh Cherry [email_address] SWIG www.swig.org NCBI C++ Toolkit Wrappers www.ncbi.nlm.nih.gov/cvsweb/index.cgi/internal/c++/src/wrappers/

×