Skip to content

Commit

Permalink
Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jan 9, 2024
1 parent 414c622 commit 8f606c5
Showing 1 changed file with 58 additions and 50 deletions.
108 changes: 58 additions & 50 deletions src/binding/python/Dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,66 @@

void init_Dataset(py::module &m)
{
py::class_<Dataset>(m, "Dataset")
auto pyDataset =
py::class_<Dataset>(m, "Dataset")
.def(
py::init<Datatype, Extent>(),
py::arg("dtype"),
py::arg("extent"))
.def(py::init<Extent>(), py::arg("extent"))
.def(
py::init([](py::dtype dt, Extent const &e) {
auto const d = dtype_from_numpy(std::move(dt));
return new Dataset{d, e};
}),
py::arg("dtype"),
py::arg("extent"))
.def(
py::init<Datatype, Extent, std::string>(),
py::arg("dtype"),
py::arg("extent"),
py::arg("options"))
.def(
py::init([](py::dtype dt, Extent e, std::string options) {
auto const d = dtype_from_numpy(std::move(dt));
return new Dataset{d, std::move(e), std::move(options)};
}),
py::arg("dtype"),
py::arg("extent"),
py::arg("options"))

.def(py::init<Datatype, Extent>(), py::arg("dtype"), py::arg("extent"))
.def(py::init<Extent>(), py::arg("extent"))
.def(
py::init([](py::dtype dt, Extent const &e) {
auto const d = dtype_from_numpy(std::move(dt));
return new Dataset{d, e};
}),
py::arg("dtype"),
py::arg("extent"))
.def(
py::init<Datatype, Extent, std::string>(),
py::arg("dtype"),
py::arg("extent"),
py::arg("options"))
.def(
py::init([](py::dtype dt, Extent const &e, std::string options) {
auto const d = dtype_from_numpy(std::move(dt));
return new Dataset{d, e, std::move(options)};
}),
py::arg("dtype"),
py::arg("extent"),
py::arg("options"))

.def(
"__repr__",
[](const Dataset &d) {
std::stringstream stream;
stream << "<openPMD.Dataset of type '" << d.dtype
<< "' and with extent ";
if (d.extent.empty())
{
stream << "[]>";
}
else
{
auto begin = d.extent.begin();
stream << '[' << *begin++;
for (; begin != d.extent.end(); ++begin)
.def(
"__repr__",
[](const Dataset &d) {
std::stringstream stream;
stream << "<openPMD.Dataset of type '" << d.dtype
<< "' and with extent ";
if (d.extent.empty())
{
stream << "[]>";
}
else
{
stream << ", " << *begin;
auto begin = d.extent.begin();
stream << '[' << *begin++;
for (; begin != d.extent.end(); ++begin)
{
stream << ", " << *begin;
}
stream << "]>";
}
stream << "]>";
}
return stream.str();
})
return stream.str();
})

.def_readonly("extent", &Dataset::extent)
.def("extend", &Dataset::extend)
.def_readonly("rank", &Dataset::rank)
.def_property_readonly(
"dtype", [](const Dataset &d) { return dtype_to_numpy(d.dtype); })
.def_readwrite("options", &Dataset::options);
.def_property_readonly(
"joined_dimension", &Dataset::joinedDimension)
.def_readonly("extent", &Dataset::extent)
.def("extend", &Dataset::extend)
.def_readonly("rank", &Dataset::rank)
.def_property_readonly(
"dtype",
[](const Dataset &d) { return dtype_to_numpy(d.dtype); })
.def_readwrite("options", &Dataset::options);
pyDataset.attr("JOINED_DIMENSION") =
py::int_(uint64_t(Dataset::JOINED_DIMENSION));
}

0 comments on commit 8f606c5

Please sign in to comment.