From 7116b1d4fe03bde4858e2476ec674de8abda71a1 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 7 Jun 2024 17:57:27 +0100 Subject: [PATCH 1/4] Modified normalize_slice in cf/functions.py to fix #774 --- Changelog.rst | 3 +++ cf/functions.py | 4 ++-- cf/test/test_functions.py | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index 6e879831f3..621978ebb4 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -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 + handle certain cyclic slices + (https://github.com/NCAS-CMS/cf-python/issues/774) ---- diff --git a/cf/functions.py b/cf/functions.py index e1d0d1d926..d186774d02 100644 --- a/cf/functions.py +++ b/cf/functions.py @@ -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" diff --git a/cf/test/test_functions.py b/cf/test/test_functions.py index 88cdf195e3..79fe13a3de 100644 --- a/cf/test/test_functions.py +++ b/cf/test/test_functions.py @@ -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, 7, -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) From d4cb72b6af6960249d43e6adfcae0f0608b0052c Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 7 Jun 2024 18:32:13 +0100 Subject: [PATCH 2/4] bug in prev commit in test_functions #774 --- cf/test/test_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf/test/test_functions.py b/cf/test/test_functions.py index 79fe13a3de..370d0a9036 100644 --- a/cf/test/test_functions.py +++ b/cf/test/test_functions.py @@ -396,7 +396,7 @@ def test_normalize_slice(self): ) self.assertEqual( cf.normalize_slice(slice(0, 7, -1), 8, cyclic=True), - slice(0, 7, -1) + slice(0, -1, -1) ) self.assertEqual( cf.normalize_slice(slice(-1, -8, 1), 8, cyclic=True), From ca952eb3f218aa25df2a37e21da20b4de437d3df Mon Sep 17 00:00:00 2001 From: Matt Brown <47819526+mattjbr123@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:20:34 +0100 Subject: [PATCH 3/4] Update Changelog.rst Co-authored-by: David Hassell --- Changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changelog.rst b/Changelog.rst index 621978ebb4..522824de8a 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -7,7 +7,7 @@ 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 +* Fix bug where `cf.normalize_slice` doesn't correctly handle certain cyclic slices (https://github.com/NCAS-CMS/cf-python/issues/774) From 4411ede69a405ee4ce17b392edd32b1ae431ef41 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 11 Jun 2024 11:45:16 +0100 Subject: [PATCH 4/4] Added name to contributors --- docs/source/contributing.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/contributing.rst b/docs/source/contributing.rst index def33598e8..e8ec845059 100644 --- a/docs/source/contributing.rst +++ b/docs/source/contributing.rst @@ -126,3 +126,4 @@ ideas, code, and documentation to the cf library: * Sadie Bartholomew * Thibault Hallouin * Tim Bradshaw +* Matt Brown