MaCh3  2.4.2
Reference Guide
Functions
manager.cpp File Reference
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "Manager/Manager.h"
#include "yaml-cpp/yaml.h"
Include dependency graph for manager.cpp:

Go to the source code of this file.

Functions

void initManager (py::module &m)
 

Function Documentation

◆ initManager()

void initManager ( py::module &  m)

Definition at line 11 of file manager.cpp.

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