C++/Python list arguments

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

C++/Python list arguments

Michael Gauland
I've got a library written in C++ with a Python interface written using
boost::python, that I'm converting to use SWIG.

There is a factory function which currently accepts an integer and a
Python list. The list can be either a single list of numbers, or two
lists of numbers.  The current C++ code handles converting the second
argument from a list of python objects into a corresponding C++ structure.

I'd like some advice on how to do this using SWIG, keeping open the
option of supporting other bindings in the future.  Pointers to similar
examples would also be appreciated.

Kind Regards,

Mike



------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++/Python list arguments

Jakob van Bethlehem
Mike,

Unfortunately I can’t help you with the problem at hand - I’m interested to hear though why you are switching from boost::python to SWIG. Would you be willing to spend some words on it?

Sincerely,
Jakob


> On 26 Sep 2016, at 11:21, Michael Gauland <[hidden email]> wrote:
>
> I've got a library written in C++ with a Python interface written using
> boost::python, that I'm converting to use SWIG.
>
> There is a factory function which currently accepts an integer and a
> Python list. The list can be either a single list of numbers, or two
> lists of numbers.  The current C++ code handles converting the second
> argument from a list of python objects into a corresponding C++ structure.
>
> I'd like some advice on how to do this using SWIG, keeping open the
> option of supporting other bindings in the future.  Pointers to similar
> examples would also be appreciated.
>
> Kind Regards,
>
> Mike
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++/Python list arguments

Michael Gauland
First, I think SWIG will be easier to use for cross-platform builds.
I've had trouble getting boost::python to work, because it needs to be
built against the target python. I was able to get a simple SWIG example
building with very little effort.

Second, I think SWIG will reduce the amount of python-specific code I
need to write, and make it less work to support other language bindings.


On 27/09/16 08:00, Jakob van Bethlehem wrote:

> Mike,
>
> Unfortunately I can’t help you with the problem at hand - I’m interested to hear though why you are switching from boost::python to SWIG. Would you be willing to spend some words on it?
>
> Sincerely,
> Jakob
>
>
>> On 26 Sep 2016, at 11:21, Michael Gauland <[hidden email]> wrote:
>>
>> I've got a library written in C++ with a Python interface written using
>> boost::python, that I'm converting to use SWIG.
>>
>> There is a factory function which currently accepts an integer and a
>> Python list. The list can be either a single list of numbers, or two
>> lists of numbers.  The current C++ code handles converting the second
>> argument from a list of python objects into a corresponding C++ structure.
>>
>> I'd like some advice on how to do this using SWIG, keeping open the
>> option of supporting other bindings in the future.  Pointers to similar
>> examples would also be appreciated.
>>
>> Kind Regards,
>>
>> Mike
>>
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Swig-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++/Python list arguments

Bob Hood
If I can interject some pennies here...

When I needed to integrate Python into our product, I evaluated both SWIG and
boost::python.  The selling point for me was SWIG's Director feature. I needed
the Python scripts to behave like plug-ins to our product so they could be
first-class citizens along side actual C/C++ plug-ins, and SWIG's Director
featureactually made that a breeze.

Admittedly, my use case was rather specific (needed scripts to behave like
normal native-code plug-ins), but honestly, I've come to really enjoy all of
SWIG's integration mechanisms.  I don't regret the choice.

My $0.02US.


On 9/26/2016 3:11 PM, Michael Gauland wrote:

> First, I think SWIG will be easier to use for cross-platform builds.
> I've had trouble getting boost::python to work, because it needs to be
> built against the target python. I was able to get a simple SWIG example
> building with very little effort.
>
> Second, I think SWIG will reduce the amount of python-specific code I
> need to write, and make it less work to support other language bindings.
>
>
> On 27/09/16 08:00, Jakob van Bethlehem wrote:
>> Mike,
>>
>> Unfortunately I can’t help you with the problem at hand - I’m interested to hear though why you are switching from boost::python to SWIG. Would you be willing to spend some words on it?
>>
>> Sincerely,
>> Jakob
>>
>>
>>> On 26 Sep 2016, at 11:21, Michael Gauland <[hidden email]> wrote:
>>>
>>> I've got a library written in C++ with a Python interface written using
>>> boost::python, that I'm converting to use SWIG.
>>>
>>> There is a factory function which currently accepts an integer and a
>>> Python list. The list can be either a single list of numbers, or two
>>> lists of numbers.  The current C++ code handles converting the second
>>> argument from a list of python objects into a corresponding C++ structure.
>>>
>>> I'd like some advice on how to do this using SWIG, keeping open the
>>> option of supporting other bindings in the future.  Pointers to similar
>>> examples would also be appreciated.
>>>
>>> Kind Regards,
>>>
>>> Mike
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> _______________________________________________
>>> Swig-user mailing list
>>> [hidden email]
>>> https://lists.sourceforge.net/lists/listinfo/swig-user
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++/Python list arguments

William S Fulton
In reply to this post by Michael Gauland
On 26 September 2016 at 10:21, Michael Gauland <[hidden email]> wrote:

> I've got a library written in C++ with a Python interface written using
> boost::python, that I'm converting to use SWIG.
>
> There is a factory function which currently accepts an integer and a
> Python list. The list can be either a single list of numbers, or two
> lists of numbers.  The current C++ code handles converting the second
> argument from a list of python objects into a corresponding C++ structure.
>
> I'd like some advice on how to do this using SWIG, keeping open the
> option of supporting other bindings in the future.  Pointers to similar
> examples would also be appreciated.

So if I have understood you correctly you have a signature something
like this at the moment:

  X* makeX(int some_number, PyObject *python_list_or_lists);

Overloading should work with different signature/types:

  %include <std_vector.i>
  %typemap(VectorDouble) std::vector<double>;
  X* makeX(int some_number, const std::vector& list1);
  X* makeX(int some_number, const std::vector<double>& list1, const
std::vector<double>& list2);

You should then be able to call this code from python like so:

  x = makeX(10, [1, 2, 3.3])
or
  x = makeX(10, [1, 2, 3.3], [9.9, 8, 7])

std::vector is supported by all the languages, so would be a good
choice and you wouldn't need to change any of the above to support
other languages. You can also use C arrays, but as you are not tied to
using C, then the STL will be easier.

William

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++/Python list arguments

Michael Gauland

On 27/09/16 10:35, William S Fulton wrote:

> On 26 September 2016 at 10:21, Michael Gauland <[hidden email]> wrote:
>> I've got a library written in C++ with a Python interface written using
>> boost::python, that I'm converting to use SWIG.
>>
>> There is a factory function which currently accepts an integer and a
>> Python list. The list can be either a single list of numbers, or two
>> lists of numbers.  The current C++ code handles converting the second
>> argument from a list of python objects into a corresponding C++ structure.
>>
>> I'd like some advice on how to do this using SWIG, keeping open the
>> option of supporting other bindings in the future.  Pointers to similar
>> examples would also be appreciated.
> So if I have understood you correctly you have a signature something
> like this at the moment:
>
>   X* makeX(int some_number, PyObject *python_list_or_lists);
>
> Overloading should work with different signature/types:
>
>   %include <std_vector.i>
>   %typemap(VectorDouble) std::vector<double>;
>   X* makeX(int some_number, const std::vector& list1);
>   X* makeX(int some_number, const std::vector<double>& list1, const
> std::vector<double>& list2);
>
> You should then be able to call this code from python like so:
>
>   x = makeX(10, [1, 2, 3.3])
> or
>   x = makeX(10, [1, 2, 3.3], [9.9, 8, 7])
>
> std::vector is supported by all the languages, so would be a good
> choice and you wouldn't need to change any of the above to support
> other languages. You can also use C arrays, but as you are not tied to
> using C, then the STL will be easier.
>
> William
You understand perfectly, and your advice has been very
helpful--definitely feel I'm on the right track now!

Thank you very much.

--Mike


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: C++/Python list arguments

Jakob van Bethlehem
In reply to this post by Michael Gauland
Mike,

Unfortunately I can’t help you with the problem at hand - I’m interested to hear though why you are switching from boost::python to SWIG. Would you be willing to spend some words on it?

Sincerely,
Jakob

On 26 Sep 2016, at 11:21, Michael Gauland <[hidden email]> wrote:

I've got a library written in C++ with a Python interface written using
boost::python, that I'm converting to use SWIG.

There is a factory function which currently accepts an integer and a
Python list. The list can be either a single list of numbers, or two
lists of numbers.  The current C++ code handles converting the second
argument from a list of python objects into a corresponding C++ structure.

I'd like some advice on how to do this using SWIG, keeping open the
option of supporting other bindings in the future.  Pointers to similar
examples would also be appreciated.

Kind Regards,

Mike


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...