mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-07 14:00:40 +02:00
updates get_object_attr_from_path to support dictionaries
This commit is contained in:
parent
564eeeb433
commit
8fd83fbd7d
@ -48,15 +48,12 @@ def get_object_attr_from_path(target_obj: Any, path: str) -> Any:
|
|||||||
"""
|
"""
|
||||||
path_list = path.split(".") if path != "" else []
|
path_list = path.split(".") if path != "" else []
|
||||||
for part in path_list:
|
for part in path_list:
|
||||||
|
attr, key = parse_keyed_attribute(part)
|
||||||
try:
|
try:
|
||||||
# Try to split the part into attribute and index
|
if key is not None:
|
||||||
attr, index_str = part.split("[", maxsplit=1)
|
target_obj = getattr(target_obj, attr)[key]
|
||||||
index_str = index_str.replace("]", "")
|
else:
|
||||||
index = int(index_str)
|
target_obj = getattr(target_obj, attr)
|
||||||
target_obj = getattr(target_obj, attr)[index]
|
|
||||||
except ValueError:
|
|
||||||
# No index, so just get the attribute
|
|
||||||
target_obj = getattr(target_obj, part)
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# The attribute doesn't exist
|
# The attribute doesn't exist
|
||||||
logger.debug("Attribute % does not exist in the object.", part)
|
logger.debug("Attribute % does not exist in the object.", part)
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
import pydase
|
||||||
import pytest
|
import pytest
|
||||||
from pydase.utils.helpers import (
|
from pydase.utils.helpers import (
|
||||||
|
get_object_attr_from_path,
|
||||||
is_property_attribute,
|
is_property_attribute,
|
||||||
parse_keyed_attribute,
|
parse_keyed_attribute,
|
||||||
)
|
)
|
||||||
@ -49,6 +51,32 @@ def test_parse_keyed_attributes(attr_name: str, expected: tuple[str, Any]) -> No
|
|||||||
assert parse_keyed_attribute(attr_name) == expected
|
assert parse_keyed_attribute(attr_name) == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_object_attr_from_path() -> None:
|
||||||
|
class SubService(pydase.DataService):
|
||||||
|
name = "SubService"
|
||||||
|
some_int = 1
|
||||||
|
some_float = 1.0
|
||||||
|
|
||||||
|
class MyService(pydase.DataService):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.some_float = 1.0
|
||||||
|
self.subservice = SubService()
|
||||||
|
self.list_attr = [1.0, SubService()]
|
||||||
|
self.dict_attr = {"foo": SubService()}
|
||||||
|
|
||||||
|
service_instance = MyService()
|
||||||
|
|
||||||
|
for attr_name, obj in [
|
||||||
|
("some_float", service_instance.some_float),
|
||||||
|
("subservice", service_instance.subservice),
|
||||||
|
("list_attr[0]", service_instance.list_attr[0]),
|
||||||
|
("list_attr[1]", service_instance.list_attr[1]),
|
||||||
|
("dict_attr['foo']", service_instance.dict_attr["foo"]),
|
||||||
|
]:
|
||||||
|
assert get_object_attr_from_path(service_instance, attr_name) == obj
|
||||||
|
|
||||||
|
|
||||||
# def test_get_nested_dict_by_path() -> None:
|
# def test_get_nested_dict_by_path() -> None:
|
||||||
# obj = {"2.1": "foo", 2.1: "bar"}
|
# obj = {"2.1": "foo", 2.1: "bar"}
|
||||||
# serialized_object = {
|
# serialized_object = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user