MaCh3  2.2.3
Reference Guide
manager.cpp
Go to the documentation of this file.
1 // pybind includes
2 #include <pybind11/pybind11.h>
3 #include <pybind11/stl.h>
4 // MaCh3 includes
5 #include "Manager/Manager.h"
6 // yaml includes
7 #include "yaml-cpp/yaml.h"
8 
9 namespace py = pybind11;
10 
11 void initManager(py::module &m){
12 
13  auto m_manager = m.def_submodule("manager");
14  m_manager.doc() =
15  "This is a Python binding of MaCh3s C++ based manager library.";
16 
17  // Bind some of the cpp-yaml library
18  // shamelessly stolen from stackoverflow: https://stackoverflow.com/questions/62347521/using-pybind11-to-wrap-yaml-cpp-iterator
19  py::enum_<YAML::NodeType::value>(m_manager, "NodeType")
20  .value("Undefined", YAML::NodeType::Undefined)
21  .value("Null", YAML::NodeType::Null)
22  .value("Scalar", YAML::NodeType::Scalar)
23  .value("Sequence", YAML::NodeType::Sequence)
24  .value("Map", YAML::NodeType::Map);
25 
26  py::class_<YAML::Node>(m_manager, "YamlNode")
27  .def(py::init<const std::string &>())
28 
29  .def("data",
30  [](const YAML::Node node){
31  if ( node.Type() != YAML::NodeType::Scalar )
32  {
33  throw MaCh3Exception(__FILE__, __LINE__, "Attempting to access the data of non-scalar yaml node. This is undefined.");
34  }
35  return node.Scalar();
36  },
37  "Access the data stored in the node. This is only valid if the node is a 'scalar' type, i.e. it is a leaf of the yaml tree structure.")
38 
39  .def("__getitem__",
40  [](const YAML::Node node, const std::string& key){
41  return node[key];
42  })
43 
44  .def("__getitem__",
45  [](const YAML::Node node, const int& key){
46  if ( node.Type() != YAML::NodeType::Sequence)
47  {
48  throw MaCh3Exception(__FILE__, __LINE__, "Trying to access a non sequence yaml node with integer index");
49  }
50  return node[key];
51  })
52 
53  .def("__iter__",
54  [](const YAML::Node &node) {
55  return py::make_iterator(node.begin(), node.end());},
56  py::keep_alive<0, 1>())
57 
58  .def("__str__",
59  [](const YAML::Node& node) {
60  YAML::Emitter out;
61  out << node;
62  return std::string(out.c_str());
63  })
64 
65  .def("type", &YAML::Node::Type)
66 
67  .def("__len__", &YAML::Node::size)
68  ;
69 
70  py::class_<YAML::detail::iterator_value, YAML::Node>(m_manager, "_YamlDetailIteratorValue")
71  .def(py::init<>())
72  .def("first", [](YAML::detail::iterator_value& val) { return val.first;})
73  .def("second", [](YAML::detail::iterator_value& val) { return val.second;})
74  ;
75 
76  m.def("load_file", &YAML::LoadFile, "");
77 
78  py::class_<manager>(m_manager, "Manager")
79  .def(
80  py::init<std::string const &>(),
81  "create a Manager object with a specified *config_file*",
82  py::arg("config_file")
83  )
84 
85  .def(
86  "print",
88  "Print currently used config."
89  )
90 
91  .def(
92  "raw",
93  &manager::raw,
94  "Get the raw yaml config."
95  )
96 
97  .def(
98  "get_test_stat",
100  "Get the test statistic that was specified in the config file."
101  )
102  ;
103 
104 }
int size
void Print()
Print currently used config.
Definition: Manager.cpp:90
int GetMCStatLLH()
Get likelihood type defined in the config.
Definition: Manager.cpp:98
YAML::Node const & raw()
Return config.
Definition: Manager.h:41
void initManager(py::module &m)
Definition: manager.cpp:11