the*_*e_e 5 cprofile python-2.7 pstats
我正在尝试加载 cProfile 配置文件,进行一些排序和处理,然后将结果输出到文件中。根据文档,我认为我可以简单地传递一个文件对象,该print_stats函数将重定向到该流。
这是我尝试使用的代码:
import sys,pstats
s = open('output.txt', 'w')
p = pstats.Stats('profile.dat', s)
Run Code Online (Sandbox Code Playgroud)
这是由此产生的错误:
TypeError: Cannot create or construct a <class pstats.Stats at 0xbaa870> object from '<open file 'output.txt', mode 'w' at 0xb2ef60>''
Run Code Online (Sandbox Code Playgroud)
我还应该补充一点,当我没有将对象传递给流参数时,输出在终端中显示为正常。
查看源代码,您必须将文件作为stream关键字参数传递(我不清楚为什么要这样实现......),例如:
p = pstats.Stats('profile.dat', stream = s)
Run Code Online (Sandbox Code Playgroud)
请参阅下面的内联注释和if "stream" in kwds行。
class Stats:
"""..."""
def __init__(self, *args, **kwds):
# I can't figure out how to explictly specify a stream keyword arg
# with *args:
# def __init__(self, *args, stream=sys.stdout): ...
# so I use **kwds and sqauwk if something unexpected is passed in.
self.stream = sys.stdout
if "stream" in kwds:
self.stream = kwds["stream"]
del kwds["stream"]
if kwds:
keys = kwds.keys()
keys.sort()
extras = ", ".join(["%s=%s" % (k, kwds[k]) for k in keys])
raise ValueError, "unrecognized keyword args: %s" % extras
if not len(args):
arg = None
else:
arg = args[0]
args = args[1:]
self.init(arg)
self.add(*args)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2366 次 |
| 最近记录: |