boost-python

why do I need comparison operators in boost python vector indexing suite?

那年仲夏 提交于 2019-12-05 07:41:21
I would like to expose C++ code with a std::vector<A> to python. My class A{}; does not have a comparison operator implemented. When I try BOOST_PYTHON_MODULE(libmyvec) { using namespace boost::python; class_<A>("A"); class_<std::vector<A> >("Avec") .def(boost::python::vector_indexing_suite<std::vector<A> >()); } I get an error about comparison operators. If I change the definition of A to class A { public: bool operator==(const A& other) {return false;} bool operator!=(const A& other) {return true;} }; It works like a charm. Why do I need to implement these comparison operators? Is there any

How can I specify the value of a named argument in boost.python?

我是研究僧i 提交于 2019-12-05 06:18:05
i want to embed a function written in python into c++ code. My python code is:test.py def func(x=None, y=None, z=None): print x,y,z My c++ code is: module = import("test"); namespace = module.attr("__dict__"); //then i want to know how to pass value 'y' only. module.attr("func")("y=1") // is that right? I'm not sure Boost.Python implements the ** dereference operator as claimed, but you can still use the Python C-API to execute the method you are intested on, as described here . Here is a prototype of the solution: //I'm starting from where you should change boost::python::object callable =

Using c++11 lambda as accessor function in boost::python's add_property (get_signature fails with lambda)

自闭症网瘾萝莉.ら 提交于 2019-12-05 04:37:35
I am trying to use c++11 lambdas as accessor functions in boost::python 's add_property , something along the following (the lambda is not strictly needed in this example, but will be needed for more complicated things happening inside the lambda, such as validation): #include<boost/python.hpp> struct A{ A(): a(2){}; int a; }; BOOST_PYTHON_MODULE(boost_python_lambda) { boost::python::class_<A>("A") // .def_readonly("a",&A::a) // the classical way: works fine .add_property("a",[](const A& a){return a.a;}) ; } However, compiling with clang++ (ver. 3.2) and -std=c++11 (the result is the same with

Boost Python: polymorphic container?

安稳与你 提交于 2019-12-05 00:14:20
问题 I have a method (or function) which returns a reference to a list of polymorphic objects: class A { }; class B : public A { }; std::list<boost::shared_ptr<A> >& getList(); How do I expose such a function in boost::python so that when iterating on the list in python, I would see the different types of A s and B s ? 回答1: First, make sure your classes are indeed polymorphic (i.e. they have at least one virtual function or a virtual destructor). Your example above doesn't, though I'm sure your

Class-scoped enum

百般思念 提交于 2019-12-04 23:10:32
I have a c++ class with an enum inside, and I wanted to mimick that with boost::python , so that I can write MyClass.value in python. boost::python::class_ does not have an enum_ method, and I was looking for workarounds. I first tried with lambdas like MyClass{ enum{value1,value2}; }; class_<MyClass>("MyClass").add_property("value1",&[](){return value1;}).staticmethod("value1"); which gives compiler error (in get_signature for add_property ). I know I could create getter method for each of the values, but that seems very awkward to me (typing-wise). Using attr : auto classObj=class_<MyClass>(

Point FindBoost CMAKE to boost_python Windows 10, VS 2017

大城市里の小女人 提交于 2019-12-04 22:44:52
My high level goal is to install the BGSlibrary which requires boost for python on Windows 10 using Visual Studio 2017. I compiled opencv and boost (1.64.0) from source using cmake 3.9.0. During cmake for BGSLIBRARY I get $ cmake -DBGS_PYTHON_SUPPORT=ON -DBOOST_ROOT="C:/Program Files/boost_1_64_0/" .. -- BGSLIBRARY WITH PYTHON SUPPORT: ON -- OpenCV library status: -- version: 3.3.0 CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.9/ Modules/FindBoost.cmake:1877 (message): Unable to find the requested Boost libraries. Boost version: 1.64.0 Boost include path: C:/Program Files/boost_1

Boost Python Hello World example not working in Python

走远了吗. 提交于 2019-12-04 21:49:43
问题 I'm having a great deal of trouble using my c++ code from Visual C++ (wrapped by boost) in Python. Alright, so the tools I'm using are: Visual Studio 2010, BoostPro 1_47, Windows 7, and Python 2.7 (32-bit). I have the following code which compiles nicely in Visual Studio 2010: #define BOOST_PYTHON_STATIC_LIB #include <boost/python.hpp> using namespace boost::python; struct World { void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; }; BOOST

Building Boost.Python

故事扮演 提交于 2019-12-04 21:26:12
问题 I am trying to build Boost.Python according to the instructions on the official website. My OS is Windows 7 64-bit, the compiler is MSVC11, the boost is 1.54. =================== Install Boost ================== To install basic Boost, I downloaded the boost library from its website, unzip it to my local disk. The path is C:\local\boost_1_54_0 . =============== Install Boost.Python =============== I then find that Boost.Python need to be built separately. So I followed the Boost.Python

Custom RTTI for use in script-defined types

旧街凉风 提交于 2019-12-04 19:36:33
I'm developing a game engine in C++ that allows Python scripting. Here's an example of how functions can be defined in Python that are then called by the an event manager whenever a certain event occurs: # Import some classes defined in C++ from cpp.event import PlayerDiesEvent, EventManager def onPlayerDeath(event): pass em = EventManager() em.connect(PlayerDiesEvent, onPlayerDeath) em.post(PlayerDiesEvent(...)) # Will call `onPlayerDeath` The user can also define new types of events: from cpp.event import Event, EventManager class CustomEvent(Event): pass def onCustomEvent(event): pass em =

How to expose a c++ function taking variable arguments in boost python

匆匆过客 提交于 2019-12-04 17:43:00
I have a c++ function taking variable number of arguments. char const* Fun(int num, ...) { ....//does some processing on the arguments passed } Boost Python code for exposing this function is written as, using namespace boost::python; BOOST_PYTHON_MODULE( lib_boost ) { def( "Fun", Fun ); } while compiling this code gives the below error In file included from /boost_1_42_0/boost/python/data_members.hpp:15, from /boost_1_42_0/boost/python/class.hpp:17, from /boost_1_42_0/boost/python.hpp:18, from Lib_boost.h:3, from Lib_boost.cpp:1: /boost_1_42_0/boost/python/make_function.hpp: In function