Skip to content

Commit ce5704d

Browse files
committed
Use polymorphism for meshes/particlesPath in Python
1 parent b0d7fe6 commit ce5704d

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/binding/python/Series.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
#include "openPMD/IO/Access.hpp"
2323
#include "openPMD/IterationEncoding.hpp"
2424
#include "openPMD/auxiliary/JSON.hpp"
25+
#include "openPMD/auxiliary/Variant.hpp"
2526
#include "openPMD/binding/python/Pickle.hpp"
2627
#include "openPMD/config.hpp"
2728

2829
#include "openPMD/binding/python/Common.hpp"
30+
#include <variant>
2931

3032
#if openPMD_HAVE_MPI
3133
// re-implemented signatures:
@@ -282,26 +284,60 @@ this method.
282284
&Series::openPMDextension,
283285
&Series::setOpenPMDextension)
284286
.def_property("base_path", &Series::basePath, &Series::setBasePath)
285-
.def_property(
286-
"meshes_path",
287-
&Series::meshesPath,
288-
py::overload_cast<std::string const &>(&Series::setMeshesPath))
289287
.def("get_rank_table", &Series::rankTable, py::arg("collective"))
290288
.def("set_rank_table", &Series::setRankTable, py::arg("my_rank_info"))
291289
.def_property(
292-
"particles_path",
293-
&Series::particlesPath,
294-
py::overload_cast<std::string const &>(&Series::setParticlesPath))
295-
.def_property(
296-
"meshes_paths",
297-
&Series::meshesPath,
298-
py::overload_cast<std::vector<std::string> const &>(
299-
&Series::setMeshesPath))
290+
"meshes_path",
291+
[](Series &self)
292+
-> std::variant<std::string, std::vector<std::string>> {
293+
using res_t =
294+
std::variant<std::string, std::vector<std::string>>;
295+
auto res = self.meshesPaths();
296+
if (res.size() == 1)
297+
{
298+
return res_t{std::move(res[0])};
299+
}
300+
else
301+
{
302+
return res_t{std::move(res)};
303+
}
304+
},
305+
[](Series &self,
306+
std::variant<std::string, std::vector<std::string>> const &arg)
307+
-> Series & {
308+
std::visit(
309+
[&](auto const &arg_resolved) {
310+
self.setMeshesPath(arg_resolved);
311+
},
312+
arg);
313+
return self;
314+
})
300315
.def_property(
301-
"particles_paths",
302-
&Series::particlesPath,
303-
py::overload_cast<std::vector<std::string> const &>(
304-
&Series::setParticlesPath))
316+
"particles_path",
317+
[](Series &self)
318+
-> std::variant<std::string, std::vector<std::string>> {
319+
using res_t =
320+
std::variant<std::string, std::vector<std::string>>;
321+
auto res = self.particlesPaths();
322+
if (res.size() == 1)
323+
{
324+
return res_t{std::move(res[0])};
325+
}
326+
else
327+
{
328+
return res_t{std::move(res)};
329+
}
330+
},
331+
[](Series &self,
332+
std::variant<std::string, std::vector<std::string>> const &arg)
333+
-> Series & {
334+
std::visit(
335+
[&](auto const &arg_resolved) {
336+
self.setParticlesPath(arg_resolved);
337+
},
338+
arg);
339+
return self;
340+
})
305341
.def_property("author", &Series::author, &Series::setAuthor)
306342
.def_property(
307343
"machine",

0 commit comments

Comments
 (0)