4

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)
   ;
}
1
  • A namedtuple is a subclass of tuple, so perhaps you could start with the code which handles that former and modify it accordingly. Commented Dec 3, 2012 at 1:33

1 Answer 1

8

The following code does the job. Better solutions will get the check.

Do this in ctor to set field sourceInfo:

auto collections = py::import("collections");
auto namedtuple = collections.attr("namedtuple");
py::list fields;
fields.append("url"); 
fields.append("label");
sourceInfo = namedtuple("Source", fields);

New method:

py::list getSources() {
   py::list result;
   for (auto& src : srcCache)
      result.append(sourceInfo(src.url, src.label));
   return result;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.