Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new method to a Python Swig Template class

I need to add a new method to my swig template class, for example:

I am declaring a template class in myswig.i as follows:

%template(DoubleVector) vector<double>;

this will generate a class named "DoubleVector" in the generated .py file with some generated methods. lets suppose they are func1(), func2() and func3(). These are generated functions and i have no control over them. Now, if I want to add a new method called "func4()" to this class(DoubleVector), how may I do it? Is it possible?

I know of a identifier called %pythoncode but I could not use it to define a new function in this template class.

like image 833
Saurabh Avatar asked Jan 12 '12 14:01

Saurabh


People also ask

How do I use SWIG in Python?

The SWIG %module directive specifies the name of the Python module. If you specify `%module example', then everything is wrapped into a Python 'example' module. Underneath the covers, this module consists of a Python source file example.py and a low-level extension module _example.so.

Does Numpy use SWIG?

We use the SWIG %apply directive to apply the typemap for one-dimensional input arrays of type double to the actual prototype used by rms . Using numpy. i effectively, therefore, requires knowing what typemaps are available and what they do.

What is SWIG object Python?

The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.

Does SWIG work with C++?

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Javascript, Perl, PHP, Python, Tcl and Ruby.


1 Answers

Given an interface file like:

%module test

%{
#include <vector>
%}

%include "std_vector.i"
%template(DoubleVector) std::vector<double>;

The easiest way to add more functionality to DoubleVector is to write it in C++, in the SWIG interface file using %extend:

%extend std::vector<double> {
  void bar() {
    // don't for get to #include <iostream> where you include vector:
    std::cout << "Hello from bar" << std::endl;       
  }
}

this has the advantage that it would work for any language you target with SWIG, not just Python.

You can also do it using %pythoncode and an unbound function:

%pythoncode %{
def foo (self):
        print "Hello from foo"

DoubleVector.foo = foo
%}

Example running this:

Python 2.6.7 (r267:88850, Aug 11 2011, 12:16:10) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> d = test.DoubleVector()
>>> d.foo()
Hello from foo
>>> d.bar()
Hello from bar
>>> 
like image 107
Flexo Avatar answered Oct 22 '22 22:10

Flexo