From 5343f568fa92a45dc8ae8159ae577cd222e0e9cd Mon Sep 17 00:00:00 2001 From: appel_c Date: Thu, 12 Mar 2026 12:53:05 +0100 Subject: [PATCH] refactor(pandabox): adapt block_name_mapping and improve docstrings --- ophyd_devices/devices/panda_box/panda_box.py | 14 ++++++++++++-- ophyd_devices/devices/panda_box/utils.py | 9 +++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ophyd_devices/devices/panda_box/panda_box.py b/ophyd_devices/devices/panda_box/panda_box.py index b1c42ab..fb3cf2e 100644 --- a/ophyd_devices/devices/panda_box/panda_box.py +++ b/ophyd_devices/devices/panda_box/panda_box.py @@ -175,7 +175,17 @@ class DataCallback(TypedDict): class PandaBoxDataConnection: - """Context Manager to conveniently manage the connection to the PandaBox data streams.""" + """ + Context Manager to conveniently manage the connection to the PandaBox data streams. + + Args: + host (str): The hostname of the PandaBox to connect to. + scaled Optional(bool): Whether to request scaled data from the PandaBox. Scaled is recommended + default is this is how data is also presented in the PandaBox UI or received + from the CLI tool. Defaults to True. + socket_timeout Optional(float): Timeout of the socket if no new data is received. Important + to be able to unblock and interrupt the data connection to the Pandabox. + """ def __init__(self, host: str, scaled: bool = True, socket_timeout: float = 0.1): self.host = host @@ -755,7 +765,7 @@ class PandaBox(PSIDeviceBase): # Python attribute names, but also to match the renamed signals provided by the signal_alias mapping. # The mapping is done in two steps: # I. Remove dots '.' from Pandablock keys as they are not valid for Python attribute names - keys = [block_name_mapping(key) for key in keys] + keys = block_name_mapping(keys) # Map keys if mapping is provided. We also need to translate all keys received from the mapped_key = [self.signal_alias.get(key, key) for key in keys] for k in mapped_key: diff --git a/ophyd_devices/devices/panda_box/utils.py b/ophyd_devices/devices/panda_box/utils.py index 024c09b..fa86d51 100644 --- a/ophyd_devices/devices/panda_box/utils.py +++ b/ophyd_devices/devices/panda_box/utils.py @@ -1,6 +1,7 @@ """Module containing utility functions for the PandaBox device.""" import keyword +from collections.abc import Iterable PANDA_AVAIL_PCAP_BLOCKS = [ "INENC1.VAL", @@ -51,9 +52,13 @@ def is_valid_attribute_name(name: str) -> bool: return name.isidentifier() and not keyword.iskeyword(name) -def block_name_mapping(block_name: str) -> str: +def block_name_mapping(block_name: str | Iterable[str]) -> str | list[str]: """Map block names to a format suitable for use as attribute names.""" - return block_name.replace(".", "_") + if isinstance(block_name, str): + return block_name.replace(".", "_") + if isinstance(block_name, Iterable): + return [name.replace(".", "_") for name in block_name] + raise TypeError(f"Unsupported type for block_name: {type(block_name)!r}") def get_pcap_capture_fields():