mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 16:50:02 +02:00
updating helpers.py
This commit is contained in:
parent
7861def541
commit
74d121b19d
@ -236,7 +236,9 @@ def convert_arguments_to_hinted_types(
|
|||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def update_value_if_changed(target: Any, attr_name: str | int, new_value: Any) -> None:
|
def update_value_if_changed(
|
||||||
|
target: Any, attr_name_or_index: str | int, new_value: Any
|
||||||
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Updates the value of an attribute or a list element on a target object if the new
|
Updates the value of an attribute or a list element on a target object if the new
|
||||||
value differs from the current one.
|
value differs from the current one.
|
||||||
@ -253,25 +255,22 @@ def update_value_if_changed(target: Any, attr_name: str | int, new_value: Any) -
|
|||||||
Args:
|
Args:
|
||||||
target (Any):
|
target (Any):
|
||||||
The target object that has the attribute or the list.
|
The target object that has the attribute or the list.
|
||||||
attr_name (str | int):
|
attr_name_or_index (str | int):
|
||||||
The name of the attribute or the index of the list element.
|
The name of the attribute or the index of the list element.
|
||||||
new_value (Any):
|
new_value (Any):
|
||||||
The new value for the attribute or the list element.
|
The new value for the attribute or the list element.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isinstance(target, list) and isinstance(attr_name, int):
|
if isinstance(target, list) and isinstance(attr_name_or_index, int):
|
||||||
if target[attr_name] != new_value:
|
if target[attr_name_or_index] != new_value:
|
||||||
target[attr_name] = new_value
|
target[attr_name_or_index] = new_value
|
||||||
elif isinstance(attr_name, str):
|
elif isinstance(attr_name_or_index, str):
|
||||||
# Get the current value of the attribute
|
|
||||||
attr_value = getattr(target, attr_name)
|
|
||||||
|
|
||||||
# If the type matches and the current value is different from the new value,
|
# If the type matches and the current value is different from the new value,
|
||||||
# update the attribute.
|
# update the attribute.
|
||||||
if attr_value != new_value:
|
if getattr(target, attr_name_or_index) != new_value:
|
||||||
setattr(target, attr_name, new_value)
|
setattr(target, attr_name_or_index, new_value)
|
||||||
else:
|
else:
|
||||||
logger.error(f"Incompatible arguments: {target}, {attr_name}.")
|
logger.error(f"Incompatible arguments: {target}, {attr_name_or_index}.")
|
||||||
|
|
||||||
|
|
||||||
def parse_list_attr_and_index(attr_string: str) -> tuple[str, Optional[int]]:
|
def parse_list_attr_and_index(attr_string: str) -> tuple[str, Optional[int]]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user