Utility function to derive a key for a custom object representing the fully qualified name of that object.
Parameters: |
-
custom_object
(Type )
–
The custom object for which to derive a key.
-
as_lower
(bool , default:
True
)
–
Whether the derived key should be returned as a lower case string.
|
Returns: |
-
str ( str
) –
The fully qualified name of the object in lowercase (e.g. module.submodule.object).
|
Source code in src/pyobjson/utils.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 | def derive_custom_object_key(custom_object: Union[Type, Callable], as_lower: bool = True) -> str:
"""Utility function to derive a key for a custom object representing the fully qualified name of that object.
Args:
custom_object (Type): The custom object for which to derive a key.
as_lower (bool, optional): Whether the derived key should be returned as a lower case string.
Returns:
str: The fully qualified name of the object in lowercase (e.g. module.submodule.object).
"""
# avoid including module if no module exists or the object is in the Python builtins
if (obj_module := getattr(custom_object, "__module__", None)) and obj_module != "builtins":
return (
f"{obj_module.lower()}.{custom_object.__qualname__.lower()}"
if as_lower
else f"{obj_module}.{custom_object.__qualname__}"
)
else:
return custom_object.__qualname__.lower() if as_lower else custom_object.__qualname__
|