获取从CPROFILE统计的类名称
我正在尝试检索Python中代码对象的类名。这些代码对象是Cprofile统计数据的输出。我想在下面尝试做的事情的一个示例:
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
看起来如下:
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}
我想知道哪个类包含任何Profiller条目中的可可。这些stats
中的每一个都是Python __代码__
对象。是否有一种简单/有效的方法可以通过__代码__
对象获取函数的类名称?
# 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论