Skip to content

Commit b1f156c

Browse files
committed
added ported object access properties
1 parent 1a3253e commit b1f156c

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

psymple/ported_objects.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC, abstractmethod
22
import itertools
3+
import warnings
34

45
# from typing import List
56
from operator import attrgetter
@@ -385,7 +386,7 @@ def add_input_ports(self, *ports: InputPort | dict | str):
385386
"""
386387
for port_info in ports:
387388
port = self.parse_port_entry(port_info, InputPort)
388-
self.input_ports[port.name] = port
389+
self._add_input_port(port)
389390

390391
def add_output_ports(self, *ports: OutputPort | dict | str):
391392
"""
@@ -402,7 +403,7 @@ def add_output_ports(self, *ports: OutputPort | dict | str):
402403
"""
403404
for port_info in ports:
404405
port = self.parse_port_entry(port_info, OutputPort)
405-
self.output_ports[port.name] = port
406+
self._add_output_port(port)
406407

407408
def add_variable_ports(self, *ports: VariablePort | dict | str):
408409
"""
@@ -419,28 +420,20 @@ def add_variable_ports(self, *ports: VariablePort | dict | str):
419420
"""
420421
for port_info in ports:
421422
port = self.parse_port_entry(port_info, VariablePort)
422-
self.variable_ports[port.name] = port
423+
self._add_variable_port(port)
423424

424425

425-
"""
426-
def add_input_port(self, port: InputPort):
427-
# DEPRECATE?
428-
# assert isinstance(port, InputPort)
426+
def _add_input_port(self, port: InputPort):
429427
self.check_existing_port_names(port)
430428
self.input_ports[port.name] = port
431429

432-
def add_output_port(self, port: OutputPort):
433-
# DEPRECATE?
434-
# assert isinstance(port, OutputPort)
430+
def _add_output_port(self, port: OutputPort):
435431
self.check_existing_port_names(port)
436432
self.output_ports[port.name] = port
437433

438-
def add_variable_port(self, port: VariablePort):
439-
# DEPRECATE?
440-
# assert isinstance(port, VariablePort)
434+
def _add_variable_port(self, port: VariablePort):
441435
self.check_existing_port_names(port)
442436
self.variable_ports[port.name] = port
443-
"""
444437

445438
def _get_port_by_name(self, port: str):
446439
if port in self.variable_ports:
@@ -450,6 +443,51 @@ def _get_port_by_name(self, port: str):
450443
if port in self.output_ports:
451444
return self.output_ports[port]
452445
return None
446+
447+
@property
448+
def input(self):
449+
"""
450+
Convenience method returning the name of the first input port of self. Intended to be used
451+
if it is known the ported object has only a single input port.
452+
"""
453+
if len(self.input_ports) > 1:
454+
warnings.warn(
455+
f"Ported object {self.name} has more than one input port. The property .input "
456+
f"will only return the first one stored, which may not behave as expected.")
457+
try:
458+
return next(iter(self.input_ports))
459+
except:
460+
return None
461+
462+
@property
463+
def output(self):
464+
"""
465+
Convenience method returning the name of the first output port of self. Intended to be used
466+
if it is known the ported object has only a single output port.
467+
"""
468+
if len(self.output_ports) > 1:
469+
warnings.warn(
470+
f"Ported object {self.name} has more than one output port. The property .output "
471+
f"will only return the first one stored, which may not behave as expected.")
472+
try:
473+
return next(iter(self.output_ports))
474+
except:
475+
return None
476+
477+
@property
478+
def variable(self):
479+
"""
480+
Convenience method returning the name of the first variable port of self. Intended to be used
481+
if it is known the ported object has only a single variable port.
482+
"""
483+
if len(self.variable_ports) > 1:
484+
warnings.warn(
485+
f"Ported object {self.name} has more than one variable port. The property .variable "
486+
f"will only return the first one stored, which may not behave as expected.")
487+
try:
488+
return next(iter(self.variable_ports))
489+
except:
490+
return None
453491

454492
@abstractmethod
455493
def compile(self, prefix_names=False):
@@ -972,6 +1010,8 @@ def _parse_child(self, child_data):
9721010
self._add_child(child)
9731011

9741012
def _add_child(self, child):
1013+
if child.name in self.children:
1014+
raise ValueError(f"Child {child.name} already exists in ported object {self.name}")
9751015
self.children[child.name] = child
9761016

9771017
def add_wires(self, variable_wires: list = [], directed_wires: list = []):

0 commit comments

Comments
 (0)