I would like to return a list of collections.namedtuple from a boost::python wrapped function, but I'm not sure how to go about creating these objects from the C++ code. For some other types there is a convenient wrapper (e.g. dict) which makes this easy but nothing like that exists for namedtuple. What is the best way to do this?
Existing code for list of dict:
namespace py = boost::python;
struct cacheWrap {
...
py::list getSources() {
py::list result;
for (auto& src : srcCache) { // srcCache is a C++ vector
// {{{ --> Want to use a namedtuple here instead of dict
py::dict pysrc;
pysrc["url"] = src.url;
pysrc["label"] = src.label;
result.append(pysrc);
// }}}
}
return result;
}
...
};
BOOST_PYTHON_MODULE(sole) {
py::class_<cacheWrap,boost::noncopyable>("doc", py::init<std::string>())
.def("getSources", &cacheWrap::getSources)
;
}
namedtupleis a subclass oftuple, so perhaps you could start with the code which handles that former and modify it accordingly.