[C++-sig] a new version of pybindgen with full STL containers method support!!!

Taner Yildirim taner at seas.upenn.edu
Wed Jun 10 07:49:08 CEST 2009


Dear Gustavo and Mike,

 

When you have a chance, please have a look at the website:

 

http://www.ncnr.nist.gov/staff/taner/pybindgen/doc_installation_features.htm
l

 

Implementing my toy std::vector class to a generic type in pybindgen code
turned out to be quite easy! All I had to do is to write a function in
module.py which basically does the same thing as add_container but instead
returning a container, it also create a std::vector class, add the methods
to it and then return the class back to user. In this way, without messing
up with the container module or any other pybindgen modules, I was able to
combine the container and the stl:vector class with an arbitrary
user-defined type!!! Since we now get a class when the container is added,
the user can add additional functions as method as well. In the two examples
given in the website, I demonstrated this adding sum() and operator== for a
complex type for the std:vector.

 

The usage is almost the same as before but instead using add_container, now
we have to use stl_add_container. The rest is automatic. Also, thanks to
__call__, we can access the vector elements as  vec(index). vec() gives the
iterable container. Hence list(Vec()) works for python list.

 

Anyway, I created a brief website to explain my approach. I discuss two
examples in details. The first one is regular std::vector with double and
integer types. The other is std:vector of a custom type!  Making this code
work for other container would be trivial too. But first, I would like to
get some feedback from you guys to see if you like the approach. If you are
interested, you can get the modified module.py (which is the same as
original but with a new function add_stl_container) at my website.

 

Please have a look at the web site:

 

http://www.ncnr.nist.gov/staff/taner/pybindgen/

 

Best regards

Taner

 

PS: I created the website very quickly using Sphinx. 

Sorry for the typos or strange behaviors with the web design. I'll fix it
later.

 

****************************************************************************
****
Dr. Taner Yildirim, Ph.D. Physicist, 
Adjunct Associate Professor,
Materials Science and Engineering,
University of Pennsylvania
and 
Computational and Neutron Science Team Leader,
NIST Center for Neutron Research, 
National Institute of Standards and Technology, 
100 Bureau Drive, Gaithersburg, MD 20899-6100. 
Email: taner at nist.gov or taner at seas.upenn.edu 
PHONE: 301-975-6228 FAX : 301-921-9847 
Web : http://webster.ncnr.nist.gov/staff/taner 
****************************************************************************
****

  _____  

From: Gustavo Carneiro [mailto:gjcarneiro at gmail.com] 
Sent: Sunday, June 07, 2009 7:24 PM
To: taner at seas.upenn.edu
Cc: J. Michael Owen; cplusplus-sig at python.org
Subject: Re: [C++-sig] pybindgen: Why do wrapped C++ containers not look
more like python containers?

 

 

2009/6/7 Taner Yildirim <taner at seas.upenn.edu>

Dear Gustavo,

 

Thanks for the detailed explanation, which fixed the problem!. As I
indicated in my previous email, I am mainly a Fortran programmer and do not
have much experience with c++. I used to wrap my programs with F2Py but then
decided to do right thing and learn c/c++ and rewrite some of the
applications.  Thanks to your pybindgen code, the wrapping them for python
would be the easiest part!! 

 

Back to pybindgen containers, I noticed a strange behavior. I did not check
the bug reports. Sorry if this was already reported.

I have the following code:

 

        mod=Module('tst1')

        mod.add_include('"tst1.h"')

        mod.add_container('std::vector<int>', 'int',
'vector',custom_name="std_vec_int")

 

In python, I do this:

 

A=std_vec_int([1,2,3])

B=std_vec_int([1,2,3])

 

But then

A==B  returns false???

 

Similarly, I do c=std_vec_int(a) and then check a==c which returns false!!!


Type comparison, like many other things, is not yet implemented in
pybindgen.

Like I said, you can do a lot of things by simply "casting" in python to a
list, i.e. instead of:

  A=std_vec_int([1,2,3])

do:

  A=list(std_vec_int([1,2,3]))

It's an ugly workaround but it works ok (although the performance isn't
great). 

 

If I do list(A) and list(B), I get the right list. It looks like "==" not
working or I am again missing something very trivial!

You are not missing anything, it's just not implemented.  Unfortunately
Python 2.x has some default comparison function; this is fixed in Python
3.x, which just raises an exception if the comparison operation is not
implemented.
 

By the way, this observation is based on pybindgen-0.10.0 and mingw (gcc
4.4.0).

 

Best regards

Taner

 

****************************************************************************
****
Dr. Taner Yildirim, Ph.D. Physicist, 
Adjunct Associate Professor,
Materials Science and Engineering,
University of Pennsylvania
and 
Computational and Neutron Science Team Leader,
NIST Center for Neutron Research, 
National Institute of Standards and Technology, 
100 Bureau Drive, Gaithersburg, MD 20899-6100. 
Email: taner at nist.gov or taner at seas.upenn.edu 
PHONE: 301-975-6228 FAX : 301-921-9847 
Web : http://webster.ncnr.nist.gov/staff/taner 
****************************************************************************
****

 




-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090610/48b1ee68/attachment-0001.htm>


More information about the Cplusplus-sig mailing list