mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 00:40:01 +02:00
replaces parse_list_attr_and_index with parse_keyed_attribute to support dictionaries
This commit is contained in:
parent
0ac4049282
commit
32e2a8a4d1
@ -9,7 +9,7 @@ from pydase.data_service.data_service_cache import DataServiceCache
|
|||||||
from pydase.utils.helpers import (
|
from pydase.utils.helpers import (
|
||||||
get_object_attr_from_path,
|
get_object_attr_from_path,
|
||||||
is_property_attribute,
|
is_property_attribute,
|
||||||
parse_list_attr_and_index,
|
parse_keyed_attribute,
|
||||||
)
|
)
|
||||||
from pydase.utils.serialization.deserializer import loads
|
from pydase.utils.serialization.deserializer import loads
|
||||||
from pydase.utils.serialization.serializer import (
|
from pydase.utils.serialization.serializer import (
|
||||||
@ -240,7 +240,7 @@ class StateManager:
|
|||||||
|
|
||||||
# If attr_name corresponds to a list entry, extract the attr_name and the
|
# If attr_name corresponds to a list entry, extract the attr_name and the
|
||||||
# index
|
# index
|
||||||
attr_name, index = parse_list_attr_and_index(attr_name)
|
attr_name, index = parse_keyed_attribute(attr_name)
|
||||||
|
|
||||||
# Update path to reflect the attribute without list indices
|
# Update path to reflect the attribute without list indices
|
||||||
path = f"{parent_path}.{attr_name}" if parent_path != "" else attr_name
|
path = f"{parent_path}.{attr_name}" if parent_path != "" else attr_name
|
||||||
|
@ -101,39 +101,51 @@ def update_value_if_changed(
|
|||||||
logger.error("Incompatible arguments: %s, %s.", target, attr_name_or_index)
|
logger.error("Incompatible arguments: %s, %s.", target, attr_name_or_index)
|
||||||
|
|
||||||
|
|
||||||
def parse_list_attr_and_index(attr_string: str) -> tuple[str, int | None]:
|
def parse_keyed_attribute(attr_string: str) -> tuple[str, str | float | int | None]:
|
||||||
"""
|
"""
|
||||||
Parses an attribute string and extracts a potential list attribute name and its
|
Parses an attribute string and extracts a potential attribute name and its key.
|
||||||
index.
|
The key can be a string (for dictionary keys) or an integer (for list indices).
|
||||||
Logs an error if the index is not a valid digit.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
attr_string (str):
|
attr_string (str):
|
||||||
The attribute string to parse. Can be a regular attribute name (e.g.,
|
The attribute string to parse. Can be a regular attribute name (e.g.,
|
||||||
'attr_name') or a list attribute with an index (e.g., 'list_attr[2]').
|
'attr_name'), a list attribute with an index (e.g., 'list_attr[2]'), or
|
||||||
|
a dictionary attribute with a key (e.g., 'dict_attr["key"]' or
|
||||||
|
'dict_attr[0]').
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple[str, Optional[int]]:
|
tuple[str, str | float | int | None]:
|
||||||
A tuple containing the attribute name as a string and the index as an
|
A tuple containing the attribute name and the key as either a string,
|
||||||
integer if present, otherwise None.
|
an integer if it's a digit, or None if no key is present.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> parse_attribute_and_index('list_attr[2]')
|
```python
|
||||||
('list_attr', 2)
|
>>> parse_keyed_attribute('list_attr[2]')
|
||||||
>>> parse_attribute_and_index('attr_name')
|
("list_attr", 2)
|
||||||
('attr_name', None)
|
>>> parse_keyed_attribute('attr_name')
|
||||||
|
("attr_name", None)
|
||||||
|
>>> parse_keyed_attribute('dict_attr["key"]')
|
||||||
|
("dict_attr", 'key')
|
||||||
|
>>> parse_keyed_attribute("dict_attr["0"]")
|
||||||
|
("dict_attr", "0")
|
||||||
|
>>> parse_keyed_attribute("dict_attr[0]")
|
||||||
|
("dict_attr", 0)
|
||||||
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
index = None
|
key = None
|
||||||
attr_name = attr_string
|
attr_name = attr_string
|
||||||
if "[" in attr_string and attr_string.endswith("]"):
|
if "[" in attr_string and attr_string.endswith("]"):
|
||||||
attr_name, index_part = attr_string.split("[", 1)
|
attr_name, key_part = attr_string.split("[", 1)
|
||||||
index_part = index_part.rstrip("]")
|
key_part = key_part.rstrip("]")
|
||||||
if index_part.isdigit():
|
# Remove quotes if present (supports both single and double quotes)
|
||||||
index = int(index_part)
|
if key_part.startswith('"') and key_part.endswith('"'):
|
||||||
|
key = key_part[1:-1]
|
||||||
|
elif "." in key_part:
|
||||||
|
key = float(key_part)
|
||||||
else:
|
else:
|
||||||
logger.error("Invalid index format in key: %s", attr_name)
|
key = int(key_part)
|
||||||
return attr_name, index
|
return attr_name, key
|
||||||
|
|
||||||
|
|
||||||
def get_component_classes() -> list[type]:
|
def get_component_classes() -> list[type]:
|
||||||
|
@ -13,7 +13,7 @@ from pydase.utils.helpers import (
|
|||||||
get_attribute_doc,
|
get_attribute_doc,
|
||||||
get_component_classes,
|
get_component_classes,
|
||||||
get_data_service_class_reference,
|
get_data_service_class_reference,
|
||||||
parse_list_attr_and_index,
|
parse_keyed_attribute,
|
||||||
render_in_frontend,
|
render_in_frontend,
|
||||||
)
|
)
|
||||||
from pydase.utils.serialization.types import (
|
from pydase.utils.serialization.types import (
|
||||||
@ -402,7 +402,7 @@ def get_next_level_dict_by_key(
|
|||||||
SerializationValueError: If the expected nested structure is not a dictionary.
|
SerializationValueError: If the expected nested structure is not a dictionary.
|
||||||
"""
|
"""
|
||||||
# Check if the key contains an index part like 'attr_name[<index>]'
|
# Check if the key contains an index part like 'attr_name[<index>]'
|
||||||
attr_name, index = parse_list_attr_and_index(attr_name)
|
attr_name, index = parse_keyed_attribute(attr_name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if index is not None:
|
if index is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user