1818import numpy as np
1919
2020if TYPE_CHECKING :
21- from cdl . core . gui . main import CDLMainWindow
21+ from collections . abc import Iterator
2222
2323 from cdlclient .remote import ServerProxy
24+ from cdlclient .simplemodel import ImageObj , SignalObj
2425
2526
2627class SimpleAbstractCDLControl (abc .ABC ):
@@ -29,6 +30,44 @@ class SimpleAbstractCDLControl(abc.ABC):
2930 This is a subset of DataLab's AbstractCDLControl, with only the methods that do not
3031 require DataLab object model to be implemented."""
3132
33+ def __len__ (self ) -> int :
34+ """Return number of objects"""
35+ return len (self .get_object_uuids ())
36+
37+ def __getitem__ (
38+ self ,
39+ nb_id_title : int | str | None = None ,
40+ ) -> SignalObj | ImageObj :
41+ """Return object"""
42+ return self .get_object (nb_id_title )
43+
44+ def __iter__ (self ) -> Iterator [SignalObj | ImageObj ]:
45+ """Iterate over objects"""
46+ uuids = self .get_object_uuids ()
47+ for uuid in uuids :
48+ yield self .get_object (uuid )
49+
50+ def __repr__ (self ) -> str :
51+ """Return object representation"""
52+ return self .__str__ ()
53+
54+ def __str__ (self ) -> str :
55+ """Return object string representation"""
56+ titles = self .get_object_titles ()
57+ uuids = self .get_object_uuids ()
58+ text = f"{ self .__class__ .__name__ } (DataLab instance, { len (titles )} items):\n "
59+ for uuid , title in zip (uuids , titles ):
60+ text += f" { uuid } : { title } \n "
61+ return text
62+
63+ def __bool__ (self ) -> bool :
64+ """Return True if model is not empty"""
65+ return bool (self .get_object_uuids ())
66+
67+ def __contains__ (self , id_title : str ) -> bool :
68+ """Return True if object (UUID or title) is in model"""
69+ return id_title in (self .get_object_titles () + self .get_object_uuids ())
70+
3271 @classmethod
3372 def get_public_methods (cls ) -> list [str ]:
3473 """Return all public methods of the class, except itself.
@@ -236,6 +275,26 @@ def get_object_titles(self, panel: str | None = None) -> list[str]:
236275 ValueError: if panel not found
237276 """
238277
278+ @abc .abstractmethod
279+ def get_object (
280+ self ,
281+ nb_id_title : int | str | None = None ,
282+ panel : str | None = None ,
283+ ) -> SignalObj | ImageObj :
284+ """Get object (signal/image) from index.
285+
286+ Args:
287+ nb_id_title: Object number, or object id, or object title.
288+ Defaults to None (current object).
289+ panel: Panel name. Defaults to None (current panel).
290+
291+ Returns:
292+ Object
293+
294+ Raises:
295+ KeyError: if object not found
296+ """
297+
239298 @abc .abstractmethod
240299 def get_object_uuids (self , panel : str | None = None ) -> list [str ]:
241300 """Get object (signal/image) uuid list for current panel.
@@ -350,7 +409,7 @@ class SimpleBaseProxy(SimpleAbstractCDLControl, metaclass=abc.ABCMeta):
350409 have to set it later (e.g. see SimpleRemoteProxy).
351410 """
352411
353- def __init__ (self , cdlclient : CDLMainWindow | ServerProxy | None = None ) -> None :
412+ def __init__ (self , cdlclient : ServerProxy | None = None ) -> None :
354413 self ._cdl = cdlclient
355414
356415 def get_version (self ) -> str :
0 commit comments