Skip to content
Snippets Groups Projects
Commit f7b083a5 authored by Bas van der Tol's avatar Bas van der Tol
Browse files

Merge branch 'ast-1340-extend-python-interface-parameter-set' into 'master'

Extend python interface to ParameterSet

See merge request RD/DP3!1227
parents 5f95d980 9aa048ad
No related branches found
No related tags found
No related merge requests found
......@@ -3,10 +3,12 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "../common/ParameterSet.h"
using dp3::common::ParameterSet;
using dp3::common::ParameterValue;
namespace py = pybind11;
......@@ -57,7 +59,20 @@ PYBIND11_MODULE(parameterset, m) {
ParameterSet::getString)
.def("get_string", (std::string(ParameterSet::*)(
const std::string &, const std::string &) const) &
ParameterSet::getString);
ParameterSet::getString)
.def("__contains__", &ParameterSet::isDefined)
.def("__getitem__", &ParameterSet::get);
py::class_<ParameterValue,
std::shared_ptr<ParameterValue> // holder type
>(m, "ParameterValue")
.def("__str__", &ParameterValue::getString)
.def("__int__", &ParameterValue::getInt)
.def("__float__", &ParameterValue::getDouble)
.def("__bool__", &ParameterValue::getBool)
.def("__iter__", [](const ParameterValue &v) {
return py::cast(v.getVector()).attr("__iter__")();
});
}
} // namespace pythondp3
......
......@@ -6,6 +6,12 @@
if("${CMAKE_CXX_FLAGS}" MATCHES "-fsanitize=address")
message(STATUS "The Python tests do not work with address sanitizer enabled.")
else()
add_python_tests(tPyDpBuffer tPyDpInfo tPyFields tPyFitters tPyStep
tQueueOutput)
add_python_tests(
tPyDpBuffer
tPyDpInfo
tPyFields
tPyFitters
tPyParameterSet
tPyStep
tQueueOutput)
endif()
# Copyright (C) 2024 ASTRON (Netherlands Institute for Radio Astronomy)
# SPDX-License-Identifier: GPL-3.0-or-later
"""
These tests are checking that the python bindings for the ParameterSet class
behave correctly.
Script can be invoked in two ways:
- as standalone from the build/pythondp3/test/unit directory,
using `pytest source/tPyParameterSet.py` (extended with pytest options of your choice)
- using ctest, see pythondp3/test/unit/CMakeLists.txt
"""
import sys
# Append current directory to system path in order to import testconfig
sys.path.append(".")
import testconfig as tcf
sys.path.insert(0, tcf.PYTHONDIR)
try:
"The import may fail while running pytest --collect-only"
import dp3.parameterset
except ImportError:
pass
def test_parameter_set_magic():
"""
Test the magic (double underscore) member functions of ParamterSet and ParameterValue
Tests the member functions __contains__ and __getitem__ of ParameterSet, and
__bool__, __float__, __int__, __iter__ and __str__ of ParameterValue
"""
ps = dp3.parameterset.ParameterSet()
ps.add("prefix.somekey", "42")
ps.add("prefix.vecint", "[1,2,3]")
ps.add("prefix.vecfloat", "[2.71828, 3.14159]")
ps.add("prefix.vecbool", "[T, F, True, False, 1, 0]")
ps.add("prefix.vecstr", "[CS001,CS002,CS003]")
ps.add("prefix.vecvecstr", "[[CS001,CS002],[CS003]]")
assert "prefix.not_existent_key" not in ps
assert "prefix.somekey" in ps
assert [int(v) for v in ps["prefix.vecint"]] == [1, 2, 3]
assert [str(v) for v in ps["prefix.vecstr"]] == ["CS001", "CS002", "CS003"]
assert [float(v) for v in ps["prefix.vecfloat"]] == [2.71828, 3.14159]
assert [bool(v) for v in ps["prefix.vecbool"]] == [
True,
False,
True,
False,
True,
False,
]
assert [[str(vv) for vv in v] for v in ps["prefix.vecvecstr"]] == [
["CS001", "CS002"],
["CS003"],
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment