获取从CPROFILE统计的类名称

发布于 2025-02-03 02:10:19 字数 1262 浏览 2 评论 0原文

我正在尝试检索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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文