From cb4b6a187c9639a53592513279b401b3e289dc62 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Tue, 11 Dec 2012 13:36:23 +0000 Subject: [PATCH 1/3] deprecate cube iteration. : test included --- lib/iris/cube.py | 15 +++++++++++++++ lib/iris/tests/test_cdm.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 4473e494e3..8b5ffb1053 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -1240,6 +1240,21 @@ def __str__(self): def __repr__(self): return "" % self.summary(shorten=True, name_padding=1) + def __iter__(self): + """ + Warn about iterating over a cube. + + This is possible (by __getitem__ existence), but currently deprecated. + """ + # Issue deprecation warning. + warnings.warn('Cube iteration has been deprecated: ' + 'please use Cube.slices() instead.', + DeprecationWarning) + + # Return a simple first-index iterator, equivalent to that produced + # with __getitem__, if __iter__ was not defined. + return (self[i] for i in range(self.shape[0])) + def __getitem__(self, keys): """ Cube indexing (through use of square bracket notation) has been implemented at the data level. That is, diff --git a/lib/iris/tests/test_cdm.py b/lib/iris/tests/test_cdm.py index b3001d3209..e29c6d8927 100644 --- a/lib/iris/tests/test_cdm.py +++ b/lib/iris/tests/test_cdm.py @@ -23,6 +23,7 @@ import os import re +import warnings import numpy @@ -503,6 +504,22 @@ def test_ellipsis(self): self.assertCML([self.t[(0, 2), :, Ellipsis]], ('cube_slice', '2d_to_1d_cube_multi_slice3.cml')) +class TestIteration(TestCube2d): + def test_cube_iteration(self): + # Check that creating a cube iterator generates a warning. + with warnings.catch_warnings(record=True) as warnings_list: + warnings.simplefilter('always') + cube_iterator = (subcube for subcube in self.t) + self.assertEqual(len(warnings_list), 1) + self.assertEqual(warnings_list[0].category, DeprecationWarning) + # Check we can step through the items, and their shape and number. + subcubes_shape = self.t.shape[1:] + n_items = 0 + for subcube in cube_iterator: + self.assertEqual(subcube.shape, subcubes_shape) + n_items += 1 + self.assertEqual(n_items, self.t.shape[0]) + class Test2dSlicing(TestCube2d): def test_cube_slice_all_dimensions(self): for cube in self.t.slices(['dim1', 'dim2']): From 040bc411459e2aa741b6d5dd9bce4de59e9239ea Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Fri, 14 Dec 2012 13:25:54 +0000 Subject: [PATCH 2/3] revised cube iteration warning + test : clearer test code : don't use DeprecationWarning --- lib/iris/cube.py | 3 +-- lib/iris/tests/test_cdm.py | 20 +++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 8b5ffb1053..d550ed586c 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -1248,8 +1248,7 @@ def __iter__(self): """ # Issue deprecation warning. warnings.warn('Cube iteration has been deprecated: ' - 'please use Cube.slices() instead.', - DeprecationWarning) + 'please use Cube.slices() instead.') # Return a simple first-index iterator, equivalent to that produced # with __getitem__, if __iter__ was not defined. diff --git a/lib/iris/tests/test_cdm.py b/lib/iris/tests/test_cdm.py index e29c6d8927..8dad778d86 100644 --- a/lib/iris/tests/test_cdm.py +++ b/lib/iris/tests/test_cdm.py @@ -507,18 +507,16 @@ def test_ellipsis(self): class TestIteration(TestCube2d): def test_cube_iteration(self): # Check that creating a cube iterator generates a warning. - with warnings.catch_warnings(record=True) as warnings_list: - warnings.simplefilter('always') - cube_iterator = (subcube for subcube in self.t) - self.assertEqual(len(warnings_list), 1) - self.assertEqual(warnings_list[0].category, DeprecationWarning) + with warnings.catch_warnings(): + warnings.simplefilter('error') + with self.assertRaises(UserWarning): + for subcube in self.t: # warning->error, so this *fails* + pass # Check we can step through the items, and their shape and number. - subcubes_shape = self.t.shape[1:] - n_items = 0 - for subcube in cube_iterator: - self.assertEqual(subcube.shape, subcubes_shape) - n_items += 1 - self.assertEqual(n_items, self.t.shape[0]) + subcubes = [subcube for subcube in self.t] + self.assertEqual(len(subcubes), self.t.shape[0]) + for subcube in subcubes: + self.assertEqual(subcube.shape, self.t.shape[1:]) class Test2dSlicing(TestCube2d): def test_cube_slice_all_dimensions(self): From 65142ec66238e797b022e0ea80f216c16cc0e93e Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Fri, 18 Jan 2013 15:58:13 +0000 Subject: [PATCH 3/3] Tidy cube.__iter__ docstring. --- lib/iris/cube.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index d550ed586c..3b9aa1a0e4 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -1241,12 +1241,8 @@ def __repr__(self): return "" % self.summary(shorten=True, name_padding=1) def __iter__(self): - """ - Warn about iterating over a cube. - - This is possible (by __getitem__ existence), but currently deprecated. - """ - # Issue deprecation warning. + # Emit a warning about iterating over the cube. + # __getitem__ makes this possible, but now deprecated as confusing. warnings.warn('Cube iteration has been deprecated: ' 'please use Cube.slices() instead.')