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

Fix bug in certain cyclic subsets #782

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ version NEXTRELEASE
* Fix bug where `cf.example_fields` returned a `list`
of Fields rather than a `Fieldlist`
(https://github.com/NCAS-CMS/cf-python/issues/725)
* Fix bug where `cf.functions.normalize_slice` doesn't correctly
mattjbr123 marked this conversation as resolved.
Show resolved Hide resolved
handle certain cyclic slices
(https://github.com/NCAS-CMS/cf-python/issues/774)

----

Expand Down
4 changes: 2 additions & 2 deletions cf/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,8 +2132,8 @@ def normalize_slice(index, size, cyclic=False):
return slice(start, stop, step)

if not (
(step > 0 and start < 0 and stop > 0)
or (step < 0 and start > 0 and stop < 0)
(step > 0 and start < 0 and stop >= 0)
or (step < 0 and start >= 0 and stop < 0)
):
raise IndexError(
f"{index!r} is not a {'cyclic ' if cyclic else ''}slice"
Expand Down
19 changes: 18 additions & 1 deletion cf/test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,24 @@ def test_normalize_slice(self):
cf.normalize_slice(slice(2, 5, -2), 8, cyclic=True),
slice(2, -3, -2),
)


self.assertEqual(
cf.normalize_slice(slice(-8, 0, 1), 8, cyclic=True),
slice(-8, 0, 1)
)
self.assertEqual(
cf.normalize_slice(slice(0, 7, -1), 8, cyclic=True),
slice(0, -1, -1)
)
self.assertEqual(
cf.normalize_slice(slice(-1, -8, 1), 8, cyclic=True),
slice(-1, 0, 1)
)
self.assertEqual(
cf.normalize_slice(slice(-8, -1, -1), 8, cyclic=True),
slice(0, -1, -1)
)

with self.assertRaises(IndexError):
cf.normalize_slice([1, 2], 8)

Expand Down
Loading