Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there's a couple of ways that are easier to accomplish than writing your own converter. You can use boost::python's map_indexing_suite to do the conversion for you, or you can use keyword arguments in python. I personally prefer keyword arguments, as this is the more "Pythonic" way to do this.</p> <p>So this is your class (I added a typedef for the map):</p> <pre><code>typedef std::map&lt;std::string, double&gt; MyMap; class myClass { public: // Constructors - set a-f to default values. void SetParameters(MyMap &amp;); private: double a, b, c, d, e, f; }; </code></pre> <p>Example using map_indexing_suite:</p> <pre><code>#include &lt;boost/python/suite/indexing/map_indexing_suite.hpp&gt; using boost::python; BOOST_PYTHON_MODULE(mymodule) { class_&lt;std::map&lt;std::string, double&gt; &gt;("MyMap") .def(map_indexing_suite&lt;std::map&lt;std::wstring, double&gt; &gt;() ); class_&lt;myClass&gt;("myClass") .def("SetParameters", &amp;myClass::SetParameters); } </code></pre> <p>Example using keyword arguments. This requires using a raw_function wrapper:</p> <pre><code>using namespace boost::python; object SetParameters(tuple args, dict kwargs) { myClass&amp; self = extract&lt;myClass&amp;&gt;(args[0]); list keys = kwargs.keys(); MyMap outMap; for(int i = 0; i &lt; len(keys); ++i) { object curArg = kwargs[keys[i]]; if(curArg) { outMap[extract&lt;std::string&gt;(keys[i])] = extract&lt;double&gt;(kwargs[keys[i]]); } } self.SetParameters(outMap); return object(); } BOOST_PYTHON_MODULE(mymodule) { class_&lt;myClass&gt;("myClass") .def("SetParameters", raw_function(&amp;SetParameters, 1)); } </code></pre> <p>this allows you to write stuff like this in Python:</p> <pre><code>A.SetParameters(a = 2.2, d = 4.3, b = 9.3) </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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