-
Notifications
You must be signed in to change notification settings - Fork 30
Add name filter to attribute setter on Segment
#598
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
base: master
Are you sure you want to change the base?
Changes from all commits
541a74c
5cd77be
f1c5d71
8cc12c3
786184d
aae4436
713be0c
3f5a23e
0834629
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -584,28 +584,70 @@ def get_beam_attrs_along_segment( | |
| else broadcasted_results[0] | ||
| ) | ||
|
|
||
| def get_attr_from_every_element( | ||
| self, | ||
| attr_name: str, | ||
| filter_type: type[Element] | tuple[type[Element]] | None = None, | ||
| filter_name: str | None = None, | ||
|
jank324 marked this conversation as resolved.
|
||
| is_recursive: bool = True, | ||
| ) -> list[Any]: | ||
| """ | ||
| Get an attribute from every element in the segment filtered by type and/or name. | ||
| :param attr_name: Name of the attribute to retrieve from each element. | ||
| :param filter_type: Type of the elements to get the attribute from. | ||
| :param filter_name: Name of the elements to get the attribute from. | ||
| :param is_recursive: If `True`, this method is applied to nested `Segment`s as | ||
| well. If `False`, only the elements directly in the top-level `Segment` are | ||
| considered. | ||
| :return: List of attribute values from the filtered elements. | ||
| """ | ||
| attrs = [] | ||
| for element in self.elements: | ||
| if (filter_type is None or isinstance(element, filter_type)) and ( | ||
| filter_name is None or element.name in filter_name | ||
| ): | ||
| attrs.append(getattr(element, attr_name)) | ||
| elif is_recursive and isinstance(element, Segment): | ||
| attrs.extend( | ||
| element.get_attr_from_every_element( | ||
|
jank324 marked this conversation as resolved.
|
||
| attr_name, | ||
| filter_type=filter_type, | ||
| filter_name=filter_name, | ||
| is_recursive=True, | ||
| ) | ||
| ) | ||
| return attrs | ||
|
|
||
| def set_attrs_on_every_element( | ||
| self, | ||
| filter_type: type[Element] | tuple[type[Element]] | None = None, | ||
| filter_name: str | tuple[str] | None = None, | ||
| is_recursive: bool = True, | ||
| **kwargs: dict[str, Any], | ||
| ) -> None: | ||
| """ | ||
| Set attributes on every element of a specific type in the segment. | ||
| Set attributes on every element type in the segment filtered by type and/or | ||
| name. | ||
|
|
||
| :param filter_type: Type of the elements to set the attributes for. | ||
| :param is_recursive: If `True`, the this method is applied to nested `Segment`s | ||
| as well. If `False`, only the elements directly in the top-level `Segment` | ||
| are considered. | ||
| :param filter_name: Name of the element to set the attributes for. | ||
| :param is_recursive: If `True`, this method is applied to nested `Segment`s as | ||
| well. If `False`, only the elements directly in the top-level `Segment` are | ||
| considered. | ||
| :param kwargs: Attributes to set and their values. | ||
| """ | ||
| for element in self.elements: | ||
| if filter_type is None or isinstance(element, filter_type): | ||
| if (filter_type is None or isinstance(element, filter_type)) and ( | ||
| filter_name is None or element.name in filter_name | ||
| ): | ||
| for key, value in kwargs.items(): | ||
| setattr(element, key, value) | ||
| elif is_recursive and isinstance(element, Segment): | ||
| element.set_attrs_on_every_element( | ||
| filter_type=filter_type, is_recursive=True, **kwargs | ||
| filter_type=filter_type, | ||
| filter_name=filter_name, | ||
| is_recursive=True, | ||
| **kwargs, | ||
| ) | ||
|
Comment on lines
+640
to
651
|
||
|
|
||
| def plot( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be moved to new version