HOPS
HOPS class reference
MHO_PyParameterStoreInterface.hh
Go to the documentation of this file.
1 #ifndef MHO_PyParameterStoreInterface_HH__
2 #define MHO_PyParameterStoreInterface_HH__
3 
4 #include "MHO_ParameterStore.hh"
5 
6 //need these extras to be able to translate between nl:json and py:dict or py::object
7 #include "pybind11_json/pybind11_json.hpp"
8 #include <pybind11/embed.h>
9 #include <pybind11/pybind11.h>
10 namespace py = pybind11;
11 namespace nl = nlohmann;
12 using namespace pybind11::literals;
13 
14 namespace hops
15 {
16 
26 {
27  public:
28  MHO_PyParameterStoreInterface(MHO_ParameterStore* paramStore): fParameterStore(paramStore){};
30 
31  bool IsPresent(const std::string& value_path) const { return fParameterStore->IsPresent(value_path); }
32 
33  //get a specific value
34  py::object Get(const std::string& value_path) const
35  {
36  mho_json obj;
37  bool ok = fParameterStore->Get(value_path, obj);
38  if(!ok)
39  {
40  msg_error("python_bindings", "error getting value associated with key: " << value_path << eom);
41  py::print("error getting value associated with key: ", value_path);
42  }
43 
44  return obj;
45  }
46 
47  //set a specific value
48  void Set(const std::string& value_path, py::object value) const
49  {
50  mho_json obj = value;
51  bool ok = fParameterStore->Set(value_path, obj);
52  if(!ok)
53  {
54  msg_error("python_bindings", "error setting value associated with key: " << value_path << eom);
55  py::print("error setting value associated with key: ", value_path);
56  }
57  }
58 
59  //returns a python dictionary containing all of the parameter stores contents
60  py::dict GetContents()
61  {
62  mho_json obj;
63  fParameterStore->DumpData(obj);
64  return obj;
65  }
66 
67  private:
68  //pointer to the parameter store
69  MHO_ParameterStore* fParameterStore;
70 };
71 
72 inline void DeclarePyParameterStoreInterface(py::module& m, std::string pyclass_name)
73 {
74  py::class_< MHO_PyParameterStoreInterface >(m, pyclass_name.c_str())
75  //no __init__ def here, as this class is not meant to be constructable on the python side
77  "check if parameter with specified path is present in the store", py::arg("value_path"))
78  .def("get_by_path", &hops::MHO_PyParameterStoreInterface::Get, "get parameter at path", py::arg("value_path"))
79  .def("set_by_path", &hops::MHO_PyParameterStoreInterface::Set, "set the value of the parameter at the specified path",
80  py::arg("value_path"), py::arg("value"))
81  .def("get_contents", &hops::MHO_PyParameterStoreInterface::GetContents, py::return_value_policy::copy,
82  "get a copy of the contents of the parameter store as a dictionary");
83 }
84 
85 } // namespace hops
86 
87 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
Class MHO_ParameterStore.
Definition: MHO_ParameterStore.hh:52
python bindings for the MHO_ParameterStore
Definition: MHO_PyParameterStoreInterface.hh:26
py::object Get(const std::string &value_path) const
Definition: MHO_PyParameterStoreInterface.hh:34
void Set(const std::string &value_path, py::object value) const
Definition: MHO_PyParameterStoreInterface.hh:48
virtual ~MHO_PyParameterStoreInterface()
Definition: MHO_PyParameterStoreInterface.hh:29
bool IsPresent(const std::string &value_path) const
Definition: MHO_PyParameterStoreInterface.hh:31
MHO_PyParameterStoreInterface(MHO_ParameterStore *paramStore)
Definition: MHO_PyParameterStoreInterface.hh:28
py::dict GetContents()
Definition: MHO_PyParameterStoreInterface.hh:60
Definition: MHO_ChannelLabeler.hh:17
void DeclarePyParameterStoreInterface(py::module &m, std::string pyclass_name)
Definition: MHO_PyParameterStoreInterface.hh:72
Definition: vex.h:175