Utilities

Python Object JSON Tool pyobjson.utils module.

Attributes:
  • __author__ (str) –

    Python package template author.

  • __email__ (str) –

    Python package template author email.

Functions:

Name Description
derive_custom_object_key

Utility function to derive a key for a custom object representing the fully qualified name of that object.

derive_custom_callable_value

Utility function to derive a string value from a custom callable object.

get_nested_subclasses

Recursive utility function to retrieve all nested subclasses of a custom class.

derive_custom_object_key

derive_custom_object_key(custom_object, as_lower=True)

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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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).

    """
    if not hasattr(custom_object, "__qualname__"):
        custom_object = custom_object.__class__

    # 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__

derive_custom_callable_value

derive_custom_callable_value(custom_callable)

Utility function to derive a string value from a custom callable object.

Parameters:
  • custom_callable (Callable) –

    Custom callable object for which to derive a string representation value.

Returns:
  • str( str ) –

    A string representing the custom callable object that can be used to import and call that object.

Source code in src/pyobjson/utils.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def derive_custom_callable_value(custom_callable: Callable) -> str:
    """Utility function to derive a string value from a custom callable object.

    Args:
        custom_callable (Callable): Custom callable object for which to derive a string representation value.

    Returns:
        str: A string representing the custom callable object that can be used to import and call that object.

    """
    arg_spec_str = ",".join(
        [
            f"{k}:{v.__name__ if hasattr(v, '__name__') else v.__class__.__name__}"
            for k, v in getfullargspec(custom_callable).annotations.items()
        ]
    )
    return f"{derive_custom_object_key(custom_callable)}::{arg_spec_str}"

get_nested_subclasses

get_nested_subclasses(custom_class)

Recursive utility function to retrieve all nested subclasses of a custom class.

Parameters:
  • custom_class (Type) –

    Custom class from which to recursively retrieve all nested subclasses.

Returns:
  • list( Type ) –

    A list of custom classes derived from the subclass tree of a custom class.

Source code in src/pyobjson/utils.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def get_nested_subclasses(custom_class: Type) -> List[Type]:
    """Recursive utility function to retrieve all nested subclasses of a custom class.

    Args:
        custom_class (Type): Custom class from which to recursively retrieve all nested subclasses.

    Returns:
        list(Type): A list of custom classes derived from the subclass tree of a custom class.

    """
    custom_subclasses = custom_class.__subclasses__()
    for custom_subclass in custom_subclasses:
        custom_subclasses.extend(get_nested_subclasses(custom_subclass))
    return list(set(custom_subclasses))