For performance reasons, I want to port parts of my python program to C ++, and so I'm trying to write a simple extension for my program. The C ++ part will build a dictionary, which then needs to be delivered to a Python program.
One of the ways I found is to build my dictated object in C ++, for example. a boost::unordered_map
, and then translate it into Python using the Py_BuildValue
[1] method, which is able to create Python dicts. But this method, which involves converting the container to a string representation and vice versa, seems too big “around the corner” to be the most efficient solution !?
So my question is: What is the most efficient way to create a Python dictionary in C ++? . I saw that boost has a Python library that supports container mapping between C ++ and Python, but I didn’t do that, t find what I need in the documentation so far. If there is such a way, I would prefer to directly build a Python dict in C ++ so that you do not need to copy, etc. But if the most effective way to do this is another, I also agree with that.
Here is the (simplified) C ++ code that I compile in .dll / .pyd:
#include <iostream> #include <string> #include <Python.h> #include "boost/unordered_map.hpp" #include "boost/foreach.hpp" extern "C"{ typedef boost::unordered_map<std::string, int> hashmap; static PyObject* _rint(PyObject* self, PyObject* args) { hashmap my_hashmap; // DO I NEED THIS? my_hashmap["a"] = 1; // CAN I RATHER INSERT TO PYTHON DICT DIRECTLY?? BOOST_FOREACH(hashmap::value_type i, my_hashmap) { // INSERT ELEMENT TO PYTHON DICT } // return PYTHON DICT } static PyMethodDef TestMethods[] = { {"rint", _rint, METH_VARARGS, ""}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC inittest(void) { Py_InitModule("test", TestMethods); } } // extern "C"
I want to use this in Python, for example:
import test new_dict = test.rint()
The dictionary will display strings in integers. Thanks for any help!