Skip to content

Classes

toolbox_python.classes 🔗

Summary

The classes module is designed for functions to be executed on classes; not within classes. For any methods/functions that should be added to classes, you should consider re-designing the original class, or sub-classing it to make further alterations.

get_full_class_name 🔗

get_full_class_name(obj: Any) -> str

Summary

This function is designed to extract the full name of a class, including the name of the module from which it was loaded.

Details

Note, this is designed to retrieve the underlying class name of an object, not the instance name of an object. This is useful for debugging purposes, or for logging.

Parameters:

Name Type Description Default
obj Any

The object for which you want to retrieve the full name.

required

Returns:

Type Description
str

The full name of the class of the object.

Examples
Set up
1
>>> from toolbox_python.classes import get_full_class_name

Example 1: Check the name of a standard class
1
>>> print(get_full_class_name(str))
Terminal
str

Conclusion: Successful class name extraction.

Example 2: Check the name of an imported class
1
2
>>> from random import Random
>>> print(get_full_class_name(Random))
Terminal
random.Random

Conclusion: Successful class name extraction.

Credit

Full credit goes to:
https://stackoverflow.com/questions/18176602/how-to-get-the-name-of-an-exception-that-was-caught-in-python#answer-58045927

Source code in src/toolbox_python/classes.py
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def get_full_class_name(obj: Any) -> str:
    """
    !!! note "Summary"
        This function is designed to extract the full name of a class, including the name of the module from which it was loaded.

    ???+ abstract "Details"
        Note, this is designed to retrieve the underlying _class name_ of an object, not the _instance name_ of an object. This is useful for debugging purposes, or for logging.

    Params:
        obj (Any):
            The object for which you want to retrieve the full name.

    Returns:
        (str):
            The full name of the class of the object.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> from toolbox_python.classes import get_full_class_name
        ```

        ```{.py .python linenums="1" title="Example 1: Check the name of a standard class"}
        >>> print(get_full_class_name(str))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        str
        ```
        !!! success "Conclusion: Successful class name extraction."
        </div>

        ```{.py .python linenums="1" title="Example 2: Check the name of an imported class"}
        >>> from random import Random
        >>> print(get_full_class_name(Random))
        ```
        <div class="result" markdown>
        ```{.sh .shell title="Terminal"}
        random.Random
        ```
        !!! success "Conclusion: Successful class name extraction."
        </div>

    ??? success "Credit"
        Full credit goes to:<br>
        https://stackoverflow.com/questions/18176602/how-to-get-the-name-of-an-exception-that-was-caught-in-python#answer-58045927
    """
    module: str = obj.__class__.__module__
    if module is None or module == str.__class__.__module__:
        return obj.__class__.__name__
    return module + "." + obj.__class__.__name__