Coverage for src/toolbox_python/classes.py: 100%
7 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-13 07:24 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-13 07:24 +0000
1# ============================================================================ #
2# #
3# Title : Classes #
4# Purpose : Contain functions which can be run on classes to extract #
5# general information. #
6# #
7# ============================================================================ #
10# ---------------------------------------------------------------------------- #
11# #
12# Overview ####
13# #
14# ---------------------------------------------------------------------------- #
17# ---------------------------------------------------------------------------- #
18# Description ####
19# ---------------------------------------------------------------------------- #
22"""
23!!! note "Summary"
24 The `classes` module is designed for functions to be executed _on_ classes; not _within_ classes.
25 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.
26"""
29# ---------------------------------------------------------------------------- #
30# #
31# Setup ####
32# #
33# ---------------------------------------------------------------------------- #
36# ---------------------------------------------------------------------------- #
37# Imports ####
38# ---------------------------------------------------------------------------- #
41# ## Python StdLib Imports ----
42from typing import Any
45# ---------------------------------------------------------------------------- #
46# Exports ####
47# ---------------------------------------------------------------------------- #
50__all__: list[str] = ["get_full_class_name"]
53# ---------------------------------------------------------------------------- #
54# #
55# Functions ####
56# #
57# ---------------------------------------------------------------------------- #
60# ---------------------------------------------------------------------------- #
61# Name of classes ####
62# ---------------------------------------------------------------------------- #
65def get_full_class_name(obj: Any) -> str:
66 """
67 !!! note "Summary"
68 This function is designed to extract the full name of a class, including the name of the module from which it was loaded.
70 ???+ abstract "Details"
71 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.
73 Params:
74 obj (Any):
75 The object for which you want to retrieve the full name.
77 Returns:
78 (str):
79 The full name of the class of the object.
81 ???+ example "Examples"
83 ```{.py .python linenums="1" title="Set up"}
84 >>> from toolbox_python.classes import get_full_class_name
85 ```
87 ```{.py .python linenums="1" title="Example 1: Check the name of a standard class"}
88 >>> print(get_full_class_name(str))
89 ```
90 <div class="result" markdown>
91 ```{.sh .shell title="Terminal"}
92 str
93 ```
94 !!! success "Conclusion: Successful class name extraction."
95 </div>
97 ```{.py .python linenums="1" title="Example 2: Check the name of an imported class"}
98 >>> from random import Random
99 >>> print(get_full_class_name(Random))
100 ```
101 <div class="result" markdown>
102 ```{.sh .shell title="Terminal"}
103 random.Random
104 ```
105 !!! success "Conclusion: Successful class name extraction."
106 </div>
108 ??? success "Credit"
109 Full credit goes to:<br>
110 https://stackoverflow.com/questions/18176602/how-to-get-the-name-of-an-exception-that-was-caught-in-python#answer-58045927
111 """
112 module: str = obj.__class__.__module__
113 if module is None or module == str.__class__.__module__:
114 return obj.__class__.__name__
115 return module + "." + obj.__class__.__name__