MUQ  0.4.3
PyDictConversion.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Utilities;
4 
5 void muq::Utilities::AddDictToPtree(pybind11::dict dict, std::string basePath, boost::property_tree::ptree &pt) {
6  pybind11::object keys = pybind11::list(dict.attr("keys")());
7  std::vector<std::string> keysCpp = keys.cast<std::vector<std::string>>();
8 
9  for(auto& key : keysCpp){
10 
11  // Recursively add dictionaries
12  if(pybind11::isinstance<pybind11::dict>(dict.attr("get")(key))){
13  AddDictToPtree(dict.attr("get")(key), basePath + key + ".", pt);
14 
15  // Convert lists in the comma-separated strings
16  }else if(pybind11::isinstance<pybind11::list>(dict.attr("get")(key))){
17  std::string val = "";
18  for(auto comp : pybind11::list(dict.attr("get")(key)))
19  val += "," + std::string(pybind11::str(comp));
20  pt.put(basePath + key, val.substr(1));
21 
22  // Add all the other objects through their "str" interpretation
23  }else{
24  pt.put(basePath + key, pybind11::str(dict.attr("get")(key)));
25  }
26  }
27 }
28 
29 boost::property_tree::ptree muq::Utilities::ConvertDictToPtree(pybind11::dict dict) {
30 
31  boost::property_tree::ptree pt;
32 
33  AddDictToPtree(dict, "", pt);
34 
35  return pt;
36 }
void AddDictToPtree(pybind11::dict dict, std::string basePath, boost::property_tree::ptree &pt)
boost::property_tree::ptree ConvertDictToPtree(pybind11::dict dict)