From 388d441730cbd33c161898f3517520afa13ef159 Mon Sep 17 00:00:00 2001 From: Bill Little Date: Wed, 13 Dec 2023 16:37:50 +0000 Subject: [PATCH] refactor --- .../plot_projections_and_annotations.py | 5 +++-- lib/iris/analysis/calculus.py | 10 ++++++---- lib/iris/analysis/cartography.py | 13 ++++++------ lib/iris/aux_factory.py | 20 ++++++++----------- lib/iris/fileformats/_ff.py | 10 ++++++---- lib/iris/fileformats/netcdf/saver.py | 5 ++--- lib/iris/fileformats/nimrod_load_rules.py | 4 +--- lib/iris/fileformats/pp.py | 5 ++--- lib/iris/fileformats/rules.py | 8 ++------ lib/iris/plot.py | 5 +++-- .../tests/unit/fileformats/ff/test_FF2PP.py | 4 +--- .../name_loaders/test__build_cell_methods.py | 10 ++++++---- .../test_optimal_array_structure.py | 10 ++++++---- 13 files changed, 53 insertions(+), 56 deletions(-) diff --git a/docs/gallery_code/general/plot_projections_and_annotations.py b/docs/gallery_code/general/plot_projections_and_annotations.py index a5a5267f1c..76c7206748 100644 --- a/docs/gallery_code/general/plot_projections_and_annotations.py +++ b/docs/gallery_code/general/plot_projections_and_annotations.py @@ -123,8 +123,9 @@ def make_plot(projection_name, projection_crs): # Add a title, and display. plt.title( - "A pseudocolour plot on the {} projection,\n" - "with overlaid contours.".format(projection_name) + "A pseudocolour plot on the {} projection,\nwith overlaid contours.".format( + projection_name + ) ) iplt.show() diff --git a/lib/iris/analysis/calculus.py b/lib/iris/analysis/calculus.py index 220999ca55..ee5d402b89 100644 --- a/lib/iris/analysis/calculus.py +++ b/lib/iris/analysis/calculus.py @@ -167,8 +167,9 @@ def cube_delta(cube, coord): coord.shape[0] == 1 and not getattr(coord, "circular", False) ) or not delta_dims: raise ValueError( - "Cannot calculate delta over {!r} as it has " - "length of 1.".format(coord.name()) + "Cannot calculate delta over {!r} as it has length of 1.".format( + coord.name() + ) ) delta_dim = delta_dims[0] @@ -570,8 +571,9 @@ def curl(i_cube, j_cube, k_cube=None): bad_coords = coord_comparison["resamplable"] if bad_coords: raise ValueError( - "Some coordinates are different ({}), consider " - "resampling.".format(", ".join(group.name() for group in bad_coords)) + "Some coordinates are different ({}), consider resampling.".format( + ", ".join(group.name() for group in bad_coords) + ) ) # Get the dim_coord, or None if none exist, for the xyz dimensions diff --git a/lib/iris/analysis/cartography.py b/lib/iris/analysis/cartography.py index 03de657601..bcdc1a6b21 100644 --- a/lib/iris/analysis/cartography.py +++ b/lib/iris/analysis/cartography.py @@ -414,8 +414,9 @@ def area_weights(cube, normalize=False): lon, lat = _get_lon_lat_coords(cube) except IndexError: raise ValueError( - "Cannot get latitude/longitude " - "coordinates from cube {!r}.".format(cube.name()) + "Cannot get latitude/longitude coordinates from cube {!r}.".format( + cube.name() + ) ) if lat.ndim > 1: @@ -631,8 +632,9 @@ def project(cube, target_proj, nx=None, ny=None): lon_coord, lat_coord = _get_lon_lat_coords(cube) except IndexError: raise ValueError( - "Cannot get latitude/longitude " - "coordinates from cube {!r}.".format(cube.name()) + "Cannot get latitude/longitude coordinates from cube {!r}.".format( + cube.name() + ) ) if lat_coord.coord_system != lon_coord.coord_system: @@ -840,8 +842,7 @@ def project(cube, target_proj, nx=None, ny=None): discarded_coords = coords_to_ignore.difference([lat_coord, lon_coord]) if discarded_coords: warnings.warn( - "Discarding coordinates that share dimensions with " - "{} and {}: {}".format( + "Discarding coordinates that share dimensions with {} and {}: {}".format( lat_coord.name(), lon_coord.name(), [coord.name() for coord in discarded_coords], diff --git a/lib/iris/aux_factory.py b/lib/iris/aux_factory.py index 819dd7c17d..e5a704bb56 100644 --- a/lib/iris/aux_factory.py +++ b/lib/iris/aux_factory.py @@ -994,9 +994,8 @@ def _check_dependencies(sigma, eta, depth, depth_c, nsigma, zlev): sigma.units = cf_units.Unit("1") if sigma is not None and not sigma.units.is_dimensionless(): - msg = ( - "Invalid units: sigma coordinate {!r} " - "must be dimensionless.".format(sigma.name()) + msg = "Invalid units: sigma coordinate {!r} must be dimensionless.".format( + sigma.name() ) raise ValueError(msg) @@ -1198,9 +1197,8 @@ def _check_dependencies(sigma, eta, depth): # Check bounds and shape. coord, term = (sigma, "sigma") if coord is not None and coord.nbounds not in (0, 2): - msg = ( - "Invalid {} coordinate {!r}: must have either " - "0 or 2 bounds.".format(term, coord.name()) + msg = "Invalid {} coordinate {!r}: must have either 0 or 2 bounds.".format( + term, coord.name() ) raise ValueError(msg) @@ -1219,9 +1217,8 @@ def _check_dependencies(sigma, eta, depth): sigma.units = cf_units.Unit("1") if sigma is not None and not sigma.units.is_dimensionless(): - msg = ( - "Invalid units: sigma coordinate {!r} " - "must be dimensionless.".format(sigma.name()) + msg = "Invalid units: sigma coordinate {!r} must be dimensionless.".format( + sigma.name() ) raise ValueError(msg) @@ -1550,9 +1547,8 @@ def _check_dependencies(s, eta, depth, a, b, depth_c): # Check bounds and shape. if s is not None and s.nbounds not in (0, 2): - msg = ( - "Invalid s coordinate {!r}: must have either " - "0 or 2 bounds.".format(s.name()) + msg = "Invalid s coordinate {!r}: must have either 0 or 2 bounds.".format( + s.name() ) raise ValueError(msg) diff --git a/lib/iris/fileformats/_ff.py b/lib/iris/fileformats/_ff.py index 741ca626a9..5c32c23757 100644 --- a/lib/iris/fileformats/_ff.py +++ b/lib/iris/fileformats/_ff.py @@ -585,8 +585,9 @@ def _adjust_field_for_lbc(self, field): lbtim_default = 11 if field.lbtim not in (0, lbtim_default): raise ValueError( - "LBC field has LBTIM of {:d}, expected only " - "0 or {:d}.".format(field.lbtim, lbtim_default) + "LBC field has LBTIM of {:d}, expected only 0 or {:d}.".format( + field.lbtim, lbtim_default + ) ) field.lbtim = lbtim_default @@ -595,8 +596,9 @@ def _adjust_field_for_lbc(self, field): lbvc_default = 65 if field.lbvc not in (0, lbvc_default): raise ValueError( - "LBC field has LBVC of {:d}, expected only " - "0 or {:d}.".format(field.lbvc, lbvc_default) + "LBC field has LBVC of {:d}, expected only 0 or {:d}.".format( + field.lbvc, lbvc_default + ) ) field.lbvc = lbvc_default # Specifying a vertical encoding scheme means a usable vertical diff --git a/lib/iris/fileformats/netcdf/saver.py b/lib/iris/fileformats/netcdf/saver.py index e7e4ec615b..b35b85bbae 100644 --- a/lib/iris/fileformats/netcdf/saver.py +++ b/lib/iris/fileformats/netcdf/saver.py @@ -1130,9 +1130,8 @@ def _add_aux_factories(self, cube, cf_var_cube, dimension_names): for factory in cube.aux_factories: factory_defn = _FACTORY_DEFNS.get(type(factory), None) if factory_defn is None: - msg = ( - "Unable to determine formula terms " - "for AuxFactory: {!r}".format(factory) + msg = "Unable to determine formula terms for AuxFactory: {!r}".format( + factory ) warnings.warn(msg, category=iris.exceptions.IrisSaveWarning) else: diff --git a/lib/iris/fileformats/nimrod_load_rules.py b/lib/iris/fileformats/nimrod_load_rules.py index 224e241ddc..d72571fff9 100644 --- a/lib/iris/fileformats/nimrod_load_rules.py +++ b/lib/iris/fileformats/nimrod_load_rules.py @@ -440,9 +440,7 @@ def horizontal_grid(cube, field, handle_metadata_errors): y_coord_name = "latitude" else: raise TranslationError( - "Horizontal grid type {} not implemented".format( - field.horizontal_grid_type - ) + "Horizontal grid type {} not implemented".format(field.horizontal_grid_type) ) points = np.linspace( field.x_origin, diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 85587f86c8..e70d4cbf52 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -1334,9 +1334,8 @@ def save(self, file_handle): elif lbpack == 1: pp_file.write(packed_data) else: - msg = ( - "Writing packed pp data with lbpack of {} " - "is not supported.".format(lbpack) + msg = "Writing packed pp data with lbpack of {} is not supported.".format( + lbpack ) raise NotImplementedError(msg) diff --git a/lib/iris/fileformats/rules.py b/lib/iris/fileformats/rules.py index 546378bdff..4dbaa8bb51 100644 --- a/lib/iris/fileformats/rules.py +++ b/lib/iris/fileformats/rules.py @@ -127,15 +127,11 @@ def aux_factory(cube, aux_factory_class): ] if not aux_factories: raise ValueError( - "Cube does not have an aux factory of type {!r}.".format( - aux_factory_class - ) + "Cube does not have an aux factory of type {!r}.".format(aux_factory_class) ) elif len(aux_factories) > 1: raise ValueError( - "Cube has more than one aux factory of type {!r}.".format( - aux_factory_class - ) + "Cube has more than one aux factory of type {!r}.".format(aux_factory_class) ) return aux_factories[0] diff --git a/lib/iris/plot.py b/lib/iris/plot.py index b432ed7620..2dbadab2c9 100644 --- a/lib/iris/plot.py +++ b/lib/iris/plot.py @@ -104,8 +104,9 @@ def get_span(coord): total_span = set().union(*spans) if len(total_span) != ndims: raise ValueError( - "The given coordinates ({}) don't span the {} data" - " dimensions.".format(names(coords), ndims) + "The given coordinates ({}) don't span the {} data dimensions.".format( + names(coords), ndims + ) ) # If we have 2-dimensional data, and one or more 1-dimensional diff --git a/lib/iris/tests/unit/fileformats/ff/test_FF2PP.py b/lib/iris/tests/unit/fileformats/ff/test_FF2PP.py index c06d8db85a..da3ac01f66 100644 --- a/lib/iris/tests/unit/fileformats/ff/test_FF2PP.py +++ b/lib/iris/tests/unit/fileformats/ff/test_FF2PP.py @@ -91,9 +91,7 @@ def mock_for_extract_field(self, fields, x=None, y=None): "struct.unpack_from", return_value=[4] ), mock.patch( "iris.fileformats.pp.make_pp_field", side_effect=fields - ), mock.patch( - "iris.fileformats._ff.FF2PP._payload", return_value=(0, 0) - ): + ), mock.patch("iris.fileformats._ff.FF2PP._payload", return_value=(0, 0)): yield ff2pp def _mock_lbc(self, **kwargs): diff --git a/lib/iris/tests/unit/fileformats/name_loaders/test__build_cell_methods.py b/lib/iris/tests/unit/fileformats/name_loaders/test__build_cell_methods.py index 2ebde5782f..26e6208db1 100644 --- a/lib/iris/tests/unit/fileformats/name_loaders/test__build_cell_methods.py +++ b/lib/iris/tests/unit/fileformats/name_loaders/test__build_cell_methods.py @@ -101,8 +101,9 @@ def test_unrecognised(self): with mock.patch("warnings.warn") as warn: _ = _build_cell_methods(av_or_int, coord_name) expected_msg = ( - "Unknown {} statistic: {!r}. Unable to " - "create cell method.".format(coord_name, unrecognised_heading) + "Unknown {} statistic: {!r}. Unable to create cell method.".format( + coord_name, unrecognised_heading + ) ) warn.assert_called_with(expected_msg, category=IrisLoadWarning) @@ -126,8 +127,9 @@ def test_unrecognised_similar_to_no_averaging(self): with mock.patch("warnings.warn") as warn: _ = _build_cell_methods(av_or_int, coord_name) expected_msg = ( - "Unknown {} statistic: {!r}. Unable to " - "create cell method.".format(coord_name, unrecognised_heading) + "Unknown {} statistic: {!r}. Unable to create cell method.".format( + coord_name, unrecognised_heading + ) ) warn.assert_called_with(expected_msg, category=IrisLoadWarning) diff --git a/lib/iris/tests/unit/fileformats/um/optimal_array_structuring/test_optimal_array_structure.py b/lib/iris/tests/unit/fileformats/um/optimal_array_structuring/test_optimal_array_structure.py index cf545e89d5..63f54fd356 100644 --- a/lib/iris/tests/unit/fileformats/um/optimal_array_structuring/test_optimal_array_structure.py +++ b/lib/iris/tests/unit/fileformats/um/optimal_array_structuring/test_optimal_array_structure.py @@ -30,14 +30,16 @@ def _check_arrays_and_dims(self, result, spec): self.assertEqual( result_dims, spec_dims, - 'element dims differ for "{}": ' - "result={!r}, expected {!r}".format(keyname, result_dims, spec_dims), + 'element dims differ for "{}": ' "result={!r}, expected {!r}".format( + keyname, result_dims, spec_dims + ), ) self.assertArrayEqual( result_array, spec_array, - 'element arrays differ for "{}": ' - "result={!r}, expected {!r}".format(keyname, result_array, spec_array), + 'element arrays differ for "{}": ' "result={!r}, expected {!r}".format( + keyname, result_array, spec_array + ), ) def test_none(self):