⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pstats.py

📁 mallet是自然语言处理、机器学习领域的一个开源项目。
💻 PY
📖 第 1 页 / 共 2 页
字号:
            width = 0            for func in list:                if  len(func_std_string(func)) > width:                    width = len(func_std_string(func))        return width+2, list    def print_stats(self, *amount):        for filename in self.files:            print filename        if self.files: print        indent = ' ' * 8        for func in self.top_level.keys():            print indent, func_get_function_name(func)        print indent, self.total_calls, "function calls",        if self.total_calls != self.prim_calls:            print "(%d primitive calls)" % self.prim_calls,        print "in %.3f CPU seconds" % self.total_tt        print        width, list = self.get_print_list(amount)        if list:            self.print_title()            for func in list:                self.print_line(func)            print            print        return self    def print_callees(self, *amount):        width, list = self.get_print_list(amount)        if list:            self.calc_callees()            self.print_call_heading(width, "called...")            for func in list:                if self.all_callees.has_key(func):                    self.print_call_line(width, func, self.all_callees[func])                else:                    self.print_call_line(width, func, {})            print            print        return self    def print_callers(self, *amount):        width, list = self.get_print_list(amount)        if list:            self.print_call_heading(width, "was called by...")            for func in list:                cc, nc, tt, ct, callers = self.stats[func]                self.print_call_line(width, func, callers)            print            print        return self    def print_call_heading(self, name_size, column_title):        print "Function ".ljust(name_size) + column_title    def print_call_line(self, name_size, source, call_dict):        print func_std_string(source).ljust(name_size),        if not call_dict:            print "--"            return        clist = call_dict.keys()        clist.sort()        name_size = name_size + 1        indent = ""        for func in clist:            name = func_std_string(func)            print indent*name_size + name + '(' \                      + `call_dict[func]`+')', \                      f8(self.stats[func][3])            indent = " "    def print_title(self):        print '   ncalls  tottime  percall  cumtime  percall', \              'filename:lineno(function)'    def print_line(self, func):  # hack : should print percentages        cc, nc, tt, ct, callers = self.stats[func]        c = str(nc)        if nc != cc:            c = c + '/' + str(cc)        print c.rjust(9),        print f8(tt),        if nc == 0:            print ' '*8,        else:            print f8(tt/nc),        print f8(ct),        if cc == 0:            print ' '*8,        else:            print f8(ct/cc),        print func_std_string(func)    def ignore(self):        # Deprecated since 1.5.1 -- see the docs.        pass # has no return value, so use at end of line :-)class TupleComp:    """This class provides a generic function for comparing any two tuples.    Each instance records a list of tuple-indices (from most significant    to least significant), and sort direction (ascending or decending) for    each tuple-index.  The compare functions can then be used as the function    argument to the system sort() function when a list of tuples need to be    sorted in the instances order."""    def __init__(self, comp_select_list):        self.comp_select_list = comp_select_list    def compare (self, left, right):        for index, direction in self.comp_select_list:            l = left[index]            r = right[index]            if l < r:                return -direction            if l > r:                return direction        return 0#**************************************************************************# func_name is a triple (file:string, line:int, name:string)def func_strip_path(func_name):    file, line, name = func_name    return os.path.basename(file), line, namedef func_get_function_name(func):    return func[2]def func_std_string(func_name): # match what old profile produced    return "%s:%d(%s)" % func_name#**************************************************************************# The following functions combine statists for pairs functions.# The bulk of the processing involves correctly handling "call" lists,# such as callers and callees.#**************************************************************************def add_func_stats(target, source):    """Add together all the stats for two profile entries."""    cc, nc, tt, ct, callers = source    t_cc, t_nc, t_tt, t_ct, t_callers = target    return (cc+t_cc, nc+t_nc, tt+t_tt, ct+t_ct,              add_callers(t_callers, callers))def add_callers(target, source):    """Combine two caller lists in a single list."""    new_callers = {}    for func in target.keys():        new_callers[func] = target[func]    for func in source.keys():        if new_callers.has_key(func):            new_callers[func] = source[func] + new_callers[func]        else:            new_callers[func] = source[func]    return new_callersdef count_calls(callers):    """Sum the caller statistics to get total number of calls received."""    nc = 0    for func in callers.keys():        nc += callers[func]    return nc#**************************************************************************# The following functions support printing of reports#**************************************************************************def f8(x):    return "%8.3f" % x#**************************************************************************# Statistics browser added by ESR, April 2001#**************************************************************************if __name__ == '__main__':    import cmd    try:        import readline    except ImportError:        pass    class ProfileBrowser(cmd.Cmd):        def __init__(self, profile=None):            cmd.Cmd.__init__(self)            self.prompt = "% "            if profile:                self.stats = Stats(profile)            else:                self.stats = None        def generic(self, fn, line):            args = line.split()            processed = []            for term in args:                try:                    processed.append(int(term))                    continue                except ValueError:                    pass                try:                    frac = float(term)                    if frac > 1 or frac < 0:                        print "Fraction argument mus be in [0, 1]"                        continue                    processed.append(frac)                    continue                except ValueError:                    pass                processed.append(term)            if self.stats:                apply(getattr(self.stats, fn), processed)            else:                print "No statistics object is loaded."            return 0        def generic_help(self):            print "Arguments may be:"            print "* An integer maximum number of entries to print."            print "* A decimal fractional number between 0 and 1, controlling"            print "  what fraction of selected entries to print."            print "* A regular expression; only entries with function names"            print "  that match it are printed."        def do_add(self, line):            self.stats.add(line)            return 0        def help_add(self):            print "Add profile info from given file to current statistics object."        def do_callees(self, line):            return self.generic('print_callees', line)        def help_callees(self):            print "Print callees statistics from the current stat object."            self.generic_help()        def do_callers(self, line):            return self.generic('print_callers', line)        def help_callers(self):            print "Print callers statistics from the current stat object."            self.generic_help()        def do_EOF(self, line):            print ""            return 1        def help_EOF(self):            print "Leave the profile brower."        def do_quit(self, line):            return 1        def help_quit(self):            print "Leave the profile brower."        def do_read(self, line):            if line:                try:                    self.stats = Stats(line)                except IOError, args:                    print args[1]                    return                self.prompt = line + "% "            elif len(self.prompt) > 2:                line = self.prompt[-2:]            else:                print "No statistics object is current -- cannot reload."            return 0        def help_read(self):            print "Read in profile data from a specified file."        def do_reverse(self, line):            self.stats.reverse_order()            return 0        def help_reverse(self):            print "Reverse the sort order of the profiling report."        def do_sort(self, line):            abbrevs = self.stats.get_sort_arg_defs().keys()            if line and not filter(lambda x,a=abbrevs: x not in a,line.split()):                apply(self.stats.sort_stats, line.split())            else:                print "Valid sort keys (unique prefixes are accepted):"                for (key, value) in Stats.sort_arg_dict_default.items():                    print "%s -- %s" % (key, value[1])            return 0        def help_sort(self):            print "Sort profile data according to specified keys."            print "(Typing `sort' without arguments lists valid keys.)"        def complete_sort(self, text, *args):            return [a for a in Stats.sort_arg_dict_default.keys() if a.startswith(text)]        def do_stats(self, line):            return self.generic('print_stats', line)        def help_stats(self):            print "Print statistics from the current stat object."            self.generic_help()        def do_strip(self, line):            self.stats.strip_dirs()            return 0        def help_strip(self):            print "Strip leading path information from filenames in the report."        def postcmd(self, stop, line):            if stop:                return stop            return None    import sys    print "Welcome to the profile statistics browser."    if len(sys.argv) > 1:        initprofile = sys.argv[1]    else:        initprofile = None    try:        ProfileBrowser(initprofile).cmdloop()        print "Goodbye."    except KeyboardInterrupt:        pass# That's all, folks.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -