@@ -158,18 +158,18 @@ def add_sub_controller(
158158
159159 sub_controller .set_path (self .path + [name ])
160160 self .__sub_controller_tree [name ] = sub_controller
161- super ().__setattr__ (str ( name ) , sub_controller )
161+ super ().__setattr__ (name , sub_controller )
162162
163163 if isinstance (sub_controller .root_attribute , Attribute ):
164- self .attributes [str ( name ) ] = sub_controller .root_attribute
164+ self .attributes [name ] = sub_controller .root_attribute
165165
166166 @property
167167 def sub_controllers (self ) -> dict [str , BaseController ]:
168168 return self .__sub_controller_tree
169169
170170 def __repr__ (self ):
171171 name = self .__class__ .__name__
172- path = "." .join ([ str ( p ) for p in self .path ] ) or None
172+ path = "." .join (self .path ) or None
173173 sub_controllers = list (self .sub_controllers .keys ()) or None
174174
175175 return f"{ name } (path={ path } , sub_controllers={ sub_controllers } )"
@@ -219,11 +219,8 @@ async def disconnect(self) -> None:
219219
220220
221221class ControllerVector (MutableMapping [int , Controller ], BaseController ):
222- """A collection of SubControllers, with an arbitrary integer index.
223- An instance of this class can be registered with a parent ``Controller`` to include
224- it's children as part of a larger controller. Each child of the vector will keep
225- a string name of the vector.
226- """
222+ """A controller with a collection of identical sub controllers distinguished
223+ by a numeric value"""
227224
228225 root_attribute : Attribute | None = None
229226
@@ -239,14 +236,22 @@ def __init__(
239236 for index , child in children .items ():
240237 super ().add_sub_controller (str (index ), child )
241238
242- def add_sub_controller (self , * args , ** kwargs ):
239+ def add_sub_controller (
240+ self , name : str , sub_controller : Controller | ControllerVector
241+ ):
243242 raise NotImplementedError (
244243 "Cannot add named sub controller to ControllerVector. "
245- "Use __setitem__ instead, for indexed sub controllers"
244+ "Use __setitem__ instead, for indexed sub controllers. "
245+ "E.g., vector[1] = Controller()"
246246 )
247247
248248 def __getitem__ (self , key : int ) -> Controller :
249- return self ._children [key ]
249+ try :
250+ return self ._children [key ]
251+ except KeyError as exception :
252+ raise KeyError (
253+ f"ControllerVector does not have Controller with key { key } "
254+ ) from exception
250255
251256 def __setitem__ (self , key : int , value : Controller ) -> None :
252257 if not isinstance (key , int ):
@@ -266,9 +271,5 @@ def __iter__(self) -> Iterator[int]:
266271 def __len__ (self ) -> int :
267272 return len (self ._children )
268273
269- def children (self ) -> Iterator [tuple [str , Controller ]]:
270- for key , child in self ._children .items ():
271- yield str (key ), child
272-
273- def __hash__ (self ):
274- return hash (id (self ))
274+ def children (self ) -> Iterator [tuple [int , Controller ]]:
275+ yield from self ._children .items ()
0 commit comments