Boost.Python create extra functions not in wrapped object

Roman Yakovenko roman.yakovenko at gmail.com
Thu Apr 19 04:48:37 EDT 2007


On 4/19/07, Stou Sandalski <stou.sandalski at gmail.com> wrote:
>
> Thanks for the reply,
>
> That is what I am doing now, the problem is that I need the code to be
> in C/C++ inside the wrapper.  I am creating a lot of these objects and
> each time I am iterating over ~1.4 million points... and in python
> it's quite slow.... which is unpleasant especially since it's such a
> trivial operation.  My calculation's in NumPy take seconds, and to
> create an object for visualization I have to wait 2 minutes. =(


You can define function that first argument is a reference to the instance
of your class.
Then you can register it as usual function.
For example:
http://language-binding.net/pyplusplus/documentation/functions/transformation/built_in/input_c_buffer.html
take a look on generated source code( at the bottom of the page )


Thanks again,
>
> Stou
>
> On 4/19/07, Roman Yakovenko <roman.yakovenko at gmail.com> wrote:
> > On 19 Apr 2007 00:37:36 -0700, Stou Sandalski <stou.sandalski at gmail.com>
> > wrote:
> > > Hi,
> > >
> > > I have a python library created by wrapping the C++ library using
> > > Boost.Python, the problem is that the wrappers are not very
> > > pythonic.... so I want to add some methods that do not exist in the C+
> > > + implementation, that would create a better Python interface.
> > >
> > > For example to initialize the data in an object in the library one
> > > must iterate through every point, setting a value for each
> > > individually.  That's the way it works in C++ but in python it would
> > > be nice to instead just have one call that can receive a numpy array
> > > or a tuple. I want to add a call like: setData(array) to the python
> > > object, a call that does not exist in the C++ implementation and then
> > > in the C++ wrappers actually use setData to iterate through the array
> > > and set the values using the normal C++ method, say setValue(index,
> > > value).
> > >
> > > Something along the lines of this (initData is not in the constructor
> > > on purpose) C++ object:
> > >
> > > class Foo
> > > {
> > > public:
> > >      void initData(int size)
> > >      {
> > >         data = new float[size];
> > >      }; // Create the data array
> > >      void setValue(int index, float value) // Set given value
> > >      {
> > >          data[index] = value;
> > >      }
> > > private:
> > >      float *data;
> > > };
> > >
> > >
> > > In python however I want to do this:
> > >
> > > obj = foo()
> > > ar = array([1,2,3,4,5], dtype=float)
> > >
> > > foo.setData(ar)
> > >
> > > Or even better:
> > >
> > > ar = array([1,2,3,4,5], dtype=float)
> > > obj = foo(ar)
> > >
> > > And have it somehow call initData() and setValue() iteration inside
> > > the C++ code of the wrapper.  I've only used SWIG and don't really
> > > know much about Boost, I am not even sure how to label what I am
> > > trying to do.
> > >
> > > Can this be done with Boost, without changing the C++ library?
> >
> > Take a look on next link:
> >
> http://boost.org/libs/python/doc/tutorial/doc/html/python/techniques.html#python.extending_wrapped_objects_in_python
> >
> > > Regards,
> > >
> > > Stou
> > >
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> > >
> >
> >
> >
> > --
> > Roman Yakovenko
> >  C++ Python language binding
> > http://www.language-binding.net/
>



-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070419/8b2db095/attachment.html>


More information about the Python-list mailing list