Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Series/Iteration: Fix Length #1659

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Note that ADIOS2 does not support compression in BP3 files.

pybind11 2.12.0 is now the minimally supported version for Python support.

The ``len(...)`` of many classes has been reworked for consistency and returns now the number of entries (iterations, record components, etc.).
Previously, this sporadically returned the number of attributes, which is better queried via ``len(<object>.attributes)``.


0.15.0
------
Expand Down
6 changes: 6 additions & 0 deletions include/openPMD/binding/python/Container.H
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ declare_container(py::handle scope, std::string const &name)
// keep container alive while iterator exists
py::keep_alive<0, 1>());

// overwrite to avoid that the __len__ of Attributable is used
cl.def(
"__len__",
[](const Map &m) { return m.size(); },
"Number of elements in the container to iterate.");

cl.def("__repr__", [name](Map const &m) {
std::stringstream stream;
stream << "<openPMD." << name << " with ";
Expand Down
2 changes: 0 additions & 2 deletions src/binding/python/Attributable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,6 @@ void init_Attributable(py::module &m)
.def("delete_attribute", &Attributable::deleteAttribute)
.def("contains_attribute", &Attributable::containsAttribute)

.def("__len__", &Attributable::numAttributes)

// @todo _ipython_key_completions_ if we find a way to add a []
// interface

Expand Down
1 change: 1 addition & 0 deletions src/binding/python/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ For further details, refer to the non-MPI overload.
)END")
#endif
.def("__bool__", &Series::operator bool)
.def("__len__", [](Series const &s) { return s.iterations.size(); })
.def(
"__repr__",
[](Series const &s) {
Expand Down
Loading