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

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Aug 5, 2024

If the len(...) intrinsic for series and iterations.
Close #1375

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

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
```
@ax3l

This comment was marked as outdated.

@franzpoeschel
Copy link
Contributor

Should we even keep .def("__len__", &Attributable::numAttributes) in src/binding/python/Attributable.cpp at this point? It's only not overwritten for RecordComponents, and it's rather surprising that len would suddenly return the number of attributes there.

@franzpoeschel
Copy link
Contributor

I am surprised the Python tests pass. I would have at least expected these to fail:

diff --git a/test/python/unittest/API/APITest.py b/test/python/unittest/API/APITest.py
index 59e6b5c9..f5bc5557 100644
--- a/test/python/unittest/API/APITest.py
+++ b/test/python/unittest/API/APITest.py
@@ -87,11 +87,12 @@ class APITest(unittest.TestCase):
         series = self.__field_series
 
         self.assertEqual(series.openPMD, "1.0.0")
-        self.assertEqual(len(series.iterations), 1)
+        self.assertEqual(len(series), 6)  # was 1
+        self.assertEqual(len(series.iterations), 6)
         for i in series.iterations:
             self.assertTrue(i in [0, 100, 200, 300, 400, 500])
 
-        self.assertEqual(len(series.iterations), 1)
+        self.assertEqual(len(series.iterations), 6)  # was 1
 
         self.assertTrue(400 in series.iterations)
         i = series.iterations[400]

When I apply this diff locally, the modified tests do fail

For consistency because its only a mixin class.
Write migration guide for breaking change.
Copy link
Contributor

@franzpoeschel franzpoeschel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

@franzpoeschel franzpoeschel merged commit b40607a into openPMD:dev Sep 9, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python: len(series)
2 participants