Skip to content

Commit c931094

Browse files
Merge pull request #30 from alimustafashah/master
#28 Replaced assert() calls with if/else/raise <Exception>()
2 parents f6a73ea + 0e29a8b commit c931094

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

adafruit_progressbar/__init__.py

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,30 @@ 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 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 size[0] <= 0 or 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(
87+
"The starting value must be within the range of minimum to maximum"
88+
)
9089

9190
_edge_size = 2 * margin_size + 2 * border_thickness
9291

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-
)
92+
if _edge_size >= size[0]:
93+
raise ValueError(
94+
"The size of the borders and margins combined must be "
95+
"less than the width of the widget"
96+
)
9797

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-
)
98+
if _edge_size >= size[1]:
99+
raise ValueError(
100+
"The size of the borders and margins combined must be "
101+
"less than the height of the widget"
102+
)
102103

103104
self._progress = 0.0
104105
self._widget_size = size
@@ -201,9 +202,8 @@ def border_color(self, color: Union[int, Tuple[int, int, int]]) -> None:
201202
:rtype: None
202203
"""
203204

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

208208
self._border_color = color
209209

@@ -285,13 +285,13 @@ def value(self, value: Union[int, float]) -> None:
285285
:rtype: None
286286
"""
287287

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

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

296296
# Save off the previous value, so we can pass it in the
297297
# call to "Render"
@@ -332,9 +332,11 @@ def progress(self, value: float) -> None:
332332
:rtype: None
333333
"""
334334

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

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

339341
self.value = (self.minimum + (self.maximum - self.minimum)) * (value * 0.01)
340342

@@ -449,19 +451,22 @@ def margin_size(self, value: int) -> None:
449451
:rtype: None
450452
"""
451453

452-
assert isinstance(value, int), "The margin size must be an integer"
454+
if not isinstance(value, int):
455+
raise TypeError("The margin size must be an integer")
453456

454457
margin_spacing = (2 * value) + (2 * self._border_thickness)
455458

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-
)
459+
if margin_spacing >= self.widget_width:
460+
raise ValueError(
461+
"The size of the borders and margins combined can total the same or more"
462+
"than the widget's width."
463+
)
464+
465+
if margin_spacing >= self.widget_height:
466+
raise ValueError(
467+
"The size of the borders and margins combined can total the same or more"
468+
"than the widget's height."
469+
)
465470

466471
self._margin_size = value
467472
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)