0

I followed https://riptutorial.com/boost/example/25280/wrapping-std--vector-in-boost-python but how to include vector_indexing_suite for passing vector as argument? I am getting the error: No Python class registered for C++ class std::vector > when running the code:

BOOST_PYTHON_MODULE(Strat)
{
    bp::class_<std::vector<long> >("Long_vec")
        .def(bp::vector_indexing_suite<std::vector<long> >())
    ;


    bp::class_<PyStrat, boost::noncopyable>("Strat")
        .def("gen_fibonacci", &Strat::gen_fibonacci)
        ;
}
int main() {
    try
    {
        Py_Initialize();
        // register the python module we created, so our script can import it
        PyImport_AppendInittab("Strat",&initStrat);
        //PyImport_AppendInittab("StrategyFramework", &initStrategyFramework);

        // import the __main__ module and obtain the globals dict
        bp::object main     = bp::import("__main__");
        bp::object globals  = main.attr("__dict__");

        // import our strategy.py file
        bp::object module   = import("Strat", "./python_plugins/strat1.py", globals);

        // obtain the strategy class and instantiate one
        bp::object Strategy = module.attr("Strategy");
        bp::object strategy = Strategy();
        std::cout<<"not here";

        Helper a;
        strategy.attr("gen_fibonacci")(boost::ref(a.v),1,100);
        long ans=a.sum_series(a.v);
        std::cout<<"done";
        return 0;
    }
    catch(const bp::error_already_set&)
    {
        std::cerr << ">>> Error! Uncaught exception:\n";
        PyErr_Print();
        return 1;
    }
}

what am i doing wrong?

strat1.py:

    from Strat import *

    class Strategy:
        def gen_fibonacci(self,l,ind,n):
            num = 3
            t1 = 0 
            t2 = 1
            nextTerm = 0
            i=1
            if ind==1:
                l.append(0)
                l.append(1)
                i=3
            if ind==2:
                l.append(1)
                i=2
            while i<n:
                nextTerm=t1+t2
                t1=t2
                t2=nextTerm
                if num>=ind:
                    i=i+1
                    l.append(nextTerm)
                num=num+1
            return 0
  • Can you please provide simple example, instead of your code, that has too much unavailable code like strat1 and so on? – ForEveR Apr 30 '20 at 11:24
  • @ForEveR I just want to pass a.v(that is a vector) as argument to the method get_fibonacci() I tried using vector_indexing_suite as mentioned her https://stackoverflow.com/questions/6157409/stdvector-to-boostpythonlist (answer 2) but it throws the error I listed. I have added the strat1.py file as you requested – Deepansh Nagaria Apr 30 '20 at 11:40
  • I'm trying to understood why you are calling python module from C++ code, trying to pass vector there. I'm not sure that vector_indexing_suite is gonna work here. You are trying to pass vector to python function, instead of calling C++ function, that receives vector from python. – ForEveR Apr 30 '20 at 12:52
  • Man I am working on a demo project for building plugin both in python and c++ (this is my requirement), I can achieve it using boost and boost:python, I wrote a method that receives a vector as argument which should be converted to a python list by the vector_indexing_suite wrapper, but it is not doing so, I asked this question to make it work and know my mistake – Deepansh Nagaria Apr 30 '20 at 16:25

0 Answers0