feat: adding utils.helper

This commit is contained in:
Mose Müller 2023-08-02 12:06:20 +02:00
parent bb48ba237d
commit 8460759a31
2 changed files with 22 additions and 1 deletions

View File

@ -1,3 +1,7 @@
from .helpers import get_class_and_instance_attributes
from .warnings import warn_if_instance_class_does_not_inherit_from_DataService
__all__ = ["warn_if_instance_class_does_not_inherit_from_DataService"]
__all__ = [
"warn_if_instance_class_does_not_inherit_from_DataService",
"get_class_and_instance_attributes",
]

View File

@ -0,0 +1,17 @@
from itertools import chain
from typing import Any
def get_class_and_instance_attributes(obj: object) -> dict[str, Any]:
"""Dictionary containing all attributes (both instance and class level) of a
given object.
If an attribute exists at both the instance and class level,the value from the
instance attribute takes precedence.
The __root__ object is removed as this will lead to endless recursion in the for
loops.
"""
attrs = dict(chain(type(obj).__dict__.items(), obj.__dict__.items()))
attrs.pop("__root__")
return attrs