From ce5704db88fc2770f06c9f265e901938158dbfaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Wed, 6 Nov 2024 22:13:04 +0100 Subject: [PATCH] Use polymorphism for meshes/particlesPath in Python --- src/binding/python/Series.cpp | 68 ++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/src/binding/python/Series.cpp b/src/binding/python/Series.cpp index 36a878f03b..2ceaf4b4b3 100644 --- a/src/binding/python/Series.cpp +++ b/src/binding/python/Series.cpp @@ -22,10 +22,12 @@ #include "openPMD/IO/Access.hpp" #include "openPMD/IterationEncoding.hpp" #include "openPMD/auxiliary/JSON.hpp" +#include "openPMD/auxiliary/Variant.hpp" #include "openPMD/binding/python/Pickle.hpp" #include "openPMD/config.hpp" #include "openPMD/binding/python/Common.hpp" +#include #if openPMD_HAVE_MPI // re-implemented signatures: @@ -282,26 +284,60 @@ this method. &Series::openPMDextension, &Series::setOpenPMDextension) .def_property("base_path", &Series::basePath, &Series::setBasePath) - .def_property( - "meshes_path", - &Series::meshesPath, - py::overload_cast(&Series::setMeshesPath)) .def("get_rank_table", &Series::rankTable, py::arg("collective")) .def("set_rank_table", &Series::setRankTable, py::arg("my_rank_info")) .def_property( - "particles_path", - &Series::particlesPath, - py::overload_cast(&Series::setParticlesPath)) - .def_property( - "meshes_paths", - &Series::meshesPath, - py::overload_cast const &>( - &Series::setMeshesPath)) + "meshes_path", + [](Series &self) + -> std::variant> { + using res_t = + std::variant>; + auto res = self.meshesPaths(); + if (res.size() == 1) + { + return res_t{std::move(res[0])}; + } + else + { + return res_t{std::move(res)}; + } + }, + [](Series &self, + std::variant> const &arg) + -> Series & { + std::visit( + [&](auto const &arg_resolved) { + self.setMeshesPath(arg_resolved); + }, + arg); + return self; + }) .def_property( - "particles_paths", - &Series::particlesPath, - py::overload_cast const &>( - &Series::setParticlesPath)) + "particles_path", + [](Series &self) + -> std::variant> { + using res_t = + std::variant>; + auto res = self.particlesPaths(); + if (res.size() == 1) + { + return res_t{std::move(res[0])}; + } + else + { + return res_t{std::move(res)}; + } + }, + [](Series &self, + std::variant> const &arg) + -> Series & { + std::visit( + [&](auto const &arg_resolved) { + self.setParticlesPath(arg_resolved); + }, + arg); + return self; + }) .def_property("author", &Series::author, &Series::setAuthor) .def_property( "machine",