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