From 627fd5d5448857c4535f743ecf8171f4835670ef Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson <oscar.gustafsson@gmail.com> Date: Thu, 19 Jan 2023 11:04:19 +0100 Subject: [PATCH] Replace assert with raise --- b_asic/core_operations.py | 57 ++++++++++++++++++++++++--------------- b_asic/operation.py | 11 ++++---- b_asic/signal.py | 5 ++-- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/b_asic/core_operations.py b/b_asic/core_operations.py index ae78545e..73941405 100644 --- a/b_asic/core_operations.py +++ b/b_asic/core_operations.py @@ -16,7 +16,8 @@ from b_asic.port import SignalSourceProvider class Constant(AbstractOperation): - """Constant value operation. + """ + Constant value operation. Gives a specified value that remains constant for every iteration. @@ -54,7 +55,8 @@ class Constant(AbstractOperation): class Addition(AbstractOperation): - """Binary addition operation. + """ + Binary addition operation. Gives the result of adding two inputs. @@ -88,7 +90,8 @@ class Addition(AbstractOperation): class Subtraction(AbstractOperation): - """Binary subtraction operation. + """ + Binary subtraction operation. Gives the result of subtracting the second input from the first one. @@ -122,7 +125,8 @@ class Subtraction(AbstractOperation): class AddSub(AbstractOperation): - """Two-input addition or subtraction operation. + """ + Two-input addition or subtraction operation. Gives the result of adding or subtracting two inputs. @@ -169,7 +173,8 @@ class AddSub(AbstractOperation): class Multiplication(AbstractOperation): - """Binary multiplication operation. + """ + Binary multiplication operation. Gives the result of multiplying two inputs. @@ -203,7 +208,8 @@ class Multiplication(AbstractOperation): class Division(AbstractOperation): - """Binary division operation. + """ + Binary division operation. Gives the result of dividing the first input by the second one. @@ -237,7 +243,8 @@ class Division(AbstractOperation): class Min(AbstractOperation): - """Binary min operation. + """ + Binary min operation. Gives the minimum value of two inputs. NOTE: Non-real numbers are not supported. @@ -268,14 +275,15 @@ class Min(AbstractOperation): return "min" def evaluate(self, a, b): - assert not isinstance(a, complex) and not isinstance( - b, complex - ), "core_operations.Min does not support complex numbers." + if isinstance(a, complex) or isinstance(b, complex): + raise ValueError( + "core_operations.Min does not support complex numbers.") return a if a < b else b class Max(AbstractOperation): - """Binary max operation. + """ + Binary max operation. Gives the maximum value of two inputs. NOTE: Non-real numbers are not supported. @@ -306,14 +314,15 @@ class Max(AbstractOperation): return "max" def evaluate(self, a, b): - assert not isinstance(a, complex) and not isinstance( - b, complex - ), "core_operations.Max does not support complex numbers." + if isinstance(a, complex) or isinstance(b, complex): + raise ValueError( + "core_operations.Max does not support complex numbers.") return a if a > b else b class SquareRoot(AbstractOperation): - """Square root operation. + """ + Square root operation. Gives the square root of its input. @@ -346,7 +355,8 @@ class SquareRoot(AbstractOperation): class ComplexConjugate(AbstractOperation): - """Complex conjugate operation. + """ + Complex conjugate operation. Gives the complex conjugate of its input. @@ -379,7 +389,8 @@ class ComplexConjugate(AbstractOperation): class Absolute(AbstractOperation): - """Absolute value operation. + """ + Absolute value operation. Gives the absolute value of its input. @@ -412,7 +423,8 @@ class Absolute(AbstractOperation): class ConstantMultiplication(AbstractOperation): - """Constant multiplication operation. + """ + Constant multiplication operation. Gives the result of multiplying its input by a specified value. @@ -458,7 +470,8 @@ class ConstantMultiplication(AbstractOperation): class Butterfly(AbstractOperation): - """Butterfly operation. + """ + Butterfly operation. Gives the result of adding its two inputs, as well as the result of subtracting the second input from the first one. @@ -494,7 +507,8 @@ class Butterfly(AbstractOperation): class MAD(AbstractOperation): - """Multiply-add operation. + """ + Multiply-add operation. Gives the result of multiplying the first input by the second input and then adding the third input. @@ -530,7 +544,8 @@ class MAD(AbstractOperation): class SymmetricTwoportAdaptor(AbstractOperation): - """Symmetric twoport-adaptor operation. + """ + Symmetric twoport-adaptor operation. output(0): input(1) + value*(input(1) - input(0) output(1): input(0) + value*(input(1) - input(0) diff --git a/b_asic/operation.py b/b_asic/operation.py index 56999add..41309c83 100644 --- a/b_asic/operation.py +++ b/b_asic/operation.py @@ -414,7 +414,8 @@ class AbstractOperation(Operation, AbstractGraphComponent): if latency is not None: # Set the latency of the rest of ports with no latency_offset. - assert latency >= 0, "Negative latency entered" + if latency < 0: + raise ValueError("Latency cannot be negative") for inp in self.inputs: if inp.latency_offset is None: inp.latency_offset = 0 @@ -858,7 +859,8 @@ class AbstractOperation(Operation, AbstractGraphComponent): return latency_offsets def set_latency(self, latency: int) -> None: - assert latency >= 0, "Negative latency entered." + if latency < 0: + raise ValueError("Latency cannot be negative") for inport in self.inputs: inport.latency_offset = 0 for outport in self.outputs: @@ -895,9 +897,8 @@ class AbstractOperation(Operation, AbstractGraphComponent): @execution_time.setter def execution_time(self, execution_time: int) -> None: - assert ( - execution_time is None or execution_time >= 0 - ), "Negative execution time entered." + if execution_time is not None and execution_time < 0: + raise ValueError("Execution time cannot be negative") self._execution_time = execution_time def get_plot_coordinates( diff --git a/b_asic/signal.py b/b_asic/signal.py index 179817f4..867f7ba7 100644 --- a/b_asic/signal.py +++ b/b_asic/signal.py @@ -138,7 +138,6 @@ class Signal(AbstractGraphComponent): should truncate received values to. None = unlimited. """ - assert bits is None or ( - isinstance(bits, int) and bits >= 0 - ), "Bits must be non-negative." + if bits is not None and bits < 0: + raise ValueError("Bits cannot be negative") self.set_param("bits", bits) -- GitLab