Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pyaml/control/abstract_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ def _iter_devices_and_ranges(devs: DeviceAccess | DeviceAccessList):
- DeviceAccessList: yields N items based on get_devices() and get_range() flattening
"""
# Single device
if hasattr(devs, "get") and hasattr(devs, "get_range") and not hasattr(devs, "get_devices"):
if isinstance(devs, DeviceAccess):
r = devs.get_range()
if r is None:
r = [None, None]
return [(devs, [r[0], r[1]])]

# Device list (expects get_devices() + get_range() flat list)
devices = devs.get_devices()
flat = np.asarray(devs.get_range(), dtype=object).ravel()
if (flat.size % 2) != 0:
# get_range() return a flat list
flat = devs.get_range()
if (len(flat) % 2) != 0:
raise ValueError(f"dev_range must have an even length, got {flat.size}")

# Reshape
pairs = []
for i, d in enumerate(devices):
pairs.append((d, [flat[2 * i], flat[2 * i + 1]]))
for i in range(devs.len()):
pairs.append((devs.get_device_at(i), [flat[2 * i], flat[2 * i + 1]]))
return pairs


Expand Down Expand Up @@ -196,7 +196,7 @@ def unit(self) -> str:
return self._devs.unit()

def nb_device(self) -> int:
return self._devs.__len__()
return self._devs.len()


# ------------------------------------------------------------------------------
Expand Down
19 changes: 12 additions & 7 deletions pyaml/control/deviceaccesslist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
import numpy.typing as npt

from .deviceaccess import DeviceAccess
from .readback_value import Value


class DeviceAccessList(list[DeviceAccess], metaclass=ABCMeta):
class DeviceAccessList(metaclass=ABCMeta):
"""
Abstract class providing access to a list of control system float variable
Abstract class providing access to a list of control system variales.
Internal structure depends on the backend and might not be trivially iterable.
"""

@abstractmethod
def add_devices(self, devices: DeviceAccess | list[DeviceAccess]):
"""Add a DeviceAccess to this list"""
"""Add a DeviceAccess (or a list) to this list"""
pass

@abstractmethod
def get_devices(self) -> DeviceAccess | list[DeviceAccess]:
"""Get the DeviceAccess list"""
def get_device_at(self, index: int) -> DeviceAccess:
"""Returns the device at the given index"""
pass

@abstractmethod
def len(self) -> int:
"""Get the DeviceAccessList length"""
pass

@abstractmethod
Expand Down Expand Up @@ -56,7 +61,7 @@ def get_range(self) -> list[float]:
Returns
-------
list[float]
List containing [min, max] values
List containing [min0, max0, min1, max1, ...] values
"""
pass

Expand Down
3 changes: 0 additions & 3 deletions tests/dummy_cs/tango-pyaml/tango/pyaml/controlsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,5 @@ def name(self) -> str:
def get_aggregator(self) -> str | None:
return MultiAttribute()

def vector_aggregator(self) -> str | None:
return self._cfg.vector_aggregator

def __repr__(self):
return repr(self._cfg).replace("ConfigModel", self.__class__.__name__)
28 changes: 14 additions & 14 deletions tests/dummy_cs/tango-pyaml/tango/pyaml/multi_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class MultiAttribute(DeviceAccessList):
def __init__(self):
super().__init__()
self._items = []

def add_devices(self, devices: DeviceAccess | list[DeviceAccess]):
if isinstance(devices, list):
Expand All @@ -23,54 +23,54 @@ def add_devices(self, devices: DeviceAccess | list[DeviceAccess]):
(tango.pyaml.attribute) but got
({device.__class__.__name__})"""
)
super().extend(devices)
self._items.extend(devices)
else:
if not isinstance(devices, Attribute):
raise pyaml.PyAMLException(
f"""Device must be an instance of Attribute
(tango.pyaml.attribute) but got
({devices.__class__.__name__})"""
)
super().append(devices)
self._items.append(devices)

def get_devices(self) -> DeviceAccess | list[DeviceAccess]:
if len(self) == 1:
return self[0]
else:
return self
def len(self) -> int:
return len(self._items)

def get_device_at(self, index: int) -> DeviceAccess:
return self._items[index]

def set(self, value: npt.NDArray[np.float64]):
print(f"MultiAttribute.set({len(value)} values)")
global LAST_NB_WRITTEN
LAST_NB_WRITTEN += len(value)
for idx, a in enumerate(self):
for idx, a in enumerate(self._items):
a.set(value[idx])

def set_and_wait(self, value: npt.NDArray[np.float64]):
pass

def get(self) -> npt.NDArray[np.float64]:
print(f"MultiAttribute.get({len(self)} values)")
return np.array([a.get() for a in self])
print(f"MultiAttribute.get({len(self._items)} values)")
return np.array([a.get() for a in self._items])

def readback(self) -> np.array:
return np.array([])

def unit(self) -> list[str]:
return [a.unit() for a in self]
return [a.unit() for a in self._items]

def get_last_nb_written(self) -> int:
return self.__last_nb_written

def get_range(self) -> list[float]:
attr_range: list[float] = []
for device in self:
for device in self._items:
attr_range.extend(device.get_range())
return attr_range

def check_device_availability(self) -> bool:
available = False
for device in self:
for device in self._items:
available = device.check_device_availability()
if not available:
break
Expand Down
Loading