feat(python/const): added constants bindings

constants module can now be fully accessed from python
This commit is contained in:
2025-05-05 11:59:24 -04:00
parent 2bf58671a0
commit 1ed0e9cde1
3 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h> // Needed for vectors, maps, sets, strings
#include <pybind11/stl_bind.h> // Needed for binding std::vector, std::map etc if needed directly
#include <string>
#include "const.h"
#include "bindings.h"
namespace py = pybind11;
void register_const_bindings(pybind11::module &const_submodule) {
py::class_<Constant>(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 "<Constant(name='" + c.name + "', value=" + std::to_string(c.value) +
", uncertainty=" + std::to_string(c.uncertainty) +
", unit='" + c.unit + "')>";
});
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."
);
}

View File

@@ -0,0 +1,5 @@
#pragma once
#include <pybind11/pybind11.h>
void register_const_bindings(pybind11::module &const_submodule);

View File

@@ -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,
)