From b40607a1a853b56647e77ba29c833021384f72ef Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Mon, 9 Sep 2024 01:12:59 -0700 Subject: [PATCH] Python Series/Iteration: Fix Length (#1659) * 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 s.iterations 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. --- NEWS.rst | 3 +++ include/openPMD/binding/python/Container.H | 6 ++++++ src/binding/python/Attributable.cpp | 2 -- src/binding/python/Series.cpp | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index eaff473149..9a6274dcb5 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -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(.attributes)``. + 0.15.0 ------ diff --git a/include/openPMD/binding/python/Container.H b/include/openPMD/binding/python/Container.H index a07847e600..cb42c89c00 100644 --- a/include/openPMD/binding/python/Container.H +++ b/include/openPMD/binding/python/Container.H @@ -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 << "