1
1
from abc import ABC , abstractmethod
2
2
import itertools
3
+ import warnings
3
4
4
5
# from typing import List
5
6
from operator import attrgetter
@@ -385,7 +386,7 @@ def add_input_ports(self, *ports: InputPort | dict | str):
385
386
"""
386
387
for port_info in ports :
387
388
port = self .parse_port_entry (port_info , InputPort )
388
- self .input_ports [ port . name ] = port
389
+ self ._add_input_port ( port )
389
390
390
391
def add_output_ports (self , * ports : OutputPort | dict | str ):
391
392
"""
@@ -402,7 +403,7 @@ def add_output_ports(self, *ports: OutputPort | dict | str):
402
403
"""
403
404
for port_info in ports :
404
405
port = self .parse_port_entry (port_info , OutputPort )
405
- self .output_ports [ port . name ] = port
406
+ self ._add_output_port ( port )
406
407
407
408
def add_variable_ports (self , * ports : VariablePort | dict | str ):
408
409
"""
@@ -419,28 +420,20 @@ def add_variable_ports(self, *ports: VariablePort | dict | str):
419
420
"""
420
421
for port_info in ports :
421
422
port = self .parse_port_entry (port_info , VariablePort )
422
- self .variable_ports [ port . name ] = port
423
+ self ._add_variable_port ( port )
423
424
424
425
425
- """
426
- def add_input_port(self, port: InputPort):
427
- # DEPRECATE?
428
- # assert isinstance(port, InputPort)
426
+ def _add_input_port (self , port : InputPort ):
429
427
self .check_existing_port_names (port )
430
428
self .input_ports [port .name ] = port
431
429
432
- def add_output_port(self, port: OutputPort):
433
- # DEPRECATE?
434
- # assert isinstance(port, OutputPort)
430
+ def _add_output_port (self , port : OutputPort ):
435
431
self .check_existing_port_names (port )
436
432
self .output_ports [port .name ] = port
437
433
438
- def add_variable_port(self, port: VariablePort):
439
- # DEPRECATE?
440
- # assert isinstance(port, VariablePort)
434
+ def _add_variable_port (self , port : VariablePort ):
441
435
self .check_existing_port_names (port )
442
436
self .variable_ports [port .name ] = port
443
- """
444
437
445
438
def _get_port_by_name (self , port : str ):
446
439
if port in self .variable_ports :
@@ -450,6 +443,51 @@ def _get_port_by_name(self, port: str):
450
443
if port in self .output_ports :
451
444
return self .output_ports [port ]
452
445
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
453
491
454
492
@abstractmethod
455
493
def compile (self , prefix_names = False ):
@@ -972,6 +1010,8 @@ def _parse_child(self, child_data):
972
1010
self ._add_child (child )
973
1011
974
1012
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 } " )
975
1015
self .children [child .name ] = child
976
1016
977
1017
def add_wires (self , variable_wires : list = [], directed_wires : list = []):
0 commit comments