Skip to content

Commit

Permalink
fix bad constructors, test channel init
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Jul 3, 2023
1 parent 29c3f79 commit be22e94
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/hvps/devices/caen/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def vset(self) -> float:
)
return float(response)

@property
def voltage_set(self) -> float:
return self.vset

@property
def vmin(self) -> float:
response = _write_command(
Expand Down
21 changes: 19 additions & 2 deletions src/hvps/devices/caen/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def number_of_channels(self) -> int:
Returns:
int: The number of channels.
"""
self._logger.debug("Getting number of channels")
if not self._serial.is_open:
# TODO: we should not cache the property if the serial port is not open
self._logger.warning(
"Serial port is not open. Returning 1 as number of channels."
)
return 1

response = _write_command(
ser=self._serial,
logger=self._logger,
Expand All @@ -36,15 +44,24 @@ def number_of_channels(self) -> int:
return int(response)

@property
def channels(self):
def channels(self) -> list[Channel]:
"""The channels in the module.
Returns:
List[Channel]: A list of Channel objects.
"""
if len(self._channels) == 0:
self._logger.debug("Initializing channels")
for channel in range(self.number_of_channels):
self._channels.append(Channel(self._serial, channel, bd=self.bd))
self._logger.debug(f"Creating channel {channel}")
self._channels.append(
Channel(
ser=self._serial,
logger=self._logger,
channel=channel,
bd=self.bd,
)
)
return self._channels

@cached_property
Expand Down
9 changes: 5 additions & 4 deletions src/hvps/devices/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@


class Channel(ABC):
def __init__(self, _serial: serial.Serial, _logger: logging.Logger, channel: int):
def __init__(self, ser: serial.Serial, logger: logging.Logger, channel: int):
"""Initialize the Channel object.
Args:
_serial (serial.Serial): The serial object used for communication.
ser (serial.Serial): The serial object used for communication.
logger (logging.Logger): The logger object used for logging.
channel (int): The channel number.
"""
self._serial = _serial
self._logger = _logger
self._serial = ser
self._logger = logger
self._channel = channel

@property
Expand Down
24 changes: 24 additions & 0 deletions src/hvps/devices/iseg/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def number_of_channels(self) -> int:
Returns:
int: The number of channels.
"""
self._logger.debug("Getting number of channels")
if not self._serial.is_open:
# TODO: we should not cache the property if the serial port is not open
self._logger.warning(
"Serial port is not open. Returning 1 as number of channels."
)
return 1

command = _get_mon_module_command(":READ:MODULE:CHANNELNUMBER")
response = _write_command(
ser=self._serial,
Expand All @@ -31,6 +39,22 @@ def number_of_channels(self) -> int:
raise ValueError("Wrong number of values were received, one value expected")
return int(response[0])

@property
def channels(self) -> list[Channel]:
"""The channels in the module.
Returns:
List[Channel]: A list of Channel objects.
"""
if len(self._channels) == 0:
self._logger.debug("Initializing channels")
for channel in range(self.number_of_channels):
self._logger.debug(f"Creating channel {channel}")
self._channels.append(
Channel(ser=self._serial, channel=channel, logger=self._logger)
)
return self._channels

@property
def firmware_release(self) -> str:
"""
Expand Down
14 changes: 10 additions & 4 deletions src/hvps/devices/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ def number_of_channels(self) -> int:
pass

@property
@abstractmethod
def channels(self) -> List[Channel]:
"""The list of channels in the module.
"""The channels in the module.
This property should be implemented by subclasses to provide the channels in the module.
Returns:
List[Channel]: The list of channels.
List[Channel]: A list of Channel objects.
Raises:
NotImplementedError: If the subclass does not implement this property.
"""
return self._channels
pass

def channel(self, channel: int) -> Channel:
"""Get the specified channel in the module.
Expand All @@ -70,7 +76,7 @@ def channel(self, channel: int) -> Channel:
KeyError: If the channel number is invalid.
"""
if channel not in range(self.number_of_channels):
if channel not in range(len(self.channels)):
raise KeyError(
f"Invalid channel {channel}. Valid channels are 0..{self.number_of_channels - 1}"
)
Expand Down
15 changes: 12 additions & 3 deletions tests/test_caen_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def test_caen_module(caplog):
caen.module(35)


def test_caen_channel():
# TODO: implement
pass
def test_caen_channel(caplog):
caplog.set_level("DEBUG")

caen = Caen(connect=False, logging_level="DEBUG")

module = caen.module()

channel = module.channel(0)

assert "Creating channel 0" in caplog.text

print(f"channel: {channel.channel}")

0 comments on commit be22e94

Please sign in to comment.