Re: [C++-sig] automatic conversion of tuple to vector

2010-05-25 Thread Michele De Stefano
Nathan,

if you want to take a boost::numeric::ublas::vector instead of
std::vector, you can use my library (mds-utils, the link is at the end
of this e-mail), which already provides the proper converters.

Bye,
Michele

2010/5/25 Jim Bosch :
> On 05/24/2010 03:00 PM, Nathan Cournia wrote:
>>
>> Basically, the desired behavior is to have a C++ function which
>> accepts a vector in C++; accept a tuple/list when in Python.
>>
>> I'm not sure if the FAQ is up-to-date.  Is this still not possible in
>> boost.python?  How do people generally get the behavior I outlined
>> above?
>>
>>
>
> In general, you'll have to write your own custom wrapper that takes a
> boost::python::object argument, and wrap that, to get this behavior:
>
>
> void py_foo(boost::python::object const & py_array) {
>    std::vector array;
>    // 
>    foo(array);
>    // 
> }
>
> boost::python::def("foo", &py_foo);
>
>
> The copies are unavoidable if your C++ function takes a container, rather
> than a templated iterator range, simply because your Python object doesn't
> actually contain a std::vector.  If you can work with an iterator range,
> check out my from_python_iterator class, part of the Python extensions in
> the boost sandbox:
>
> https://svn.boost.org/trac/boost/browser/sandbox/python_extensions/boost/python/from_python/iterator.hpp
>
> Along with the to-python iterator found in the main Boost.Python
> distribution, it should also help in the case where you have to make copies.
>
> HTH
>
> Jim Bosch
>
>
> ___
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://code.google.com/p/mds-utils
http://xoomer.virgilio.it/michele_de_stefano
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] No to_python (by-value) converter found for C++ type

2010-05-25 Thread Roman Yakovenko
On Tue, May 25, 2010 at 9:58 AM, abhi.sri45
 wrote:
>
> Hi,
>
>>mb = module_builder_t( ... )
>>mb.class_(...).noncopyable = False
>
> This does not works in my case.
>
> Example code is:
>
> enum CallStatus
> {
>        SUCCESS = 423,
>        FAILURE = 764
> };
>
> template
> struct CallResult
> {
> public:
>        CallResult(CallStatus const callStatus)
>        : status(callStatus)
>        {
>        }
>
>        CallResult(CallStatus const callStatus, Result const & result)
>        : status(callStatus), result(result)
>        {
>        }
>        /*
>        CallResult(CallResult const& callResult)
>        {
>                status = callResult.status;
>                result = callResult.result;
>        }
>        */
>        CallStatus const status;
>        Result const result;
>     //   boost::optional const result;
> };


May be I miss something, but this class has "const member variable"
and doesn't have user define copy constructor ( it is commented out ).
In this case the class indeed noncopyable.

Am I wrong?


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] How to wrap reference to abstract base class to get C++ polymorphism

2010-05-25 Thread Kun Hong

On 20/05/2010, at 11:03 PM, Kun Hong wrote:


Hi,

I have a maybe entry-level question. But it really troubled me for a
while.

I need to wrap a C++ library which has some factory method to return
references to some abstract base classes. So the implementation is
hidden. If I want a python equivalent to the abstract base class
reference type
(so the polymorphism can work), how should I do it?

Please see a very simplified example below which demonstrates what I
mean.



Finally, I get to understand it and be able to answer my own question.
Post my own answer so future reader can get a pointer directly.

The answer is in the test code that comes with boost python library.
The code is in libs/python/test/polymorphism.{cpp,py}

I got my code working like below:

== test.cpp ==


#include 

using namespace boost::python;


class B
{
public:
virtual const char *getName() const = 0;

virtual int getValue() const
{
return 10;
}
};

class C : public B
{
public:
virtual const char *getName() const
{
return "Class C";
}

};

struct BWrap : public B
{
BWrap(PyObject *self) : mSelf(self) {}
PyObject *mSelf;

const char *getName() const
{
return call_method(mSelf, "getName");
}
};

const B &getB()
{
static boost::shared_ptr b(new C);

return (*b.get());
}


BOOST_PYTHON_MODULE(Test)
{

class_ ("B", no_init)
.def("getName", pure_virtual(&B::getName))
.def("getValue", &B::getValue)
;

class_ >("C")
.def("getName", &C::getName)
;

def("getB", &getB,  
return_value_policy());

}

 test.py =
from Test import *

print getB().getName()
print getB().getValue()
c = C()
print c.getName()
print c.getValue()
#b = B()
#print b.getName()
#print b.getValue()

=== end

Not fully understand the pure_virtual call though, which doesn't  
prevent B
instance be created in python. "no_init" is needed for preventing B  
creation.


But, fun learning boost python, a lot to learn.

Kun
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] automatic conversion of tuple to vector

2010-05-25 Thread Jim Bosch

Hope I'm not thread-hijacking too much here, but this piqued my curiosity:

On 05/25/2010 12:06 AM, Michele De Stefano wrote:

Nathan,

if you want to take a boost::numeric::ublas::vector instead of
std::vector, you can use my library (mds-utils, the link is at the end
of this e-mail), which already provides the proper converters.

   


Do you handle the non-const reference case automatically?  More 
precisely, can you do:


void func(boost::numeric::ublas::vector & arg);
boost::python::def("func", func);

...pass it a python list, and find that list modified after the call?

I've been stumped by this problem in boost python before, and I had 
previously come the conclusion that it was impossible, so I'd be very 
interested to hear it if you have a solution.



Jim Bosch
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] automatic conversion of tuple to vector

2010-05-25 Thread Michele De Stefano
Jim,

actually, this IS impossible, for a limit of Boost.Python. The
explanation is into the FAQs of the Boost.Python documentation.

Basically it is not possible to develop an l-value converter, so it's
not possible to solve this problem. The author of Boost.Python
explains that for solving this problem a complete review of the
library is needed (and I hope he will do it so that in the future that
feature will be available).

Bye,
Michele

2010/5/25 Jim Bosch :
> Hope I'm not thread-hijacking too much here, but this piqued my curiosity:
>
> On 05/25/2010 12:06 AM, Michele De Stefano wrote:
>>
>> Nathan,
>>
>> if you want to take a boost::numeric::ublas::vector instead of
>> std::vector, you can use my library (mds-utils, the link is at the end
>> of this e-mail), which already provides the proper converters.
>>
>>
>
> Do you handle the non-const reference case automatically?  More precisely,
> can you do:
>
> void func(boost::numeric::ublas::vector & arg);
> boost::python::def("func", func);
>
> ...pass it a python list, and find that list modified after the call?
>
> I've been stumped by this problem in boost python before, and I had
> previously come the conclusion that it was impossible, so I'd be very
> interested to hear it if you have a solution.
>
>
> Jim Bosch
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://code.google.com/p/mds-utils
http://xoomer.virgilio.it/michele_de_stefano
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] No to_python (by-value) converter found for C++ type

2010-05-25 Thread abhi.sri45

Hi,

> In this case the class indeed noncopyable.

I could not get this because, in my example program which I send earlier,
there is a function
CallResult getCallResult()
{
Simple si;
return CallResult(SUCCESS, si);
} 

Temporary object is created in return statement and hence it calls the
default copy constructor successfully.
You can verify this by compiling this.

main()
{
CallResult obj = getCallResult();
std::cout << obj.status << std::endl;

}
 
There may be some problem while assignment but copy will work properly.
Correct me if there is some gap in my understanding.

Is there any way to disable this noncopyable because
mb.class_(...).noncopyable = False  doesn't worked in my case.

With Regards,
Abhishek Srivastava 
 

-- 
View this message in context: 
http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28665970.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.

___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] How to expose to a python user defined type with to/from_python convertors?

2010-05-25 Thread X Wang

> How do I expose my C++ type to a user
> defined python type? I think I could use boost python
> class_<> wrapper to wrap around, but if I want to use
> to_python/from_python convertors?
> 
> Say I want to expose my C++ class(CA) to a python
> type(PA):
> 
> C++:
> class CA
> {
>   public:
>     //ctors, accessors...
>   private:
>     std::string name_;
>     int value_;
> };
> 
> Python:
> class PA(object):
>   def __init__(self, name, value):
>     self.name=name
>     self.value=value
> 
> If I use,
> 
> void initializeConverters()
> {
>    using namespace boost::python;
>    to_python_converter CA_to_pA>();
> }
> 
> with
> 
> struct CA_to_PA
> {
>       static PyObject* convert(CA const&
> s)
>       {
>          printf("In C++ ->
> python converter?\n");
> 
>          std::string
> name=s.name();
>          int
> value=s.value();
> 
> #if 0
>          static
> boost::python::object obj;
>          PyObject*
> obj_ptr=obj.ptr();
> 
>      
>    PyObject_SetAttrString(obj_ptr, "name", 
>                
> PyString_FromString(name.c_str()));
>      
>    PyObject_SetAttrString(obj_ptr, "value", 
>                
> PyInt_FromLong(value));
> 
>          return
> boost::python::incref(obj_ptr);
> #else
>          return
> boost::python::incref(
>            
> //PyInt_FromLong(1999)
>            
> Py_BuildValue("s", "native string works")
>            
> //Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456)
>             );
> #endif
>       }
> };
> 
> How do I create a boost::python::object (or PyObject*) with
> "name" and "value" attributes? I could get creation and
> return of python native types to work, but can not get user
> defined types returned to the python side -- always get
> NoneType returned.
> 
> boost::python::object C2Py()
> {
>    printf("Pre assignment\n");
> 
> #if 0
>    boost::python::object obj(new CA("From
> C++ to python", 987654321));
>    PyObject* o=obj.ptr();
> #else
>    CA a("From C++ to python", 987654321);
>    PyObject *o=CA_to_PA::convert(a);
> #endif
> 
>    printf("Post assignment\n");
> 
>    PyObject *tmp=NULL;
> 
>    std::string name="No Name";
>    int value=-1;
> 
>    tmp=PyObject_GetAttrString(o, "name");
>    if (tmp!=NULL)
> name=PyString_AsString(tmp);
> 
>    tmp=PyObject_GetAttrString(o, "value");
>    if (tmp!=NULL) value=PyInt_AsLong(tmp);
> 
>    printf("(%s, %d)\n", name.c_str(),
> value);
> 
>    return obj;
> }
> 
> $ python
> Python 2.5.1 (r251:54863, Oct  5 2007, 11:16:46)
> [GCC 4.1.1 20070105 (Red Hat 4.1.1-53)] on linux2
> Type "help", "copyright", "credits" or "license" for more
> information.
> >>> import mytypes
> >>> pa=mytypes.C2Py()
> ("No Name", -1)
> >>>type(pa)
> NoneType
> 
> Any help is appreciated, thanks!
> 
Any comments? Am I asking the right question? Basically I have a simple C++ 
type and want to extend it to python. However, I haven't figured out a way to 
do the C++ to python conversion with boost python's to_python/from_python 
converter scheme. Am I missing something?


  
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] How to expose to a python user defined type with to/from_python convertors?

2010-05-25 Thread Michele De Stefano
There's a very good tutorial on writing from/to python converters.
This tutorial was suggested to me by one of the members of this
mailing list and I've found it very useful.

The link is this one:
http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/

After reading this very useful article, I managed to write my
mds-utils library (the link is at the end of this e-mail) which you
can download to see how you can write a from/to python converter.

Bye.
Michele

2010/5/25 X Wang :
>
>> How do I expose my C++ type to a user
>> defined python type? I think I could use boost python
>> class_<> wrapper to wrap around, but if I want to use
>> to_python/from_python convertors?
>>
>> Say I want to expose my C++ class(CA) to a python
>> type(PA):
>>
>> C++:
>> class CA
>> {
>>   public:
>>     //ctors, accessors...
>>   private:
>>     std::string name_;
>>     int value_;
>> };
>>
>> Python:
>> class PA(object):
>>   def __init__(self, name, value):
>>     self.name=name
>>     self.value=value
>>
>> If I use,
>>
>> void initializeConverters()
>> {
>>    using namespace boost::python;
>>    to_python_converter> CA_to_pA>();
>> }
>>
>> with
>>
>> struct CA_to_PA
>> {
>>       static PyObject* convert(CA const&
>> s)
>>       {
>>          printf("In C++ ->
>> python converter?\n");
>>
>>          std::string
>> name=s.name();
>>          int
>> value=s.value();
>>
>> #if 0
>>          static
>> boost::python::object obj;
>>          PyObject*
>> obj_ptr=obj.ptr();
>>
>>
>>    PyObject_SetAttrString(obj_ptr, "name",
>>
>> PyString_FromString(name.c_str()));
>>
>>    PyObject_SetAttrString(obj_ptr, "value",
>>
>> PyInt_FromLong(value));
>>
>>          return
>> boost::python::incref(obj_ptr);
>> #else
>>          return
>> boost::python::incref(
>>
>> //PyInt_FromLong(1999)
>>
>> Py_BuildValue("s", "native string works")
>>
>> //Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456)
>>             );
>> #endif
>>       }
>> };
>>
>> How do I create a boost::python::object (or PyObject*) with
>> "name" and "value" attributes? I could get creation and
>> return of python native types to work, but can not get user
>> defined types returned to the python side -- always get
>> NoneType returned.
>>
>> boost::python::object C2Py()
>> {
>>    printf("Pre assignment\n");
>>
>> #if 0
>>    boost::python::object obj(new CA("From
>> C++ to python", 987654321));
>>    PyObject* o=obj.ptr();
>> #else
>>    CA a("From C++ to python", 987654321);
>>    PyObject *o=CA_to_PA::convert(a);
>> #endif
>>
>>    printf("Post assignment\n");
>>
>>    PyObject *tmp=NULL;
>>
>>    std::string name="No Name";
>>    int value=-1;
>>
>>    tmp=PyObject_GetAttrString(o, "name");
>>    if (tmp!=NULL)
>> name=PyString_AsString(tmp);
>>
>>    tmp=PyObject_GetAttrString(o, "value");
>>    if (tmp!=NULL) value=PyInt_AsLong(tmp);
>>
>>    printf("(%s, %d)\n", name.c_str(),
>> value);
>>
>>    return obj;
>> }
>>
>> $ python
>> Python 2.5.1 (r251:54863, Oct  5 2007, 11:16:46)
>> [GCC 4.1.1 20070105 (Red Hat 4.1.1-53)] on linux2
>> Type "help", "copyright", "credits" or "license" for more
>> information.
>> >>> import mytypes
>> >>> pa=mytypes.C2Py()
>> ("No Name", -1)
>> >>>type(pa)
>> NoneType
>>
>> Any help is appreciated, thanks!
>>
> Any comments? Am I asking the right question? Basically I have a simple C++ 
> type and want to extend it to python. However, I haven't figured out a way to 
> do the C++ to python conversion with boost python's to_python/from_python 
> converter scheme. Am I missing something?
>
>
>
> ___
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://code.google.com/p/mds-utils
http://xoomer.virgilio.it/michele_de_stefano
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Boost.Python Problem

2010-05-25 Thread Jim Bosch

On 05/18/2010 12:44 PM, alexandre monti wrote:

object main_module((handle<>(borrowed(PyImport_AddModule("__main__");
object main_namespace = main_module.attr("__dict__");


What's the context?  At least on my Ubuntu system, the following code:

#include 

using namespace boost::python;

BOOST_PYTHON_MODULE(example) {
object 
main_module((handle<>(borrowed(PyImport_AddModule("__main__");

object main_namespace = main_module.attr("__dict__");
}

...compiles and imports just fine.


Jim Bosch

___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig