Skip to content

Commit 12f8180

Browse files
committed
Remote API: added dict-like interface
1 parent b04187f commit 12f8180

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# DataLab Simple Client Releases #
22

3+
## Version 0.4.0 ##
4+
5+
Changes:
6+
7+
* Remote API:
8+
* Added dict-like interface (e.g. `proxy['obj123']`)
9+
310
## Version 0.3.0 ##
411

512
Changes:

cdlclient/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from cdlclient.baseproxy import SimpleBaseProxy
1717
from cdlclient.remote import SimpleRemoteProxy
1818

19-
__version__ = "0.3.0"
19+
__version__ = "0.4.0"
2020
__docurl__ = "https://cdlclient.readthedocs.io/en/latest/"
2121
__homeurl__ = "https://github.com/Codra-Ingenierie-Informatique/DataLabSimpleClient/"
2222
__supporturl__ = "https://github.com/Codra-Ingenierie-Informatique/DataLabSimpleClient/issues/new/choose"

cdlclient/baseproxy.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import numpy as np
1919

2020
if 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

2627
class 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

Comments
 (0)