fix(python/constants): adjusted python constants to be a true singleton / static class

This commit is contained in:
2025-05-05 13:25:27 -04:00
parent 0a7cc309b0
commit 4757a6f63a

View File

@@ -24,16 +24,31 @@ void register_const_bindings(pybind11::module &const_submodule) {
py::class_<Constants>(const_submodule, "Constants")
.def_property_readonly("loaded", &Constants::isLoaded)
.def("get", &Constants::get, py::arg("name"), "Get a constant by name. Returns None if not found.")
.def("__getitem__", &Constants::get, py::arg("name"))
.def("has", &Constants::has, py::arg("name"), "Check if a constant exists by name.")
.def("keys", &Constants::keys, "Get a list of all constant names.")
.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."
.def_static("get",
[](const std::string &name) {
return py::cast(
Constants::getInstance().get(name)
);
},
"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)
);
});
}