Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle committed Dec 13, 2023
1 parent 03039f1 commit 388d441
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 56 deletions.
5 changes: 3 additions & 2 deletions docs/gallery_code/general/plot_projections_and_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 6 additions & 4 deletions lib/iris/analysis/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions lib/iris/analysis/cartography.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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],
Expand Down
20 changes: 8 additions & 12 deletions lib/iris/aux_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions lib/iris/fileformats/_ff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/iris/fileformats/netcdf/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions lib/iris/fileformats/nimrod_load_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/iris/fileformats/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions lib/iris/fileformats/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
5 changes: 3 additions & 2 deletions lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/iris/tests/unit/fileformats/ff/test_FF2PP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 388d441

Please sign in to comment.