我有一个C++类,它有以下方法:
class Bar {
...
const Foo& getFoo() const;
void setFoo(const Foo&);
};
Run Code Online (Sandbox Code Playgroud)
class Foo可以转换为std::string(它有一个隐式构造函数std::string和一个std::string强制转换运算符).
我定义了一个Boost.Python包装类,除其他外,它基于前两个函数定义一个属性:
class_<Bar>("Bar")
...
.add_property(
"foo",
make_function(
&Bar::getFoo,
return_value_policy<return_by_value>()),
&Bar::setFoo)
...
Run Code Online (Sandbox Code Playgroud)
我还将该类标记为可转换为/来自std::string.
implicitly_convertible<std::string, Foo>();
implicitly_convertible<Foo, std::string>();
Run Code Online (Sandbox Code Playgroud)
但在运行时,我仍然遇到尝试访问此属性的转换错误:
TypeError: No to_python (by-value) converter found for C++ type: Foo
Run Code Online (Sandbox Code Playgroud)
如何在没有太多包装函数的样板的情况下实现转换?(我已经在课堂上拥有所有转换功能Foo,因此不希望重复.
是否可以使用Boost.Python来暴露(使用cPickle)枚举的枚举?我已经使用这里描述的第一种方法成功地腌制了其他对象,但是这些方法似乎都没有适用于枚举类型,并且默认情况下这些对象似乎不是可拾取的.
我有一个C++函数,它返回一个结构列表.在struct中,有更多的结构列表.
struct CameraInfo {
CamName name;
std::list<CamImageFormat> lImgFormats;
std::list<CamControls> lCamControls;
};
std::list<CameraInfo> getCameraInfo()
{
std::list<CameraInfo> lCamerasInfo;
// fill lCamerasInfo
return lCamerasInfo;
}
Run Code Online (Sandbox Code Playgroud)
然后出口我我用的是:
class_<CameraNode....> >("CameraNode", no_init)
...
...
.def("listCameraInfo", make_function(&CameraNode::listCameraInfo))
.staticmethod("listCameraInfo")
...
;
Run Code Online (Sandbox Code Playgroud)
这是好的,因为我使用cout在屏幕上打印数据...我想现在使用返回值和它的内容来自python类属性,这样:
cameras = []
cameras = CameraNode.getCameraInfo()
print cameras[0].name
print cameras[0].lImgFormats[0]
and so on...
Run Code Online (Sandbox Code Playgroud)
这有可能吗?我应该使用add_property吗?我不认为我可以为每个结构创建一个类.这个设计在我只使用C++时很有意义,但是现在我必须把它包起来,我越来越困惑了.
有关以一般方式使用boost.python包装std :: list的任何建议都将被广泛接受.
我将在这里添加我发现有用的链接: 迭代器 StlContainers
我正在尝试使用boost python来使用自定义python库.我有一个python源代码并使用以下命令构建boost.python:
./bootstrap.sh --with-python-root=../Python-2.7.2 --with-libraries=python
然后 ./b2
但是当我尝试在我的应用程序中使用boost.python时,我得到了
Fatal Python error: Interpreter not initialized (version mismatch?)
当我打电话时PyRun_SimpleString("import sys\nprint sys.version");,我得到2.7.2,正如我所期望的那样(和python的版本一起构建boost.python而不是系统版本.)
有什么我想念的吗?
当我检查dylib链接的库时,我得到:
libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.1)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)`
Run Code Online (Sandbox Code Playgroud)
在我的Xcode目标中,我包含了--with-python-root参数文件夹中的python和boost/stage/lib的内容,

我在C++中有以下代码:
class Person
{
public:
enum Gender {Male, Female};
Gender GetGender() const;
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式将它包装在boost :: python中:
BOOST_PYTHON_MODULE(TestPython)
{
scope the_scope = class_<Person>("Person")
.def("GetGender", &Person::GetGender);
enum_<Person::Gender>("Gender")
.value(Male, Person::Male)
.value(Female, Person::Female)
.export_values();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试person.GetGender()从Python 调用时,我得到以下异常:
Can't pickle : attribute lookup **PyBF.TestPython.Gender**. It guesses the namespace of the Gender (which is actually **PyBF.TestPython.Person.Gender**) enum return type incorrectly.
如何告诉GetGender函数显式返回什么类型?
我有一个Event用C++定义的类,我使用Boost向Python公开.我的脚本应该派生自这个类,并且我想在定义新的子类时进行一些初始化.
如何设置公开Event类的元类,以便每当Python脚本派生自此类时,元类都可以执行所需的初始化?
我想避免在脚本中明确使用元类...
class KeyboardEvent(Event): # This is what I want
pass
class KeyboardEvent(Event, metaclass=EventMeta): # This is not a good solution
pass
Run Code Online (Sandbox Code Playgroud)
编辑: 解决方案的一部分
似乎没有办法用Boost.Python设置元类.接下来最好的事情是在定义类之后即兴创建和更改元类.在本机Python中,更改元类的安全方法是执行此操作:
B = MetaClass(B.__name__, B.__bases__, B.__dict__)
Run Code Online (Sandbox Code Playgroud)
在Boost中,它看起来像这样:
BOOST_PYTHON_MODULE(event)
{
using namespace boost::python;
using boost::python::objects::add_to_namespace;
class_<EventMetaClass> eventmeta("__EventMetaClass")
...;
class_<Event> event("Event")
...;
add_to_namespace(scope(), "Event",
eventmeta(event["__name__"], event["__bases__"], event["__dict__"]));
}
Run Code Online (Sandbox Code Playgroud)
问题是我似乎找不到使用Boost.Python定义元类的方法,这就是为什么我打开了如何使用Boost.Python定义Python元类的原因?.
我正在将(大多数)浮动列表传递给boost python中的模块,某些元素是None对象。在C ++代码中,我像这样提取浮点数:
for(i=0;i<boost::python::len(list) - window_len + 1;i++) {
double value = boost::python::extract<double>(list[i]);
}
Run Code Online (Sandbox Code Playgroud)
当list [i]指向python None对象时,这显然是有问题的。因此,我写了这样的东西:
for(i=0;i<boost::python::len(list) - window_len + 1;i++) {
if(list[i] == NULL) continue;
double value = boost::python::extract<double>(list[i]);
}
Run Code Online (Sandbox Code Playgroud)
和
for(i=0;i<boost::python::len(list) - window_len + 1;i++) {
if(list[i] == boost::python::api::object()) continue;
double value = boost::python::extract<double>(list[i]);
}
Run Code Online (Sandbox Code Playgroud)
因为显然boost :: python :: api :: object()的计算结果为无。但是,这些都不起作用。如何检查python None对象中的list [i]?
我创建了一个环形缓冲区,我想使用boost将该类导入Python.当我试图得到它的错误.
ring.cpp: In function ‘void init_module_ring()’:
ring.cpp:130:16: error: wrong number of template arguments (1, should be 4)
class_<Ring>("Ring").Please help me. Thanks in Advance.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include <boost/python/def.hpp>
#include<iostream>
using namespace std;
using namespace boost::python;
template <class T>
class Ring
{
public:
class iterator;
public:
unsigned int m_size;
unsigned int pos;
T *val;
Ring():
m_size(0),pos(0),val(NULL){};
Ring(int size):m_size(size),pos(0){
val=new T[m_size];
};
Ring(const Ring &other)
{
this->m_size = other.m_size;
this->pos= other.pos;
this->val = other.val;
}
~Ring()
{
delete[] val;
}
void insert(T data)
{ …Run Code Online (Sandbox Code Playgroud) 我正在考虑从boost :: python到pybind11移植一个复杂的代码,但是我对缺少类似的东西感到困惑boost::python::extract<...>().check()。我读到的pybind11::cast<T>可用于从中提取c ++对象py::object,但是检查是否可以进行强制转换的唯一方法是调用它并在强制转换失败时捕获异常。有什么我要注意的吗?
例如,让我们考虑pythonfile中具有以下结构的类mult.py。
mult.py
class Mult:
def __init__(self, first, second):
self.a = first
self.b = second
def multiply(self):
c = self.a*self.b
print 'The result of', self.a, 'x', self.b, ':', c
return c
Run Code Online (Sandbox Code Playgroud)
我们如何multiply在C++Code中从上面的类调用方法?(但没有PyRun_SimpleString()功能)
main.cpp
#include<python2.7/Python.h>
#include <boost/python.hpp>
int main()
{
Py_Initialize();
int a {10};
int b {5};
... CODE HERE (a*b using python function)
Py_Finalize();
}
Run Code Online (Sandbox Code Playgroud)
我当时在寻找解决方案Cython,boost.python但找不到。
附加链接: