Skip to content

Commit

Permalink
chore: Improve error handling and error messages in ports_containers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nulinspiratie committed Aug 23, 2024
1 parent 261a23c commit a48efda
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions quam/components/ports/ports_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a48efda

Please sign in to comment.