Skip to content

Commit

Permalink
Python Series/Iteration: Fix Length (#1659)
Browse files Browse the repository at this point in the history
* Python Series/Iteration: Fix Length

If the `len(...)` intrinsic for series and iterations.

```py
import openpmd_api as io

s = io.Series("build/samples/git-sample/3d-bp4/example-3d-bp4_%T.bp", io.Access.read_only)

s
<openPMD.Series at 'build/samples/git-sample/3d-bp4/example-3d-bp4_%T.bp' with 2 iteration(s) and 10 attributes>

s.iterations
<openPMD.Iteration_Container with 2 entries and 0 attribute(s)>

len(s)
2

len(s.iterations)
2
```

* Python: Remove `Attributable.__len__`

For consistency because its only a mixin class.
Write migration guide for breaking change.
  • Loading branch information
ax3l authored Sep 9, 2024
1 parent dc58cc5 commit b40607a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Note that ADIOS2 does not support compression in BP3 files.
CMake 3.22.0 is now the minimally supported version for CMake.
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

0 comments on commit b40607a

Please sign in to comment.