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

refactoring get_objects_by_role() in HardwareObjectNode class #857

Merged
merged 1 commit into from
Feb 29, 2024
Merged
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
43 changes: 14 additions & 29 deletions mxcubecore/BaseHardwareObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,39 +464,24 @@ def get_objects(
yield obj

def get_object_by_role(self, role: str) -> Union["HardwareObject", None]:
"""Get hardware object by role.
"""Get hardware object by role.

Args:
role (str): Role.

Returns:
Union[HardwareObject, None]: Hardware object.
"""
object = None
obj: Self = self
objects = []
role = str(role).lower()
Args:
role (str): Role.

while True:
if role in obj._objects_by_role:
return obj._objects_by_role[role]

for object in obj:
objects.append(object)
Returns:
Union[HardwareObject, None]: Hardware object.
"""
role = str(role).lower()
objects = [self]

try:
obj = objects.pop()
except IndexError:
break
else:
object = obj.get_object_by_role(role)
if object is not None:
return object
for curr in objects:
result = curr._objects_by_role.get(role)
if result is None:
objects.extend(obj for obj in curr if obj)

if len(objects) > 0:
obj = objects.pop()
else:
break
else :
return result

def objects_names(self) -> List[Union[str, None]]:
"""Return hardware object names.
Expand Down
Loading