Skip to content
Snippets Groups Projects
Commit a6f817cb authored by Oscar Gustafsson's avatar Oscar Gustafsson :bicyclist:
Browse files

Replace assert with specified exceptions

parent 56446a1f
No related branches found
No related tags found
No related merge requests found
Pipeline #88132 passed
...@@ -174,16 +174,14 @@ class InputPort(AbstractPort): ...@@ -174,16 +174,14 @@ class InputPort(AbstractPort):
def add_signal(self, signal: Signal) -> None: def add_signal(self, signal: Signal) -> None:
if self._source_signal is not None: if self._source_signal is not None:
raise ValueError("Cannot add to already connected input port.") raise ValueError("Cannot add to already connected input port.")
assert ( if signal is self._source_signal:
signal is not self._source_signal raise ValueError("Cannot add same signal twice")
), "Attempted to add already connected signal."
self._source_signal = signal self._source_signal = signal
signal.set_destination(self) signal.set_destination(self)
def remove_signal(self, signal: Signal) -> None: def remove_signal(self, signal: Signal) -> None:
assert ( if signal is not self._source_signal:
signal is self._source_signal raise ValueError("Cannot remove signal that is not connected")
), "Attempted to remove signal that is not connected."
self._source_signal = None self._source_signal = None
signal.remove_destination() signal.remove_destination()
...@@ -242,16 +240,14 @@ class OutputPort(AbstractPort, SignalSourceProvider): ...@@ -242,16 +240,14 @@ class OutputPort(AbstractPort, SignalSourceProvider):
return self._destination_signals return self._destination_signals
def add_signal(self, signal: Signal) -> None: def add_signal(self, signal: Signal) -> None:
assert ( if signal in self._destination_signals:
signal not in self._destination_signals raise ValueError("Cannot add same signal twice")
), "Attempted to add already connected signal."
self._destination_signals.append(signal) self._destination_signals.append(signal)
signal.set_source(self) signal.set_source(self)
def remove_signal(self, signal: Signal) -> None: def remove_signal(self, signal: Signal) -> None:
assert ( if signal not in self._destination_signals:
signal in self._destination_signals raise ValueError("Cannot remove signal that is not connected")
), "Attempted to remove signal that is not connected."
self._destination_signals.remove(signal) self._destination_signals.remove(signal)
signal.remove_source() signal.remove_source()
......
...@@ -74,11 +74,11 @@ class Schedule: ...@@ -74,11 +74,11 @@ class Schedule:
self._schedule_time = schedule_time self._schedule_time = schedule_time
def start_time_of_operation(self, op_id: GraphID) -> int: def start_time_of_operation(self, op_id: GraphID) -> int:
"""Get the start time of the operation with the specified by the op_id.
""" """
assert ( Get the start time of the operation with the specified by the op_id.
op_id in self._start_times """
), "No operation with the specified op_id in this schedule." if op_id not in self._start_times:
raise ValueError(f"No operation with op_id {op_id} in schedule")
return self._start_times[op_id] return self._start_times[op_id]
def get_max_end_time(self) -> int: def get_max_end_time(self) -> int:
...@@ -93,9 +93,8 @@ class Schedule: ...@@ -93,9 +93,8 @@ class Schedule:
return max_end_time return max_end_time
def forward_slack(self, op_id: GraphID) -> int: def forward_slack(self, op_id: GraphID) -> int:
assert ( if op_id not in self._start_times:
op_id in self._start_times raise ValueError(f"No operation with op_id {op_id} in schedule")
), "No operation with the specified op_id in this schedule."
slack = sys.maxsize slack = sys.maxsize
output_slacks = self._forward_slacks(op_id) output_slacks = self._forward_slacks(op_id)
# Make more pythonic # Make more pythonic
...@@ -125,9 +124,8 @@ class Schedule: ...@@ -125,9 +124,8 @@ class Schedule:
return ret return ret
def backward_slack(self, op_id: GraphID) -> int: def backward_slack(self, op_id: GraphID) -> int:
assert ( if op_id not in self._start_times:
op_id in self._start_times raise ValueError(f"No operation with op_id {op_id} in schedule")
), "No operation with the specified op_id in this schedule."
slack = sys.maxsize slack = sys.maxsize
input_slacks = self._backward_slacks(op_id) input_slacks = self._backward_slacks(op_id)
# Make more pythonic # Make more pythonic
...@@ -157,16 +155,16 @@ class Schedule: ...@@ -157,16 +155,16 @@ class Schedule:
return ret return ret
def slacks(self, op_id: GraphID) -> Tuple[int, int]: def slacks(self, op_id: GraphID) -> Tuple[int, int]:
assert ( if op_id not in self._start_times:
op_id in self._start_times raise ValueError(f"No operation with op_id {op_id} in schedule")
), "No operation with the specified op_id in this schedule."
return self.backward_slack(op_id), self.forward_slack(op_id) return self.backward_slack(op_id), self.forward_slack(op_id)
def print_slacks(self) -> None: def print_slacks(self) -> None:
raise NotImplementedError raise NotImplementedError
def set_schedule_time(self, time: int) -> "Schedule": def set_schedule_time(self, time: int) -> "Schedule":
assert self.get_max_end_time() <= time, "New schedule time to short." if time < self.get_max_end_time():
raise ValueError( "New schedule time ({time})to short, minimum: ({self.get_max_end_time()}).")
self._schedule_time = time self._schedule_time = time
return self return self
...@@ -260,9 +258,8 @@ class Schedule: ...@@ -260,9 +258,8 @@ class Schedule:
return self return self
def move_operation(self, op_id: GraphID, time: int) -> "Schedule": def move_operation(self, op_id: GraphID, time: int) -> "Schedule":
assert ( if op_id not in self._start_times:
op_id in self._start_times raise ValueError(f"No operation with op_id {op_id} in schedule")
), "No operation with the specified op_id in this schedule."
(backward_slack, forward_slack) = self.slacks(op_id) (backward_slack, forward_slack) = self.slacks(op_id)
if not -backward_slack <= time <= forward_slack: if not -backward_slack <= time <= forward_slack:
......
...@@ -62,12 +62,10 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -62,12 +62,10 @@ class GraphicsAxesItem(QGraphicsItemGroup):
*parent* is passed to QGraphicsItemGroup's constructor. *parent* is passed to QGraphicsItemGroup's constructor.
""" """
super().__init__(parent=parent) super().__init__(parent=parent)
assert ( if width < 0:
width >= 0 raise ValueError(f"'width' greater or equal to 0 expected, got: {width}.")
), f"'width' greater or equal to 0 expected, got: {width}." if height < 0:
assert ( raise ValueError(f"'height' greater or equal to 0 expected, got: {height}.")
height >= 0
), f"'height' greater or equal to 0 expected, got: {height}."
self._width = width self._width = width
self._height = height self._height = height
...@@ -153,13 +151,15 @@ class GraphicsAxesItem(QGraphicsItemGroup): ...@@ -153,13 +151,15 @@ class GraphicsAxesItem(QGraphicsItemGroup):
def set_height(self, height: int) -> "GraphicsAxesItem": def set_height(self, height: int) -> "GraphicsAxesItem":
# TODO: implement, docstring # TODO: implement, docstring
if height < 0:
raise ValueError(f"'height' greater or equal to 0 expected, got: {height}.")
raise NotImplementedError raise NotImplementedError
def set_width(self, width: int) -> "GraphicsAxesItem": def set_width(self, width: int) -> "GraphicsAxesItem":
# TODO: docstring # TODO: docstring
assert ( if width < 0:
width >= 0 raise ValueError(f"'width' greater or equal to 0 expected, got: {width}.")
), f"'width' greater or equal to 0 expected, got: {width}."
delta_width = width - self._width delta_width = width - self._width
if delta_width > 0: if delta_width > 0:
......
...@@ -150,9 +150,8 @@ class SFG(AbstractOperation): ...@@ -150,9 +150,8 @@ class SFG(AbstractOperation):
# Setup input signals. # Setup input signals.
if input_signals is not None: if input_signals is not None:
for input_index, signal in enumerate(input_signals): for input_index, signal in enumerate(input_signals):
assert ( if signal in self._original_components_to_new:
signal not in self._original_components_to_new raise ValueError(f"Duplicate input signal {signal!r} in SFG")
), "Duplicate input signals supplied to SFG constructor."
new_input_op = self._add_component_unconnected_copy(Input()) new_input_op = self._add_component_unconnected_copy(Input())
new_signal = self._add_component_unconnected_copy(signal) new_signal = self._add_component_unconnected_copy(signal)
new_signal.set_source(new_input_op.output(0)) new_signal.set_source(new_input_op.output(0))
...@@ -162,9 +161,8 @@ class SFG(AbstractOperation): ...@@ -162,9 +161,8 @@ class SFG(AbstractOperation):
# Setup input operations, starting from indices after input signals. # Setup input operations, starting from indices after input signals.
if inputs is not None: if inputs is not None:
for input_index, input_op in enumerate(inputs, input_signal_count): for input_index, input_op in enumerate(inputs, input_signal_count):
assert ( if input_op in self._original_components_to_new:
input_op not in self._original_components_to_new raise ValueError(f"Duplicate input operation {input_op!r} in SFG")
), "Duplicate input operations supplied to SFG constructor."
new_input_op = self._add_component_unconnected_copy(input_op) new_input_op = self._add_component_unconnected_copy(input_op)
for signal in input_op.output(0).signals: for signal in input_op.output(0).signals:
assert signal not in self._original_components_to_new, ( assert signal not in self._original_components_to_new, (
...@@ -200,9 +198,9 @@ class SFG(AbstractOperation): ...@@ -200,9 +198,9 @@ class SFG(AbstractOperation):
for output_index, output_op in enumerate( for output_index, output_op in enumerate(
outputs, output_signal_count outputs, output_signal_count
): ):
assert ( if output_op in self._original_components_to_new:
output_op not in self._original_components_to_new raise ValueError(f"Duplicate output operation {output_op!r} in SFG")
), "Duplicate output operations supplied to SFG constructor."
new_output_op = self._add_component_unconnected_copy(output_op) new_output_op = self._add_component_unconnected_copy(output_op)
for signal in output_op.input(0).signals: for signal in output_op.input(0).signals:
if signal in self._original_components_to_new: if signal in self._original_components_to_new:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment