Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make name separator configurable #675

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions src/ophyd_async/core/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def log(self) -> LoggerAdapter:
getLogger("ophyd_async.devices"), {"ophyd_async_device_name": self.name}
)

def set_name(self, name: str):
def set_name(self, name: str, separator: str = "-"):
coretl marked this conversation as resolved.
Show resolved Hide resolved
"""Set ``self.name=name`` and each ``self.child.name=name+"-child"``.

Parameters
Expand All @@ -110,8 +110,8 @@ def set_name(self, name: str):
if "log" in self.__dict__:
del self.log
for child_name, child in self.children():
child_name = f"{self.name}-{child_name.strip('_')}" if self.name else ""
child.set_name(child_name)
child_name = f"{self.name}{separator}{child_name}" if self.name else ""
child.set_name(child_name, separator)

def __setattr__(self, name: str, value: Any) -> None:
# Bear in mind that this function is called *a lot*, so
Expand Down
4 changes: 2 additions & 2 deletions src/ophyd_async/epics/demo/_mover.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def __init__(self, prefix: str, name="") -> None:

super().__init__(name=name)

def set_name(self, name: str):
super().set_name(name)
def set_name(self, name: str, separator: str = "-"):
super().set_name(name, separator)
# Readback should be named the same as its parent in read()
self.readback.set_name(name)

Expand Down
4 changes: 2 additions & 2 deletions src/ophyd_async/epics/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def __init__(self, prefix: str, name="") -> None:

super().__init__(name=name)

def set_name(self, name: str):
super().set_name(name)
def set_name(self, name: str, separator: str = "-"):
super().set_name(name, separator)
# Readback should be named the same as its parent in read()
self.user_readback.set_name(name)

Expand Down
15 changes: 13 additions & 2 deletions tests/core/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def test_children_of_device_have_set_names_and_get_connected(
):
assert parent.name == "parent"
assert parent.child1.name == "parent-child1"
assert parent._child2.name == "parent-child2"
assert parent._child2.name == "parent-_child2"
assert parent.dict_with_children.name == "parent-dict_with_children"
assert parent.dict_with_children[123].name == "parent-dict_with_children-123"

Expand All @@ -112,6 +112,17 @@ async def test_children_of_device_have_set_names_and_get_connected(
assert parent.dict_with_children[123].connected


async def test_children_of_device_with_different_separator(
parent: DummyDeviceGroup,
):
parent.set_name("parent", separator="_")
assert parent.name == "parent"
assert parent.child1.name == "parent_child1"
assert parent._child2.name == "parent__child2"
assert parent.dict_with_children.name == "parent_dict_with_children"
assert parent.dict_with_children[123].name == "parent_dict_with_children_123"


async def test_device_with_device_collector():
async with DeviceCollector(mock=True):
parent = DummyDeviceGroup("parent")
Expand All @@ -120,7 +131,7 @@ async def test_device_with_device_collector():
assert parent.parent is None
assert parent.child1.name == "parent-child1"
assert parent.child1.parent == parent
assert parent._child2.name == "parent-child2"
assert parent._child2.name == "parent-_child2"
assert parent._child2.parent == parent
assert parent.dict_with_children.name == "parent-dict_with_children"
assert parent.dict_with_children.parent == parent
Expand Down
2 changes: 1 addition & 1 deletion tests/epics/demo/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async def test_assembly_renaming() -> None:
thing.set_name("foo")
assert thing.x.name == "foo-x"
assert thing.x.velocity.name == "foo-x-velocity"
assert thing.x.stop_.name == "foo-x-stop"
assert thing.x.stop_.name == "foo-x-stop_"


async def test_dynamic_sensor_group_disconnected():
Expand Down
Loading