我有带有签名的函数:
function(std::vector<double> vector);
而且我已经暴露,但它并没有在Python列表服用。 我已经通过其他的SO答案看,大部分涉及到更改功能采取的boost ::蟒蛇::名单,但我不想改变功能。 我想我可以使用vector_indexing_suite写解决此功能的简单包装,但我有这种形式的许多功能,宁可不写每一个的包装。 有没有一种方法可以自动使一个Python列表 - >的std ::矢量绘图发生?
我有带有签名的函数:
function(std::vector<double> vector);
而且我已经暴露,但它并没有在Python列表服用。 我已经通过其他的SO答案看,大部分涉及到更改功能采取的boost ::蟒蛇::名单,但我不想改变功能。 我想我可以使用vector_indexing_suite写解决此功能的简单包装,但我有这种形式的许多功能,宁可不写每一个的包装。 有没有一种方法可以自动使一个Python列表 - >的std ::矢量绘图发生?
有几个解决方案,以实现这一目标,而无需修改原来的功能。
要使用Python的少量样板代码和透明度做到这一点,可以考虑注册一个自定义的converter
。 C ++和Python类型之间正在进行时Boost.Python使用注册转换器。 创建绑定,当如当某些转换器隐式创建class_
导出一个类型。
下面完整的示例使用iterable_converter
类型,允许的转换函数从一个python型支撑登记蟒迭代协议 。 这个例子启用转换:
std::vector<double>
std::vector<std::vector<std::String> >
std::list<foo>
#include <iostream>
#include <list>
#include <vector>
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>
/// @brief Mockup model.
class foo {};
// Test functions demonstrating capabilities.
void test1(std::vector<double> values)
{
for (auto&& value: values)
std::cout << value << std::endl;
}
void test2(std::vector<std::vector<std::string> > values)
{
for (auto&& inner: values)
for (auto&& value: inner)
std::cout << value << std::endl;
}
void test3(std::list<foo> values)
{
std::cout << values.size() << std::endl;
}
/// @brief Type that allows for registration of conversions from
/// python iterable types.
struct iterable_converter
{
/// @note Registers converter from a python interable type to the
/// provided type.
template <typename Container>
iterable_converter&
from_python()
{
boost::python::converter::registry::push_back(
&iterable_converter::convertible,
&iterable_converter::construct<Container>,
boost::python::type_id<Container>());
// Support chaining.
return *this;
}
/// @brief Check if PyObject is iterable.
static void* convertible(PyObject* object)
{
return PyObject_GetIter(object) ? object : NULL;
}
/// @brief Convert iterable PyObject to C++ container type.
///
/// Container Concept requirements:
///
/// * Container::value_type is CopyConstructable.
/// * Container can be constructed and populated with two iterators.
/// I.e. Container(begin, end)
template <typename Container>
static void construct(
PyObject* object,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
namespace python = boost::python;
// Object is a borrowed reference, so create a handle indicting it is
// borrowed for proper reference counting.
python::handle<> handle(python::borrowed(object));
// Obtain a handle to the memory block that the converter has allocated
// for the C++ type.
typedef python::converter::rvalue_from_python_storage<Container>
storage_type;
void* storage = reinterpret_cast<storage_type*>(data)->storage.bytes;
typedef python::stl_input_iterator<typename Container::value_type>
iterator;
// Allocate the C++ type into the converter's memory block, and assign
// its handle to the converter's convertible variable. The C++
// container is populated by passing the begin and end iterators of
// the python object to the container's constructor.
new (storage) Container(
iterator(python::object(handle)), // begin
iterator()); // end
data->convertible = storage;
}
};
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
// Register interable conversions.
iterable_converter()
// Build-in type.
.from_python<std::vector<double> >()
// Each dimension needs to be convertable.
.from_python<std::vector<std::string> >()
.from_python<std::vector<std::vector<std::string> > >()
// User type.
.from_python<std::list<foo> >()
;
python::class_<foo>("Foo");
python::def("test1", &test1);
python::def("test2", &test2);
python::def("test3", &test3);
}
互动用法:
>>> import example
>>> example.test1([1, 2, 3])
1
2
3
>>> example.test1((4, 5, 6))
4
5
6
>>> example.test2([
... ['a', 'b', 'c'],
... ['d', 'e', 'f']
... ])
a
b
c
d
e
f
>>> example.test3([example.Foo(), example.Foo()])
2
这种方法的几点意见:
iterable_converter::convertible
功能可以更改为只允许Python列表,而不是让支持迭代协议的类型。 然而,扩展可能成为导致轻微unpythonic。 example
扩展名称空间。 替代的方法包括:
boost::python::list
为每个函数接受std::vector
。 这种方法会导致按比例的基础上的函数的量的绑定被导出,而不是类型转换·需要的量。 使用Boost.Python的vector_indexing_suite
。 在*_indexing_suite
类导出适于匹配Python列表或字典的一些语义的类型。 因此,Python代码现在必须知道确切的容器类型来提供,产生,Python化较少扩展。 例如,如果std::vector<double>
导出为VecDouble
,然后将所得的Python用法是:
v = example.VecDouble() v[:] = [1, 2, 3] example.test1(v)
但是,以下将不起作用,因为确切的类型必须匹配,作为导出类只注册之间的变换VecDouble
和std::vector<double>
:
example.test1([4, 5, 6])
虽然这种方法可扩展到类型,而不是功能,它导致不太pythonic的延伸和涨大的example
不必要类型命名空间。