From 1ed0e9cde1dcc3623403b338b844529f3932c654 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 11:59:24 -0400 Subject: [PATCH] feat(python/const): added constants bindings constants module can now be fully accessed from python --- src/python/const/bindings.cpp | 39 +++++++++++++++++++++++++++++++++++ src/python/const/bindings.h | 5 +++++ src/python/const/meson.build | 17 +++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/python/const/bindings.cpp create mode 100644 src/python/const/bindings.h create mode 100644 src/python/const/meson.build diff --git a/src/python/const/bindings.cpp b/src/python/const/bindings.cpp new file mode 100644 index 0000000..c606e76 --- /dev/null +++ b/src/python/const/bindings.cpp @@ -0,0 +1,39 @@ +#include +#include // Needed for vectors, maps, sets, strings +#include // Needed for binding std::vector, std::map etc if needed directly + +#include +#include "const.h" +#include "bindings.h" + +namespace py = pybind11; + + +void register_const_bindings(pybind11::module &const_submodule) { + py::class_(const_submodule, "Constant") + .def_readonly("name", &Constant::name) + .def_readonly("value", &Constant::value) + .def_readonly("uncertainty", &Constant::uncertainty) + .def_readonly("unit", &Constant::unit) + .def_readonly("reference", &Constant::reference) + .def("__repr__", [](const Constant &c) { + return ""; + }); + + py::class_(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." + ); + +} diff --git a/src/python/const/bindings.h b/src/python/const/bindings.h new file mode 100644 index 0000000..01cd891 --- /dev/null +++ b/src/python/const/bindings.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void register_const_bindings(pybind11::module &const_submodule); diff --git a/src/python/const/meson.build b/src/python/const/meson.build new file mode 100644 index 0000000..a816819 --- /dev/null +++ b/src/python/const/meson.build @@ -0,0 +1,17 @@ +# Define the library +bindings_sources = files('bindings.cpp') +bindings_headers = files('bindings.h') + +dependencies = [ + const_dep, + python3_dep, + pybind11_dep, +] + +shared_module('py_const', + bindings_sources, + include_directories: include_directories('.'), + cpp_args: ['-fvisibility=default'], + install : true, + dependencies: dependencies, +)