Skip to content

Commit 3c65d9f

Browse files
Assert replaced with IFs and Raises
1 parent 7b617d3 commit 3c65d9f

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

adafruit_progressbar/__init__.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,28 @@ def __init__(
7676
value_range: Union[Tuple[int, int], Tuple[float, float]] = (0, 100),
7777
) -> None:
7878

79-
assert (
80-
value_range[0] < value_range[1]
81-
), "The minimum value must be less than the maximum value"
79+
if not (value_range[0] < value_range[1]):
80+
raise ValueError("The minimum value must be less than the maximum value")
8281

83-
assert (
84-
size[0] > 0 and size[1] > 0
85-
), "The width and the height must be greater than zero"
82+
if not (size[0] > 0 and size[1] > 0):
83+
raise ValueError("The width and the height must be greater than zero")
8684

87-
assert (
88-
value_range[0] <= value <= value_range[1]
89-
), "The starting value must be within the range of minimum to maximum"
85+
if not (value_range[0] <= value <= value_range[1]):
86+
raise ValueError("The starting value must be within the range of minimum to maximum")
9087

9188
_edge_size = 2 * margin_size + 2 * border_thickness
9289

93-
assert _edge_size < size[0], (
94-
"The size of the borders and margins combined must be "
95-
"less than the width of the widget"
96-
)
90+
if not (_edge_size < size[0]):
91+
raise ValueError(
92+
"The size of the borders and margins combined must be "
93+
"less than the width of the widget"
94+
)
9795

98-
assert _edge_size < size[1], (
99-
"The size of the borders and margins combined must be "
100-
"less than the height of the widget"
101-
)
96+
if not (_edge_size < size[1]):
97+
raise ValueError(
98+
"The size of the borders and margins combined must be "
99+
"less than the height of the widget"
100+
)
102101

103102
self._progress = 0.0
104103
self._widget_size = size
@@ -201,9 +200,8 @@ def border_color(self, color: Union[int, Tuple[int, int, int]]) -> None:
201200
:rtype: None
202201
"""
203202

204-
assert (
205-
isinstance(color, int) or color is None
206-
), "A color must be represented by a integer value"
203+
if not (isinstance(color, int) or color is None):
204+
raise TypeError("A color must be represented by a integer value")
207205

208206
self._border_color = color
209207

@@ -285,13 +283,11 @@ def value(self, value: Union[int, float]) -> None:
285283
:rtype: None
286284
"""
287285

288-
assert isinstance(
289-
value, (int, float)
290-
), "The value to set must be either an integer or a float"
286+
if not (isinstance(value, (int, float))):
287+
raise TypeError("The value to set must be either an integer or a float")
291288

292-
assert (
293-
self.minimum <= value <= self.maximum
294-
), f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})"
289+
if not (self.minimum <= value <= self.maximum):
290+
raise ValueError(f"The value must be between minimum ({self.minimum}) and maximum ({self.maximum})")
295291

296292
# Save off the previous value, so we can pass it in the
297293
# call to "Render"
@@ -332,9 +328,11 @@ def progress(self, value: float) -> None:
332328
:rtype: None
333329
"""
334330

335-
assert [isinstance(value, (float, int)), "'progress' must be an int or a float"]
331+
if not (isinstance(value, (float, int))):
332+
raise TypeError("'progress' must be an int or a float")
336333

337-
assert 0.0 <= value <= 100.0, "'progress' must be between 0 and 100"
334+
if not (0.0 <= value <= 100.0):
335+
raise ValueError("'progress' must be between 0 and 100")
338336

339337
self.value = (self.minimum + (self.maximum - self.minimum)) * (value * 0.01)
340338

@@ -449,19 +447,22 @@ def margin_size(self, value: int) -> None:
449447
:rtype: None
450448
"""
451449

452-
assert isinstance(value, int), "The margin size must be an integer"
450+
if not (isinstance(value, int)):
451+
raise TypeError("The margin size must be an integer")
453452

454453
margin_spacing = (2 * value) + (2 * self._border_thickness)
455454

456-
assert margin_spacing < self.widget_width, (
457-
"The size of the borders and margins combined can total the same or more"
458-
"than the widget's width."
459-
)
460-
461-
assert margin_spacing < self.widget_height, (
462-
"The size of the borders and margins combined can total the same or more"
463-
"than the widget's height."
464-
)
455+
if not (margin_spacing < self.widget_width):
456+
raise ValueError(
457+
"The size of the borders and margins combined can total the same or more"
458+
"than the widget's width."
459+
)
460+
461+
if not (margin_spacing < self.widget_height):
462+
raise ValueError(
463+
"The size of the borders and margins combined can total the same or more"
464+
"than the widget's height."
465+
)
465466

466467
self._margin_size = value
467468
self._set_progress(self._progress) # For a render pass

adafruit_progressbar/progressbar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def __init__(
6565

6666
# This needs to remain for backward compatibility, the default ProgressBar class
6767
# should only be able to handle values of type "float"
68-
assert isinstance(progress, float), "Progress must be a floating point value."
68+
if not (isinstance(progress, float)):
69+
raise TypeError("Progress must be a floating point value.")
6970

7071
super().__init__(
7172
(x, y),

0 commit comments

Comments
 (0)