473,698 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Boost.Python create extra functions not in wrapped object

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?

Regards,

Stou

Apr 19 '07 #1
0 1090

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3053
by: Steve Knight | last post by:
Hello, I'm new to Boost & Python and I'm diving straight in by trying to write an extension module to a third party library. Foolishness probably, but I don't have much choice! My question is how can I write a C++ method that will either return a boost wrapped C++ class or a Python object type depending on some internal value?
0
1762
by: Osiris | last post by:
My experiences with BOOST on Windows XP and Visual C++ 2005 I'm new to Python. I built software in more than ten other computer languages. I'm not sure if that is not a handicap, when reading documentation of a system like BOOST. However: It took me more than four full working days to get a Python/C/C++ 'hello world!' program to work. There is a lot of documentation, but somehow it did not 'click' with my previous experience. I think...
0
1353
by: willievdm | last post by:
Hi all I'm working with Boost.Python at the moment and have to do the following: -Using Boost, assign a char* in C++ to a PySwigObject (SWIG exposed object) of type uchar*, which is received in C++ as a boost::python::object class. The uchar* pointer in the PySwigObject points to a buffer containing image data. All I wish to do is assign a C++ pointer that is also
4
4235
by: Shawn McGrath | last post by:
Hi, I'm trying to expose a C++ class' internals to python via boost::python. I can do integer/boolean functions fine, but as soon as I do a string get/set it craps out. boost::python::class_<Entity, std::auto_ptr<pyEntity("Entity") //publics .def("isActive", &Entity::isActive) //bool .def("activate", &Entity::activate) //bool .def("deactivate", &Entity::deactivate) //bool //...
270
8063
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration inspired rant on my blog, and I thought it was relevant enough as a wider issue to the Python community to post here for your discussion and consideration. This is not flamebait. I love Python, and I'm not out to antagonise the community. I also...
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.