SlideShare a Scribd company logo
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]
SWIG Simplified Wrapper and Interface Generator www.swig.org
Two Ways to Extend Python Write functions, classes, etc. in Python Write C/C++ Use Python C API Usually turn into shared library (dll)
Why C/C++? Performance Do things that couldn’t otherwise be done Use already existing C/C++ code
double foo(int n);
double foo(int n); #include <Python.h> PyObject *wrap_foo(PyObject *dummy, PyObject *args) { ... }
double foo(int n); #include <Python.h> PyObject *wrap_foo(PyObject *dummy, PyObject *args) { PyObject *resultobj; int n; double result; PyArg_ParseTuple(args, “i&quot;, &n); result = foo(n); resultobj = PyFloat_FromDouble(result) return resultobj; }
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&quot;, &n, &m); result = bar(n, m); resultobj = PyInt_FromLong(result) return resultobj; }
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
Shared Library C++ Code Python Code Compile, link SWIG Interface Files (point at C++ headers) SWIG
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); %}
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
%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
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
Scripting Languages E.g., Perl and Python “High-level” No (explicit) compilation necessary Usually lots of functionality included, and more available separately
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
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
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
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
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
Josh Cherry [email_address] SWIG www.swig.org NCBI C++ Toolkit Wrappers www.ncbi.nlm.nih.gov/cvsweb/index.cgi/internal/c++/src/wrappers/

More Related Content

PDF
Python for the Mobile and Web
Derek Kiong
 
PPTX
C++ TRAINING IN AMBALA CANTT! BATRA COMPUTER CENTER
groversimrans
 
PPT
Introduction to Go programming
Exotel
 
PDF
The GNOME way - What can we learn from and within the Open Documentation World
Radina Matic
 
PPTX
What is Python? An overview of Python for science.
Nicholas Pringle
 
PPTX
1901200100000 presentation short term mini project on python
SANTOSHJAISWAL52
 
PPTX
Presentation on python
william john
 
PDF
Research paper on python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Python for the Mobile and Web
Derek Kiong
 
C++ TRAINING IN AMBALA CANTT! BATRA COMPUTER CENTER
groversimrans
 
Introduction to Go programming
Exotel
 
The GNOME way - What can we learn from and within the Open Documentation World
Radina Matic
 
What is Python? An overview of Python for science.
Nicholas Pringle
 
1901200100000 presentation short term mini project on python
SANTOSHJAISWAL52
 
Presentation on python
william john
 
Research paper on python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 

What's hot (20)

PPT
A First Look at Google's Go Programming Language
Ganesh Samarthyam
 
PPTX
Python Basics
Pooja B S
 
PPTX
Ctypes
嘉駿 戴
 
PPTX
Python presentation by Monu Sharma
Mayank Sharma
 
PPTX
difference between c c++ c#
Sireesh K
 
ODP
C Types - Extending Python
Priyank Kapadia
 
PDF
Python in real world.
[email protected]
 
PDF
Python Programming - I. Introduction
Ranel Padon
 
PDF
Extending Python with ctypes
Anant Narayanan
 
PPTX
Python and its Applications
Abhijeet Singh
 
PPTX
Python Programming Language
Laxman Puri
 
PDF
Python Basics | Python Tutorial | Edureka
Edureka!
 
PPTX
Python 101 for the .NET Developer
Sarah Dutkiewicz
 
PPTX
Python presentation
gaganapponix
 
PPTX
Introduction to python for Beginners
Sujith Kumar
 
PDF
علم البيانات - Data Sience
App Ttrainers .com
 
PPTX
Why Python?
Adam Pah
 
PPTX
C++ vs C#
sudipv
 
PPTX
Exploring SharePoint with F#
Talbott Crowell
 
DOCX
Programming in c plus plus2
AA Coaching Academy
 
A First Look at Google's Go Programming Language
Ganesh Samarthyam
 
Python Basics
Pooja B S
 
Ctypes
嘉駿 戴
 
Python presentation by Monu Sharma
Mayank Sharma
 
difference between c c++ c#
Sireesh K
 
C Types - Extending Python
Priyank Kapadia
 
Python in real world.
[email protected]
 
Python Programming - I. Introduction
Ranel Padon
 
Extending Python with ctypes
Anant Narayanan
 
Python and its Applications
Abhijeet Singh
 
Python Programming Language
Laxman Puri
 
Python Basics | Python Tutorial | Edureka
Edureka!
 
Python 101 for the .NET Developer
Sarah Dutkiewicz
 
Python presentation
gaganapponix
 
Introduction to python for Beginners
Sujith Kumar
 
علم البيانات - Data Sience
App Ttrainers .com
 
Why Python?
Adam Pah
 
C++ vs C#
sudipv
 
Exploring SharePoint with F#
Talbott Crowell
 
Programming in c plus plus2
AA Coaching Academy
 
Ad

Viewers also liked (9)

PDF
Real World Intranets - Joel Burton
Jeffrey Clark
 
PDF
Science To Bfg
Jeffrey Clark
 
PPTX
WRA 150 Week 10 Remix and IP Law
Miami University
 
PPTX
Diabetic Ketoacidosis in children
Abhijeet Deshmukh
 
PDF
Zpugdc deformpresentation-100709203803-phpapp01
Jeffrey Clark
 
ZIP
Fundamental JavaScript [In Control 2009]
Aaron Gustafson
 
PDF
Zpugdc2007 101105081808-phpapp01
Jeffrey Clark
 
PDF
Introduction to JavaScript
Bryan Basham
 
Real World Intranets - Joel Burton
Jeffrey Clark
 
Science To Bfg
Jeffrey Clark
 
WRA 150 Week 10 Remix and IP Law
Miami University
 
Diabetic Ketoacidosis in children
Abhijeet Deshmukh
 
Zpugdc deformpresentation-100709203803-phpapp01
Jeffrey Clark
 
Fundamental JavaScript [In Control 2009]
Aaron Gustafson
 
Zpugdc2007 101105081808-phpapp01
Jeffrey Clark
 
Introduction to JavaScript
Bryan Basham
 
Ad

Similar to Zpugdccherry 101105081729-phpapp01 (20)

PDF
Using SWIG to Control, Prototype, and Debug C Programs with Python
David Beazley (Dabeaz LLC)
 
PDF
Interfacing C/C++ and Python with SWIG
David Beazley (Dabeaz LLC)
 
PDF
PyHEP 2018: Tools to bind to Python
Henry Schreiner
 
PDF
Perl-C/C++ Integration with Swig
David Beazley (Dabeaz LLC)
 
PPTX
Python with a SWIG of c++
bobmcn
 
PPTX
Kostiantyn Grygoriev "Wrapping C++ for Python"
LogeekNightUkraine
 
PDF
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
David Beazley (Dabeaz LLC)
 
PDF
Easy native wrappers with SWIG
Javier Arturo Rodríguez
 
PDF
Mpi4py
A Jorge Garcia
 
PDF
OpenSAF Symposium_Python Bindings_9.21.11
OpenSAF Foundation
 
PDF
PyCon2022 - Building Python Extensions
Henry Schreiner
 
PDF
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
pythoncharmers
 
PDF
SunPy: Python for solar physics
segfaulthunter
 
PDF
speed up ntvv2 by php ext module
hazzaz
 
PDF
02 vng thanhnt-speedup_ntvv2_by_ph_pextmodule_
Nguyen Duc Phu
 
PDF
02. php ext module vng
Quang Anh Le
 
PDF
Speed up zing me – ntvv2 code with PHP extension module
Võ Duy Tuấn
 
PDF
Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module
Nguyen Thanh
 
PDF
Notes about moving from python to c++ py contw 2020
Yung-Yu Chen
 
PPT
Poing: a coder’s take on protein modelling
Biogeeks
 
Using SWIG to Control, Prototype, and Debug C Programs with Python
David Beazley (Dabeaz LLC)
 
Interfacing C/C++ and Python with SWIG
David Beazley (Dabeaz LLC)
 
PyHEP 2018: Tools to bind to Python
Henry Schreiner
 
Perl-C/C++ Integration with Swig
David Beazley (Dabeaz LLC)
 
Python with a SWIG of c++
bobmcn
 
Kostiantyn Grygoriev "Wrapping C++ for Python"
LogeekNightUkraine
 
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
David Beazley (Dabeaz LLC)
 
Easy native wrappers with SWIG
Javier Arturo Rodríguez
 
OpenSAF Symposium_Python Bindings_9.21.11
OpenSAF Foundation
 
PyCon2022 - Building Python Extensions
Henry Schreiner
 
Python for Science and Engineering: a presentation to A*STAR and the Singapor...
pythoncharmers
 
SunPy: Python for solar physics
segfaulthunter
 
speed up ntvv2 by php ext module
hazzaz
 
02 vng thanhnt-speedup_ntvv2_by_ph_pextmodule_
Nguyen Duc Phu
 
02. php ext module vng
Quang Anh Le
 
Speed up zing me – ntvv2 code with PHP extension module
Võ Duy Tuấn
 
Speed up ZingMe-Nông trại vui vẻ 2 with PHP extension module
Nguyen Thanh
 
Notes about moving from python to c++ py contw 2020
Yung-Yu Chen
 
Poing: a coder’s take on protein modelling
Biogeeks
 

More from Jeffrey Clark (19)

PDF
Python memory management_v2
Jeffrey Clark
 
PDF
Python meetup
Jeffrey Clark
 
PDF
Jwt with flask slide deck - alan swenson
Jeffrey Clark
 
PDF
Genericmeetupslides 110607190400-phpapp02
Jeffrey Clark
 
PDF
Pyramiddcpythonfeb2013 131006105131-phpapp02
Jeffrey Clark
 
PPTX
Dc python meetup
Jeffrey Clark
 
PDF
Tornado
Jeffrey Clark
 
PDF
The PSF and You
Jeffrey Clark
 
ODP
Using Grok to Walk Like a Duck - Brandon Craig Rhodes
Jeffrey Clark
 
PPT
What Makes A Great Dev Team - Mike Robinson
Jeffrey Clark
 
PPT
What Makes A Great Dev Team - Mike Robinson
Jeffrey Clark
 
PDF
Plone I18n Tutorial - Hanno Schlichting
Jeffrey Clark
 
PDF
State Of Zope 3 - Stephan Richter
Jeffrey Clark
 
PDF
KSS Techniques - Joel Burton
Jeffrey Clark
 
ZIP
Zenoss: Buildout
Jeffrey Clark
 
PDF
Opensourceweblion
Jeffrey Clark
 
PPT
Simplifying Plone
Jeffrey Clark
 
PDF
Bfg Ploneconf Oct2008
Jeffrey Clark
 
ODP
Kss Extjs
Jeffrey Clark
 
Python memory management_v2
Jeffrey Clark
 
Python meetup
Jeffrey Clark
 
Jwt with flask slide deck - alan swenson
Jeffrey Clark
 
Genericmeetupslides 110607190400-phpapp02
Jeffrey Clark
 
Pyramiddcpythonfeb2013 131006105131-phpapp02
Jeffrey Clark
 
Dc python meetup
Jeffrey Clark
 
Tornado
Jeffrey Clark
 
The PSF and You
Jeffrey Clark
 
Using Grok to Walk Like a Duck - Brandon Craig Rhodes
Jeffrey Clark
 
What Makes A Great Dev Team - Mike Robinson
Jeffrey Clark
 
What Makes A Great Dev Team - Mike Robinson
Jeffrey Clark
 
Plone I18n Tutorial - Hanno Schlichting
Jeffrey Clark
 
State Of Zope 3 - Stephan Richter
Jeffrey Clark
 
KSS Techniques - Joel Burton
Jeffrey Clark
 
Zenoss: Buildout
Jeffrey Clark
 
Opensourceweblion
Jeffrey Clark
 
Simplifying Plone
Jeffrey Clark
 
Bfg Ploneconf Oct2008
Jeffrey Clark
 
Kss Extjs
Jeffrey Clark
 

Zpugdccherry 101105081729-phpapp01

  • 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. SWIG Simplified Wrapper and Interface Generator www.swig.org
  • 3. Two Ways to Extend Python Write functions, classes, etc. in Python Write C/C++ Use Python C API Usually turn into shared library (dll)
  • 4. Why C/C++? Performance Do things that couldn’t otherwise be done Use already existing C/C++ code
  • 6. double foo(int n); #include <Python.h> PyObject *wrap_foo(PyObject *dummy, PyObject *args) { ... }
  • 7. double foo(int n); #include <Python.h> PyObject *wrap_foo(PyObject *dummy, PyObject *args) { PyObject *resultobj; int n; double result; PyArg_ParseTuple(args, “i&quot;, &n); result = foo(n); resultobj = PyFloat_FromDouble(result) return resultobj; }
  • 8. 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&quot;, &n, &m); result = bar(n, m); resultobj = PyInt_FromLong(result) return resultobj; }
  • 9. 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
  • 10. Shared Library C++ Code Python Code Compile, link SWIG Interface Files (point at C++ headers) SWIG
  • 11. 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); %}
  • 12. 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
  • 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. 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
  • 15. Scripting Languages E.g., Perl and Python “High-level” No (explicit) compilation necessary Usually lots of functionality included, and more available separately
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 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/