Skip to content

#28 Replaced assert() calls with if/else/raise <Exception>() #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions adafruit_progressbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,30 @@ 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 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 size[0] <= 0 or 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 _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 _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
Expand Down Expand Up @@ -201,9 +202,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

Expand Down Expand Up @@ -285,13 +285,13 @@ 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"
Expand Down Expand Up @@ -332,9 +332,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)

Expand Down Expand Up @@ -449,19 +451,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 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 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
Expand Down
3 changes: 2 additions & 1 deletion adafruit_progressbar/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down