renaming function, updating docstring

This commit is contained in:
Mose Müller 2023-11-07 17:15:54 +01:00
parent 03fee3f88c
commit 393b025648
3 changed files with 17 additions and 47 deletions

View File

@ -19,7 +19,7 @@ from pydase.utils.helpers import (
parse_list_attr_and_index, parse_list_attr_and_index,
update_value_if_changed, update_value_if_changed,
) )
from pydase.utils.serializer import Serializer, generate_paths_from_DataService_dict from pydase.utils.serializer import Serializer, generate_serialized_data_paths
from pydase.utils.warnings import ( from pydase.utils.warnings import (
warn_if_instance_class_does_not_inherit_from_DataService, warn_if_instance_class_does_not_inherit_from_DataService,
) )
@ -164,7 +164,7 @@ class DataService(rpyc.Service, AbstractDataService):
# Traverse the serialized representation and set the attributes of the class # Traverse the serialized representation and set the attributes of the class
serialized_class = self.serialize() serialized_class = self.serialize()
for path in generate_paths_from_DataService_dict(json_dict): for path in generate_serialized_data_paths(json_dict):
value = get_nested_value_from_DataService_by_path_and_key( value = get_nested_value_from_DataService_by_path_and_key(
json_dict, path=path json_dict, path=path
) )

View File

@ -7,7 +7,7 @@ from typing import TYPE_CHECKING, Any, Optional, cast
import pydase.units as u import pydase.units as u
from pydase.data_service.data_service_cache import DataServiceCache from pydase.data_service.data_service_cache import DataServiceCache
from pydase.utils.helpers import get_nested_value_from_DataService_by_path_and_key from pydase.utils.helpers import get_nested_value_from_DataService_by_path_and_key
from pydase.utils.serializer import generate_paths_from_DataService_dict from pydase.utils.serializer import generate_serialized_data_paths
if TYPE_CHECKING: if TYPE_CHECKING:
from pydase import DataService from pydase import DataService
@ -101,7 +101,7 @@ class StateManager:
return return
serialized_class = self.cache serialized_class = self.cache
for path in generate_paths_from_DataService_dict(json_dict): for path in generate_serialized_data_paths(json_dict):
value = get_nested_value_from_DataService_by_path_and_key( value = get_nested_value_from_DataService_by_path_and_key(
json_dict, path=path json_dict, path=path
) )

View File

@ -331,51 +331,23 @@ def get_nested_dict_by_attr_and_index(
return serialization_dict return serialization_dict
def generate_paths_from_DataService_dict( def generate_serialized_data_paths(
data: dict, parent_path: str = "" data: dict[str, Any], parent_path: str = ""
) -> list[str]: ) -> list[str]:
""" """
Recursively generate paths from a dictionary representing a DataService object. Generate a list of access paths for all attributes in a dictionary representing
data serialized with `pydase.utils.serializer.Serializer`, excluding those that are
This function traverses through a nested dictionary, which is typically obtained methods.
from serializing a DataService object. The function generates a list where each
element is a string representing the path to each terminal value in the original
dictionary.
The paths are represented as strings, with dots ('.') denoting nesting levels and
square brackets ('[]') denoting list indices.
Args: Args:
data (dict): The input dictionary to generate paths from. This is typically data: The dictionary representing serialized data, typically produced by
obtained from serializing a DataService object. `pydase.utils.serializer.Serializer`.
parent_path (str, optional): The current path up to the current level of parent_path: The base path to prepend to the keys in the `data` dictionary to
recursion. Defaults to ''. form the access paths. Defaults to an empty string.
Returns: Returns:
list[str]: A list with paths as elements. A list of strings where each string is a dot-notation access path to an
attribute in the serialized data.
Note:
The function ignores keys whose "type" is "method", as these represent methods
of the DataService object and not its state.
Example:
-------
>>> {
... "attr1": {"type": "int", "value": 10},
... "attr2": {
... "type": "list",
... "value": [{"type": "int", "value": 1}, {"type": "int", "value": 2}],
... },
... "add": {
... "type": "method",
... "async": False,
... "parameters": {"a": "float", "b": "int"},
... "doc": "Returns the sum of the numbers a and b.",
... },
... }
>>> print(generate_paths_from_DataService_dict(nested_dict))
[attr1, attr2[0], attr2[1]]
""" """
paths = [] paths = []
@ -385,15 +357,13 @@ def generate_paths_from_DataService_dict(
continue continue
new_path = f"{parent_path}.{key}" if parent_path else key new_path = f"{parent_path}.{key}" if parent_path else key
if isinstance(value["value"], dict) and value["type"] != "Quantity": if isinstance(value["value"], dict) and value["type"] != "Quantity":
paths.extend(generate_paths_from_DataService_dict(value["value"], new_path)) # type: ignore paths.extend(generate_serialized_data_paths(value["value"], new_path)) # type: ignore
elif isinstance(value["value"], list): elif isinstance(value["value"], list):
for index, item in enumerate(value["value"]): for index, item in enumerate(value["value"]):
indexed_key_path = f"{new_path}[{index}]" indexed_key_path = f"{new_path}[{index}]"
if isinstance(item["value"], dict): if isinstance(item["value"], dict):
paths.extend( # type: ignore paths.extend( # type: ignore
generate_paths_from_DataService_dict( generate_serialized_data_paths(item["value"], indexed_key_path)
item["value"], indexed_key_path
)
) )
else: else:
paths.append(indexed_key_path) # type: ignore paths.append(indexed_key_path) # type: ignore