From 615bf294e1252765469b6dbbd1c7006ec09fc177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Tue, 7 Nov 2023 16:14:41 +0100 Subject: [PATCH] moves get_attribute_doc to helpers --- src/pydase/utils/helpers.py | 11 +++++++++++ src/pydase/utils/serializer.py | 32 +++++++++++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/pydase/utils/helpers.py b/src/pydase/utils/helpers.py index 7ec6acb..3b35942 100644 --- a/src/pydase/utils/helpers.py +++ b/src/pydase/utils/helpers.py @@ -1,3 +1,4 @@ +import inspect import logging import re from itertools import chain @@ -17,6 +18,16 @@ STANDARD_TYPES = ( ) +def get_attribute_doc(attr: Any) -> Optional[str]: + """This function takes an input attribute attr and returns its documentation + string if it's different from the documentation of its type, otherwise, + it returns None. + """ + attr_doc = inspect.getdoc(attr) + attr_class_doc = inspect.getdoc(type(attr)) + return attr_doc if attr_class_doc != attr_doc else None + + def get_class_and_instance_attributes(obj: object) -> dict[str, Any]: """Dictionary containing all attributes (both instance and class level) of a given object. diff --git a/src/pydase/utils/serializer.py b/src/pydase/utils/serializer.py index af909aa..4e560db 100644 --- a/src/pydase/utils/serializer.py +++ b/src/pydase/utils/serializer.py @@ -2,12 +2,13 @@ import inspect import logging from collections.abc import Callable from enum import Enum -from typing import Any, Optional, cast +from typing import Any, Optional import pydase.units as u from pydase.data_service.abstract_data_service import AbstractDataService from pydase.utils.helpers import ( STANDARD_TYPES, + get_attribute_doc, get_component_class_names, parse_list_attr_and_index, ) @@ -15,17 +16,8 @@ from pydase.utils.helpers import ( logger = logging.getLogger(__name__) -class Serializer: - @staticmethod - def get_attribute_doc(attr: Any) -> Optional[str]: - """This function takes an input attribute attr and returns its documentation - string if it's different from the documentation of its type, otherwise, - it returns None. - """ - attr_doc = inspect.getdoc(attr) - attr_class_doc = inspect.getdoc(type(attr)) - return attr_doc if attr_class_doc != attr_doc else None +class Serializer: @staticmethod def serialize_object(obj: Any) -> dict[str, Any]: result: dict[str, Any] = {} @@ -54,7 +46,7 @@ class Serializer: obj_type = type(obj).__name__ value = obj readonly = False - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) result = { "type": obj_type, "value": value, @@ -68,7 +60,7 @@ class Serializer: def _serialize_enum(obj: Enum) -> dict[str, Any]: value = obj.name readonly = False - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) if type(obj).__base__.__name__ == "ColouredEnum": obj_type = "ColouredEnum" else: @@ -88,7 +80,7 @@ class Serializer: def _serialize_Quantity(obj: u.Quantity) -> dict[str, Any]: obj_type = "Quantity" readonly = False - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) value = {"magnitude": obj.m, "unit": str(obj.u)} return { "type": obj_type, @@ -101,7 +93,7 @@ class Serializer: def _serialize_dict(obj: dict[str, Any]) -> dict[str, Any]: obj_type = "dict" readonly = False - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) value = {key: Serializer.serialize_object(val) for key, val in obj.items()} return { "type": obj_type, @@ -114,7 +106,7 @@ class Serializer: def _serialize_list(obj: list[Any]) -> dict[str, Any]: obj_type = "list" readonly = False - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) value = [Serializer.serialize_object(o) for o in obj] return { "type": obj_type, @@ -128,7 +120,7 @@ class Serializer: obj_type = "method" value = None readonly = True - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) # Store parameters and their anotations in a dictionary sig = inspect.signature(obj) @@ -158,7 +150,7 @@ class Serializer: @staticmethod def _serialize_DataService(obj: AbstractDataService) -> dict[str, Any]: readonly = False - doc = Serializer.get_attribute_doc(obj) + doc = get_attribute_doc(obj) obj_type = type(obj).__name__ if type(obj).__name__ not in get_component_class_names(): obj_type = "DataService" @@ -205,9 +197,7 @@ class Serializer: if isinstance(getattr(obj.__class__, key, None), property): prop: property = getattr(obj.__class__, key) value[key]["readonly"] = prop.fset is None - value[key]["doc"] = Serializer.get_attribute_doc( - prop - ) # overwrite the doc + value[key]["doc"] = get_attribute_doc(prop) # overwrite the doc return { "type": obj_type,