From a48efda52ee50f39fa361cb3b838c460b6cefb4a Mon Sep 17 00:00:00 2001 From: Serwan Asaad Date: Fri, 23 Aug 2024 15:43:56 +0200 Subject: [PATCH] chore: Improve error handling and error messages in ports_containers.py --- quam/components/ports/ports_containers.py | 60 +++++++++++++++++------ 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/quam/components/ports/ports_containers.py b/quam/components/ports/ports_containers.py index 1e4509b5..368c932a 100644 --- a/quam/components/ports/ports_containers.py +++ b/quam/components/ports/ports_containers.py @@ -57,6 +57,14 @@ def _get_port( create: bool = False, **kwargs, ): + if port_type not in [ + "analog_output", + "analog_input", + "digital_output", + "digital_input", + ]: + raise ValueError(f"Invalid port type: {port_type}") + controllers = getattr(self, f"{port_type}s") try: @@ -95,15 +103,21 @@ def reference_to_port( if reference is None: raise ValueError("Cannot get port from reference {port_reference}") port_reference = reference - elems = port_reference.split("/") - port_type, controller_id, port_id = elems[-3:] - port_type = port_type[:-1] - if controller_id.isdigit(): - controller_id = int(controller_id) - port_id = int(port_id) + try: + elems = port_reference.split("/") + port_type, controller_id, port_id = elems[-3:] + + port_type = port_type[:-1] + if controller_id.isdigit(): + controller_id = int(controller_id) + port_id = int(port_id) - return self._get_port(controller_id, port_id, port_type, create=create) + return self._get_port(controller_id, port_id, port_type, create=create) + except Exception as e: + raise ValueError( + f"Unable to parse port reference for OPX+: {port_reference}" + ) from e def get_analog_output( self, @@ -178,6 +192,15 @@ def _get_port( create: bool = False, **kwargs, ): + if port_type not in [ + "analog_output", + "analog_input", + "mw_output", + "mw_input", + "digital_output", + ]: + raise ValueError(f"Invalid port type: {port_type}") + controllers = getattr(self, f"{port_type}s") try: @@ -238,16 +261,23 @@ def reference_to_port( raise ValueError("Cannot get port from reference {port_reference}") port_reference = reference - elems = port_reference.split("/") - port_type, controller_id, fem_id, port_id = elems[-4:] + try: + elems = port_reference.split("/") + port_type, controller_id, fem_id, port_id = elems[-4:] - port_type = port_type[:-1] - if controller_id.isdigit(): - controller_id = int(controller_id) - fem_id = int(fem_id) - port_id = int(port_id) + port_type = port_type[:-1] + if controller_id.isdigit(): + controller_id = int(controller_id) + fem_id = int(fem_id) + port_id = int(port_id) - return self._get_port(controller_id, fem_id, port_id, port_type, create=create) + return self._get_port( + controller_id, fem_id, port_id, port_type, create=create + ) + except Exception as e: + raise ValueError( + f"Unable to parse port reference for OPX1000 FEM: {port_reference}" + ) from e def get_analog_output( self,