From bd25038da73bc4a01cd03efc5a806ef2fda96371 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Mon, 5 Aug 2024 11:58:44 -0700 Subject: [PATCH 1/2] 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 ``` --- include/openPMD/binding/python/Container.H | 6 ++++++ src/binding/python/Series.cpp | 1 + 2 files changed, 7 insertions(+) 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 << " Date: Thu, 5 Sep 2024 10:34:02 -0700 Subject: [PATCH 2/2] Python: Remove `Attributable.__len__` For consistency because its only a mixin class. Write migration guide for breaking change. --- NEWS.rst | 3 +++ src/binding/python/Attributable.cpp | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 89089391a4..5f95465cff 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -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(.attributes)``. + 0.15.0 ------ diff --git a/src/binding/python/Attributable.cpp b/src/binding/python/Attributable.cpp index 2383ceab50..806a57b666 100644 --- a/src/binding/python/Attributable.cpp +++ b/src/binding/python/Attributable.cpp @@ -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