10 examples of 'python get current class name' in Python

Every line of 'python get current class name' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
58def getClassName(obj):
59 name = getattr(obj.__class__, 'classname', None)
60 if name is not None:
61 return name
62 return obj.__class__.__name__
25def _caller_module_name():
26 # Move up the stack twice: we want the module name of the
27 # function/module that called the function calling
28 # _caller_module_name
29 caller_module = inspect.getmodule(inspect.currentframe().f_back.f_back)
30 if caller_module is None:
31 return ""
32 return caller_module.__name__
50def get_long_class_name(obj):
51 return '.'.join((get_module_name(obj), get_class_name(obj)))
25def current_function(frame):
26 """
27 Get reference to currently running function from inspect/trace stack frame.
28
29 Parameters
30 ----------
31 frame : stack frame
32 Stack frame obtained via trace or inspect
33
34 Returns
35 -------
36 fnc : function reference
37 Currently running function
38 """
39
40 if frame is None:
41 return None
42
43 code = frame.f_code
44 # Attempting to extract the function reference for these calls appears
45 # to be problematic
46 if code.co_name == '__del__' or code.co_name == '_remove' or \
47 code.co_name == '_removeHandlerRef':
48 return None
49
50 try:
51 # Solution follows suggestion at http://stackoverflow.com/a/37099372
52 lst = [referer for referer in gc.get_referrers(code)
53 if getattr(referer, "__code__", None) is code and
54 inspect.getclosurevars(referer).nonlocals.items() <=
55 frame.f_locals.items()]
56 if lst:
57 return lst[0]
58 else:
59 return None
60 except ValueError:
61 # inspect.getclosurevars can fail with ValueError: Cell is empty
62 return None
114def classname(obj):
115 return obj.__class__.__name__
84def get_script_name():
85 """Return the name of the top-level script."""
86 return os.path.splitext(scriptinfo()["name"])[0]
115def func_name(
116 level: Optional[int] = 1,
117 parent: Optional[Callable] = None) -> str:
118 """ Return the name of the function that is calling this function. """
119 frame = inspect.currentframe()
120 # Go back a number of frames (usually 1).
121 backlevel = level or 1
122 while backlevel > 0:
123 frame = frame.f_back
124 backlevel -= 1
125 if parent:
126 func = '{}.{}'.format(parent.__class__.__name__, frame.f_code.co_name)
127 return func
128
129 return frame.f_code.co_name
26def _get_script_path():
27 """Get directory path of current script"""
28 script_filename = inspect.getframeinfo(inspect.currentframe()).filename
29 script_path = os.path.dirname(os.path.abspath(script_filename))
30
31 return script_path
67def _classname(cls):
68 return getattr(cls, "classname", cls.__name__)
154def get_current_method_and_class(current_line_index, current_buffer):
155 class_regex, class_name = re.compile(r"^class (?P.+)\("), False
156 method_regex, method_name = re.compile(r"def (?P.+)\("), False
157 for line in range(current_line_index - 1, -1, -1):
158 if class_regex.search(current_buffer[line]) is not None and not class_name:
159 class_name = class_regex.search(current_buffer[line])
160 class_name = class_name.group(1)
161 if method_regex.search(current_buffer[line]) is not None and not method_name and not class_name:
162 method_name = method_regex.search(current_buffer[line])
163 method_name = method_name.group(1)
164 return (class_name, method_name)

Related snippets