문제

cprofile 프로파일을로드하려고 시도하고 일부 정렬 및 finessing을 수행 한 다음 결과를 파일로 출력합니다. 문서 , 나는 단순히 통과 할 수 있다고 생각했습니다.파일 객체와 print_stats 함수는 해당 스트림으로 리디렉션됩니다.

여기에 사용하려는 코드가 있습니다.

import sys,pstats
s = open('output.txt', 'w')
p = pstats.Stats('profile.dat', s)
.

여기에 결과 오류가 있습니다.

TypeError: Cannot create or construct a <class pstats.Stats at 0xbaa870> object from '<open file 'output.txt', mode 'w' at 0xb2ef60>''
.

스트림 매개 변수에 객체를 전달하지 않으면 출력이 터미널에 정상적으로 나타납니다.

도움이 되었습니까?

해결책

소스 코드를보고 파일을 stream 키워드 인수로 전달해야합니다 (왜 그렇게 될지 알려지지 않은 이유는 분명하지 않습니다 ...).

p = pstats.Stats('profile.dat', stream = s)
.

인라인 주석 아래 및 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)
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top