From 3c65d9f48225eb27caf77d093a813c38ec9bdebc Mon Sep 17 00:00:00 2001 From: shahmustafa54 Date: Sun, 2 May 2021 22:22:03 +0500 Subject: [PATCH 1/3] Assert replaced with IFs and Raises --- adafruit_progressbar/__init__.py | 77 +++++++++++++++-------------- adafruit_progressbar/progressbar.py | 3 +- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/adafruit_progressbar/__init__.py b/adafruit_progressbar/__init__.py index e833f27..88f01fe 100755 --- a/adafruit_progressbar/__init__.py +++ b/adafruit_progressbar/__init__.py @@ -76,29 +76,28 @@ def __init__( value_range: Union[Tuple[int, int], Tuple[float, float]] = (0, 100), ) -> None: - assert ( - value_range[0] < value_range[1] - ), "The minimum value must be less than the maximum value" + if not (value_range[0] < value_range[1]): + raise ValueError("The minimum value must be less than the maximum value") - assert ( - size[0] > 0 and size[1] > 0 - ), "The width and the height must be greater than zero" + if not (size[0] > 0 and size[1] > 0): + raise ValueError("The width and the height must be greater than zero") - assert ( - value_range[0] <= value <= value_range[1] - ), "The starting value must be within the range of minimum to maximum" + if not (value_range[0] <= value <= value_range[1]): + raise ValueError("The starting value must be within the range of minimum to maximum") _edge_size = 2 * margin_size + 2 * border_thickness - assert _edge_size < size[0], ( - "The size of the borders and margins combined must be " - "less than the width of the widget" - ) + if not (_edge_size < size[0]): + raise ValueError( + "The size of the borders and margins combined must be " + "less than the width of the widget" + ) - assert _edge_size < size[1], ( - "The size of the borders and margins combined must be " - "less than the height of the widget" - ) + if not (_edge_size < size[1]): + raise ValueError( + "The size of the borders and margins combined must be " + "less than the height of the widget" + ) self._progress = 0.0 self._widget_size = size @@ -201,9 +200,8 @@ def border_color(self, color: Union[int, Tuple[int, int, int]]) -> None: :rtype: None """ - assert ( - isinstance(color, int) or color is None - ), "A color must be represented by a integer value" + if not (isinstance(color, int) or color is None): + raise TypeError("A color must be represented by a integer value") self._border_color = color @@ -285,13 +283,11 @@ def value(self, value: Union[int, float]) -> None: :rtype: None """ - assert isinstance( - value, (int, float) - ), "The value to set must be either an integer or a float" + if not (isinstance(value, (int, float))): + raise TypeError("The value to set must be either an integer or a float") - assert ( - self.minimum <= value <= self.maximum - ), f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})" + if not (self.minimum <= value <= self.maximum): + raise ValueError(f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})") # Save off the previous value, so we can pass it in the # call to "Render" @@ -332,9 +328,11 @@ def progress(self, value: float) -> None: :rtype: None """ - assert [isinstance(value, (float, int)), "'progress' must be an int or a float"] + if not (isinstance(value, (float, int))): + raise TypeError("'progress' must be an int or a float") - assert 0.0 <= value <= 100.0, "'progress' must be between 0 and 100" + if not (0.0 <= value <= 100.0): + raise ValueError("'progress' must be between 0 and 100") self.value = (self.minimum + (self.maximum - self.minimum)) * (value * 0.01) @@ -449,19 +447,22 @@ def margin_size(self, value: int) -> None: :rtype: None """ - assert isinstance(value, int), "The margin size must be an integer" + if not (isinstance(value, int)): + raise TypeError("The margin size must be an integer") margin_spacing = (2 * value) + (2 * self._border_thickness) - assert margin_spacing < self.widget_width, ( - "The size of the borders and margins combined can total the same or more" - "than the widget's width." - ) - - assert margin_spacing < self.widget_height, ( - "The size of the borders and margins combined can total the same or more" - "than the widget's height." - ) + if not (margin_spacing < self.widget_width): + raise ValueError( + "The size of the borders and margins combined can total the same or more" + "than the widget's width." + ) + + if not (margin_spacing < self.widget_height): + raise ValueError( + "The size of the borders and margins combined can total the same or more" + "than the widget's height." + ) self._margin_size = value self._set_progress(self._progress) # For a render pass diff --git a/adafruit_progressbar/progressbar.py b/adafruit_progressbar/progressbar.py index 5f1eeb3..83fd11b 100755 --- a/adafruit_progressbar/progressbar.py +++ b/adafruit_progressbar/progressbar.py @@ -65,7 +65,8 @@ def __init__( # This needs to remain for backward compatibility, the default ProgressBar class # should only be able to handle values of type "float" - assert isinstance(progress, float), "Progress must be a floating point value." + if not (isinstance(progress, float)): + raise TypeError("Progress must be a floating point value.") super().__init__( (x, y), From b286a2ae1262e4785ff12cdf3c7467b5e8cee010 Mon Sep 17 00:00:00 2001 From: shahmustafa54 Date: Thu, 6 May 2021 03:55:02 +0500 Subject: [PATCH 2/3] Replaced Asserts with Ifs and Raises --- adafruit_progressbar/__init__.py | 24 ++++++++++++------------ adafruit_progressbar/progressbar.py | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/adafruit_progressbar/__init__.py b/adafruit_progressbar/__init__.py index 88f01fe..905ba72 100755 --- a/adafruit_progressbar/__init__.py +++ b/adafruit_progressbar/__init__.py @@ -76,24 +76,24 @@ def __init__( value_range: Union[Tuple[int, int], Tuple[float, float]] = (0, 100), ) -> None: - if not (value_range[0] < value_range[1]): + if value_range[0] >= value_range[1]: raise ValueError("The minimum value must be less than the maximum value") - if not (size[0] > 0 and size[1] > 0): + if size[0] <= 0 or size[1] <= 0: raise ValueError("The width and the height must be greater than zero") - if not (value_range[0] <= value <= value_range[1]): + if not value_range[0] <= value <= value_range[1]: raise ValueError("The starting value must be within the range of minimum to maximum") _edge_size = 2 * margin_size + 2 * border_thickness - if not (_edge_size < size[0]): + if _edge_size >= size[0]: raise ValueError( "The size of the borders and margins combined must be " "less than the width of the widget" ) - if not (_edge_size < size[1]): + if _edge_size >= size[1]: raise ValueError( "The size of the borders and margins combined must be " "less than the height of the widget" @@ -283,10 +283,10 @@ def value(self, value: Union[int, float]) -> None: :rtype: None """ - if not (isinstance(value, (int, float))): + if not isinstance(value, (int, float)): raise TypeError("The value to set must be either an integer or a float") - if not (self.minimum <= value <= self.maximum): + if not self.minimum <= value <= self.maximum: raise ValueError(f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})") # Save off the previous value, so we can pass it in the @@ -328,10 +328,10 @@ def progress(self, value: float) -> None: :rtype: None """ - if not (isinstance(value, (float, int))): + if not isinstance(value, (float, int)): raise TypeError("'progress' must be an int or a float") - if not (0.0 <= value <= 100.0): + if not 0.0 <= value <= 100.0: raise ValueError("'progress' must be between 0 and 100") self.value = (self.minimum + (self.maximum - self.minimum)) * (value * 0.01) @@ -447,18 +447,18 @@ def margin_size(self, value: int) -> None: :rtype: None """ - if not (isinstance(value, int)): + if not isinstance(value, int): raise TypeError("The margin size must be an integer") margin_spacing = (2 * value) + (2 * self._border_thickness) - if not (margin_spacing < self.widget_width): + if margin_spacing >= self.widget_width: raise ValueError( "The size of the borders and margins combined can total the same or more" "than the widget's width." ) - if not (margin_spacing < self.widget_height): + if margin_spacing >= self.widget_height: raise ValueError( "The size of the borders and margins combined can total the same or more" "than the widget's height." diff --git a/adafruit_progressbar/progressbar.py b/adafruit_progressbar/progressbar.py index 83fd11b..ab1069d 100755 --- a/adafruit_progressbar/progressbar.py +++ b/adafruit_progressbar/progressbar.py @@ -65,7 +65,7 @@ def __init__( # This needs to remain for backward compatibility, the default ProgressBar class # should only be able to handle values of type "float" - if not (isinstance(progress, float)): + if not isinstance(progress, float): raise TypeError("Progress must be a floating point value.") super().__init__( From 4b699951f31cbf4b6686f9d80a1ed9e8127cf872 Mon Sep 17 00:00:00 2001 From: Ali Mustafa Date: Thu, 20 May 2021 16:14:16 +0500 Subject: [PATCH 3/3] Reformatted with Black --- adafruit_progressbar/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/adafruit_progressbar/__init__.py b/adafruit_progressbar/__init__.py index 905ba72..79d5289 100755 --- a/adafruit_progressbar/__init__.py +++ b/adafruit_progressbar/__init__.py @@ -83,7 +83,9 @@ def __init__( raise ValueError("The width and the height must be greater than zero") if not value_range[0] <= value <= value_range[1]: - raise ValueError("The starting value must be within the range of minimum to maximum") + raise ValueError( + "The starting value must be within the range of minimum to maximum" + ) _edge_size = 2 * margin_size + 2 * border_thickness @@ -287,7 +289,9 @@ def value(self, value: Union[int, float]) -> None: raise TypeError("The value to set must be either an integer or a float") if not self.minimum <= value <= self.maximum: - raise ValueError(f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})") + raise ValueError( + f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})" + ) # Save off the previous value, so we can pass it in the # call to "Render"