Skip to content

Commit 30ac5fe

Browse files
committed
Same trick for JSON config
1 parent e9f4abb commit 30ac5fe

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

examples/10_streaming_read.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
import json
32
import sys
43

54
import openpmd_api as io
@@ -19,7 +18,7 @@
1918
sys.exit(0)
2019

2120
series = io.Series("simData.sst", io.Access_Type.read_linear,
22-
json.dumps(config))
21+
options=config)
2322

2423
# Read all available iterations and print electron position data.
2524
# Direct access to iterations is possible via `series.iterations`.

examples/10_streaming_write.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
import json
32
import sys
43

54
import numpy as np
@@ -21,8 +20,7 @@
2120

2221
# create a series and specify some global metadata
2322
# change the file extension to .json, .h5 or .bp for regular file writing
24-
series = io.Series("simData.sst", io.Access_Type.create,
25-
json.dumps(config))
23+
series = io.Series("simData.sst", io.Access_Type.create, options=config)
2624
series.set_author("Franz Poeschel <f.poeschel@hzdr.de>")
2725
series.set_software("openPMD-api-python-examples")
2826

src/binding/python/Series.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "openPMD/binding/python/Common.hpp"
3232
#include <filesystem>
3333
#include <optional>
34+
#include <tuple>
3435

3536
#if openPMD_HAVE_MPI
3637
// re-implemented signatures:
@@ -72,16 +73,34 @@ namespace internal
7273
{
7374
struct DefineSeriesConstructorPerPathType
7475
{
75-
template <typename PathType>
76+
static constexpr auto json_cfg_as_string(std::string const &str)
77+
-> std::string const &
78+
{
79+
return str;
80+
}
81+
82+
static auto json_cfg_as_string(py::object const &obj) -> std::string
83+
{
84+
py::module_ json = py::module_::import("json");
85+
auto dumps = json.attr("dumps");
86+
auto dumped = dumps(obj);
87+
return py::cast<std::string>(dumped);
88+
}
89+
90+
template <typename TupleType>
7691
static void call(py::class_<Series, Attributable> &py_class)
7792
{
93+
using PathType = typename std::tuple_element<0, TupleType>::type;
94+
using JsonCfgType = typename std::tuple_element<1, TupleType>::type;
95+
7896
py_class
7997
.def(
8098
py::init([](PathType const &filepath,
8199
Access at,
82-
std::string const &options) {
100+
JsonCfgType const &options) {
101+
auto options_ = json_cfg_as_string(options);
83102
py::gil_scoped_release release;
84-
return new Series(filepath, at, options);
103+
return new Series(filepath, at, options_);
85104
}),
86105
py::arg("filepath"),
87106
py::arg("access"),
@@ -137,7 +156,8 @@ It will be replaced with an automatically determined file name extension:
137156
py::init([](PathType const &filepath,
138157
Access at,
139158
py::object &comm,
140-
std::string const &options) {
159+
JsonCfgType const &options) {
160+
auto options_ = json_cfg_as_string(options);
141161
auto variant = pythonObjectAsMpiComm(comm);
142162
if (auto errorMsg = std::get_if<std::string>(&variant))
143163
{
@@ -147,7 +167,10 @@ It will be replaced with an automatically determined file name extension:
147167
{
148168
py::gil_scoped_release release;
149169
return new Series(
150-
filepath, at, std::get<MPI_Comm>(variant), options);
170+
filepath,
171+
at,
172+
std::get<MPI_Comm>(variant),
173+
options_);
151174
}
152175
}),
153176
py::arg("filepath"),
@@ -299,8 +322,10 @@ not possible once it has been closed.
299322
py::class_<Series, Attributable> cl(m, "Series");
300323
::auxiliary::ForEachType<
301324
::internal::DefineSeriesConstructorPerPathType,
302-
std::string,
303-
std::filesystem::path>::template call(cl);
325+
std::tuple<std::string, std::string>,
326+
std::tuple<std::string, py::object>,
327+
std::tuple<std::filesystem::path, std::string>,
328+
std::tuple<std::filesystem::path, py::object> >::template call(cl);
304329

305330
cl.def("__bool__", &Series::operator bool)
306331
.def("__len__", [](Series const &s) { return s.iterations.size(); })

0 commit comments

Comments
 (0)