I am trying to retrieve the class name of an code object in python. These code objects are the outputs of the statistics of a cProfile. An example of what I am trying to do below:
from cProfile import Profile
from typing import Any
class foo:
def __init__(self) -> None:
pass
def __call__(self, *args: Any, **kwds: Any) -> Any:
return args
p = Profile()
bar = foo()
with p:
bar()
stats = p.getstats()
stats
looks like the following:
3 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 cProfile.py:117(__exit__)
1 0.000 0.000 0.000 0.000 tmp.py:8(__call__)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
I would like to know which class contained the callable in any profiler entry. Each of these stats
are python __code__
objects. Is there a simple/efficient way to get the class name of a function via the __code__
object?
# Get foo's callable profile statistic
foo_callable = p.getstats()[2]
print(foo_callable)
_lsprof.profiler_entry(code=<code object __call__ at 0x7f9573925450, file "tmp.py", line 8>, callcount=1, reccallcount=0, totaltime=2.6400000000000003e-07, inlinetime=2.6400000000000003e-07, calls=None)