473,721 Members | 2,220 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 1092

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

Similar topics

2
3055
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
1765
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
4240
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
8112
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
8840
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
8730
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
9367
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
9215
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...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
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.