Note that there are some explanatory texts on larger screens.

plurals
  1. POexposing std::vector<double> with boost.python
    primarykey
    data
    text
    <p>I have written some C++ code that generates a std::vector.</p> <p>I also have a python script that manipulates some data that, for now, I am declaring like this (below). </p> <pre><code>import numpy x = numpy.random.randn(1000) y = numpy.random.randn(1000) </code></pre> <p>I can run the script fine. From my C++ code:</p> <pre><code> using namespace boost::python; try{ Py_Initialize(); object main = import("__main__"); object global(main.attr("__dict__")); object result = exec_file("scatterPlot.py", global, global); Py_Finalize(); } catch(error_already_set){ PyErr_Print(); } return; </code></pre> <p>I have no idea how to get my C++ data to python. I've around quite a bit, but there doesn't seem to be anything definitive.</p> <p>I have in my C++</p> <pre><code>BOOST_PYTHON_MODULE(vector_indexing_suite_ext){ boost::python::class_&lt;std::vector&lt;double&gt; &gt;("PyVec") .def(boost::python::vector_indexing_suite&lt;std::vector&lt;double&gt; &gt;()); } </code></pre> <p>This seems to work, but as I understand, it only provides a class "PyVec" for my python script but not the data I need. Am I wrong?</p> <p>I've also seen some other people use <a href="http://aspn.activestate.com/ASPN/Mail/Message/cpp-sig/3786737" rel="nofollow noreferrer">boost::shared_ptr in a python mailing list</a>. </p> <p>I also found <a href="http://thread.gmane.org/gmane.comp.python.c++/12004/focus=12011" rel="nofollow noreferrer">this example</a> but found it confusing.</p> <p>I can think of a few approaches</p> <ol> <li>Pass something to the <code>boost::python::exec_file</code> method</li> <li>Using the <code>boost_indexing_suite_ext</code></li> <li>Uinsg <code>boost::shared_ptr</code></li> </ol> <p>Which approach is easiest to get going? No approach seems clear to me.</p> <p>Here are some more links I've looked at: <a href="http://www.boost.org/doc/libs/1_41_0/libs/python/doc/tutorial/doc/html/python/embedding.html" rel="nofollow noreferrer">from the boost website</a> <a href="http://wiki.python.org/moin/boost.python/HowTo#std.3A.3AC.2B-.2B-container" rel="nofollow noreferrer">from the python website</a> <a href="http://old.nabble.com/-python--Expose-a-struct-with-a-std::vector&lt;std::string&gt;-member-td21809375.html" rel="nofollow noreferrer">another mailing list thread</a></p> <p>UPDATE:</p> <p>This works for passing an <code>int</code> to my python code like below</p> <pre><code>int main(){ int five_squared=0; int a =3; try { Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); main_namespace["var"]=a; object ignored = exec("result = 5 ** var", main_namespace); five_squared = extract&lt;int&gt;(main_namespace["result"]); } catch( error_already_set ) { PyErr_Print(); } std::cout &lt;&lt; five_squared &lt;&lt; std::endl; return 0; } </code></pre> <p>But I want to pass a vector, when I try to do that in a similar fashion as above I get this error</p> <blockquote> <p>TypeError: No to_python (by-value) converter found for C++ type: std::vector ></p> </blockquote> <p>So, obviously I need to tell python how to deal with std::vector. I think this code could help with that.</p> <pre><code>BOOST_PYTHON_MODULE(vector_indexing_suite_ext){ boost::python::class_&lt;std::vector&lt;double&gt; &gt;("PyVec") .def(boost::python::vector_indexing_suite&lt;std::vector&lt;double&gt; &gt;()); } </code></pre> <p>But since std::vector is pretty common, there must be a defined way to do this... right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload