diff --git a/b_asic/port.py b/b_asic/port.py index d7a0cc17fcf6c998dc15ad270977702e7a7162cd..64c56bc9aef2ca71c78d08ccf0b8c20c1c68fd61 100644 --- a/b_asic/port.py +++ b/b_asic/port.py @@ -49,20 +49,23 @@ class Port(ABC): @abstractmethod def connect(self, port: "Port") -> Signal: """Create and return a signal that is connected to this port and the entered - port and connect this port to the signal and the entered port to the signal.""" + port and connect this port to the signal and the entered port to the signal. + """ raise NotImplementedError @abstractmethod def add_signal(self, signal: Signal) -> None: """Connect this port to the entered signal. If the entered signal isn't connected to - this port then connect the entered signal to the port aswell.""" + this port then connect the entered signal to the port aswell. + """ raise NotImplementedError @abstractmethod def disconnect(self, port: "Port") -> None: """Disconnect the entered port from the port by removing it from the ports signal. If the entered port is still connected to this ports signal then disconnect the entered - port from the signal aswell.""" + port from the signal aswell. + """ raise NotImplementedError @abstractmethod @@ -112,7 +115,6 @@ class InputPort(AbstractPort): _source_signal: Optional[Signal] _value_length: Optional[int] - def __init__(self, port_id: PortIndex, operation: Operation): super().__init__(port_id, operation) self._source_signal = None @@ -130,11 +132,11 @@ class InputPort(AbstractPort): def connected_ports(self) -> List[Port]: return [] if self._source_signal is None or self._source_signal.source is None \ else [self._source_signal.source] - + @value_length.setter def value_length(self, bits: Optional[int]) -> None: - assert isinstance(bits, int) or bits is None, "Value length on input port has to be of type Int or NoneType." - assert bits is None or bits >= 0, "Value length on input port has to be a non-negative integer or None." + assert (isinstance(bits, int) and bits >= 0) or bits is None, \ + "Value length on input port has to be a non-negative integer or None." self._value_length = bits def signal_count(self) -> int: @@ -142,7 +144,7 @@ class InputPort(AbstractPort): def connect(self, port: "OutputPort") -> Signal: assert self._source_signal is None, "Connecting new port to already connected input port." - return Signal(port, self) # self._source_signal is set by the signal constructor + return Signal(port, self) # self._source_signal is set by the signal constructor. def add_signal(self, signal: Signal) -> None: assert self._source_signal is None, "Connecting new port to already connected input port." @@ -159,12 +161,13 @@ class InputPort(AbstractPort): old_signal: Signal = self._source_signal self._source_signal = None if self is old_signal.destination: - # Disconnect the dest of the signal if this inputport currently is the dest + # Disconnect the dest of the signal if this inputport currently is the dest. old_signal.remove_destination() def clear(self) -> None: self.remove_signal(self._source_signal) + class OutputPort(AbstractPort): """Output port. TODO: More info. @@ -193,14 +196,14 @@ class OutputPort(AbstractPort): return len(self._destination_signals) def connect(self, port: InputPort) -> Signal: - return Signal(self, port) # Signal is added to self._destination_signals in signal constructor + return Signal(self, port) # Signal is added to self._destination_signals in signal constructor. def add_signal(self, signal: Signal) -> None: assert signal not in self.signals, \ "Attempting to connect to Signal already connected." self._destination_signals.append(signal) if self is not signal.source: - # Connect this outputport to the signal if it isn't already + # Connect this outputport to the signal if it isn't already. signal.set_source(self) def disconnect(self, port: InputPort) -> None: diff --git a/test/test_inputport.py b/test/test_inputport.py index 910b2700e25f4cb8d4148cc8bfb6b89126576a7b..a0f24e4e90cb69553a200e0a8402ac248c14d3b9 100644 --- a/test/test_inputport.py +++ b/test/test_inputport.py @@ -24,18 +24,16 @@ def dangling_sig(): return Signal() @pytest.fixture -def s_w_source(): - out_port = OutputPort(0, None) +def s_w_source(out_port): return Signal(source=out_port) @pytest.fixture -def sig_with_dest(): - #inp_port = InputPort(0, None) - return Signal(destination=inp_port()) +def sig_with_dest(inp_port): + return Signal(destination=inp_port) @pytest.fixture -def connected_sig(): - return Signal(source=out_port(), destination=inp_port()) +def connected_sig(inp_port, out_port): + return Signal(source=out_port, destination=inp_port) def test_connect_then_disconnect(inp_port, out_port): """Test connect unused port to port.""" @@ -104,4 +102,4 @@ def test_set_value_length_float(inp_port): def test_set_value_length_pos_then_none(inp_port): inp_port.value_length = 10 inp_port.value_length = None - assert inp_port.value_length == None \ No newline at end of file + assert inp_port.value_length is None