From dab0d71694e66743e03e1b26ef7d7e0eee2266e0 Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Tue, 31 Aug 2021 21:58:36 +1200 Subject: [PATCH 1/2] refactor(exceptions): reduce redundancy, simplify message generation --- flopy/mf6/coordinates/modelgrid.py | 3 -- flopy/mf6/mfbase.py | 48 ++++++++---------------------- flopy/plot/plotutil.py | 3 +- 3 files changed, 14 insertions(+), 40 deletions(-) diff --git a/flopy/mf6/coordinates/modelgrid.py b/flopy/mf6/coordinates/modelgrid.py index da9867dda8..21e0e8961e 100644 --- a/flopy/mf6/coordinates/modelgrid.py +++ b/flopy/mf6/coordinates/modelgrid.py @@ -8,9 +8,6 @@ class MFGridException(Exception): Model grid related exception """ - def __init__(self, error): - Exception.__init__(self, f"MFGridException: {error}") - class ModelCell: """ diff --git a/flopy/mf6/mfbase.py b/flopy/mf6/mfbase.py index 9f87820f09..e5de5b2a6f 100644 --- a/flopy/mf6/mfbase.py +++ b/flopy/mf6/mfbase.py @@ -13,11 +13,6 @@ class MFInvalidTransientBlockHeaderException(Exception): Exception occurs when parsing a transient block header """ - def __init__(self, error): - Exception.__init__( - self, f"MFInvalidTransientBlockHeaderException: {error}" - ) - class ReadAsArraysException(Exception): """ @@ -25,9 +20,6 @@ class ReadAsArraysException(Exception): package. """ - def __init__(self, error): - Exception.__init__(self, f"ReadAsArraysException: {error}") - # external exceptions for users class FlopyException(Exception): @@ -37,7 +29,7 @@ class FlopyException(Exception): def __init__(self, error, location=""): self.message = error - Exception.__init__(self, f"FlopyException: {error} ({location})") + super().__init__(f"{error} ({location})") class StructException(Exception): @@ -47,7 +39,7 @@ class StructException(Exception): def __init__(self, error, location): self.message = error - Exception.__init__(self, f"StructException: {error} ({location})") + super().__init__(f"{error} ({location})") class MFDataException(Exception): @@ -132,35 +124,21 @@ def __init__( self.org_type, self.org_value, self.org_traceback ) # build error string - error_message_0 = "An error occurred in " + error_message = "An error occurred in " if self.data_element is not None and self.data_element != "": - error_message_1 = f'data element "{self.data_element}" ' - else: - error_message_1 = "" + error_message += f'data element "{self.data_element}" ' if self.model is not None and self.model != "": - error_message_2 = f'model "{self.model}" ' - else: - error_message_2 = "" - error_message_3 = f'package "{self.package}".' - error_message_4 = ( - ' The error occurred while {} in the "{}" method' - ".".format(self.current_process, self.method_caught_in) + error_message += f'model "{self.model}" ' + error_message += ( + f'package "{self.package}". The error occurred while ' + f'{self.current_process} in the "{self.method_caught_in}" method.' ) if len(self.messages) > 0: - error_message_5 = "\nAdditional Information:\n" - for index, message in enumerate(self.messages): - error_message_5 = f"{error_message_5}({index + 1}) {message}\n" - else: - error_message_5 = "" - error_message = "{}{}{}{}{}{}".format( - error_message_0, - error_message_1, - error_message_2, - error_message_3, - error_message_4, - error_message_5, - ) - Exception.__init__(self, error_message) + error_message += "\nAdditional Information:\n" + error_message += "\n".join( + f"({idx}) {msg}" for (idx, msg) in enumerate(self.messages, 1) + ) + super().__init__(error_message) class VerbosityLevel(Enum): diff --git a/flopy/plot/plotutil.py b/flopy/plot/plotutil.py index d5a27acc64..443e87aab6 100644 --- a/flopy/plot/plotutil.py +++ b/flopy/plot/plotutil.py @@ -39,8 +39,7 @@ class PlotException(Exception): - def __init__(self, message): - super().__init__(message) + pass class PlotUtilities: From 30f7e18e3b62bb4d34ffdbf4d2ef31ade8b657d8 Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Thu, 2 Sep 2021 14:34:06 +1200 Subject: [PATCH 2/2] Remove PlotException, replace with KeyError and ImportError --- flopy/plot/plotutil.py | 44 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/flopy/plot/plotutil.py b/flopy/plot/plotutil.py index 443e87aab6..8522e6e50c 100644 --- a/flopy/plot/plotutil.py +++ b/flopy/plot/plotutil.py @@ -38,10 +38,6 @@ } -class PlotException(Exception): - pass - - class PlotUtilities: """ Class which groups a collection of plotting utilities @@ -661,13 +657,10 @@ def _plot_mflist_helper( try: arr = arr_dict[key] - except: - err_msg = "Cannot find key to plot\n" - err_msg += f" Provided key={key}\n Available keys=" - for name, arr in arr_dict.items(): - err_msg += f"{name}, " - err_msg += "\n" - raise PlotException(err_msg) + except KeyError: + err_msg = f'Cannot find key "{key}" to plot\n Available keys=' + err_msg += ", ".join(str(k) for k in arr_dict.keys()) + raise KeyError(err_msg) axes = PlotUtilities._plot_array_helper( arr, @@ -1118,7 +1111,7 @@ def _plot_array_helper( # check that matplotlib is installed if plt is None: - raise PlotException( + raise ImportError( "Could not import matplotlib. Must install matplotlib " "in order to plot LayerFile data." ) @@ -1269,7 +1262,7 @@ def _plot_bc_helper( from .map import PlotMapView if plt is None: - raise PlotException( + raise ImportError( "Could not import matplotlib. Must install matplotlib " "in order to plot boundary condition data." ) @@ -2030,8 +2023,10 @@ def shapefile_extents(shp): """ if shapefile is None: - s = "Could not import shapefile. Must install pyshp in order to plot shapefiles." - raise PlotException(s) + raise ImportError( + "Could not import shapefile. " + "Must install pyshp in order to plot shapefiles." + ) sf = shapefile.Reader(shp) shapes = sf.shapes() @@ -2072,8 +2067,10 @@ def shapefile_get_vertices(shp): """ if shapefile is None: - s = "Could not import shapefile. Must install pyshp in order to plot shapefiles." - raise PlotException(s) + raise ImportError( + "Could not import shapefile. " + "Must install pyshp in order to plot shapefiles." + ) sf = shapefile.Reader(shp) shapes = sf.shapes() @@ -2123,9 +2120,9 @@ def shapefile_to_patch_collection(shp, radius=500.0, idx=None): """ if shapefile is None: - raise PlotException( - "Could not import shapefile. Must install pyshp " - "in order to plot shapefiles." + raise ImportError( + "Could not import shapefile. " + "Must install pyshp in order to plot shapefiles." ) if plt is None: raise ImportError( @@ -2271,11 +2268,10 @@ def plot_shapefile( """ if shapefile is None: - s = ( - "Could not import shapefile. Must install pyshp in " - "order to plot shapefiles." + raise ImportError( + "Could not import shapefile. " + "Must install pyshp in order to plot shapefiles." ) - raise PlotException(s) vmin = kwargs.pop("vmin", None) vmax = kwargs.pop("vmax", None)