fix(python/constants): adjusted python constants to be a true singleton / static class
This commit is contained in:
@@ -24,16 +24,31 @@ void register_const_bindings(pybind11::module &const_submodule) {
|
|||||||
|
|
||||||
py::class_<Constants>(const_submodule, "Constants")
|
py::class_<Constants>(const_submodule, "Constants")
|
||||||
.def_property_readonly("loaded", &Constants::isLoaded)
|
.def_property_readonly("loaded", &Constants::isLoaded)
|
||||||
.def("get", &Constants::get, py::arg("name"), "Get a constant by name. Returns None if not found.")
|
.def_static("get",
|
||||||
.def("__getitem__", &Constants::get, py::arg("name"))
|
[](const std::string &name) {
|
||||||
.def("has", &Constants::has, py::arg("name"), "Check if a constant exists by name.")
|
return py::cast(
|
||||||
.def("keys", &Constants::keys, "Get a list of all constant names.")
|
Constants::getInstance().get(name)
|
||||||
.def(py::init([]() {
|
|
||||||
return &Constants::getInstance();
|
|
||||||
}),
|
|
||||||
// Tell pybind11 Python doesn't own this memory.
|
|
||||||
py::return_value_policy::reference,
|
|
||||||
"Get the singleton instance of Constants."
|
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
"Get a constant by name. Returns None if not found."
|
||||||
|
)
|
||||||
|
.def_static("has",
|
||||||
|
[](const std::string &name) {
|
||||||
|
return Constants::getInstance().has(name);
|
||||||
|
},
|
||||||
|
"Check if a constant exists by name.")
|
||||||
|
.def_static("keys",
|
||||||
|
[]() {
|
||||||
|
return py::cast(
|
||||||
|
Constants::getInstance().keys()
|
||||||
|
);
|
||||||
|
},
|
||||||
|
"Get a list of all constant names.")
|
||||||
|
.def_static("__class_getitem__",
|
||||||
|
[](const std::string &name) {
|
||||||
|
return py::cast(
|
||||||
|
Constants::getInstance().get(name)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user