[Python-checkins] CVS: python/dist/src/Lib pstats.py,1.21,1.22
Tim Peters
tim_one@users.sourceforge.net
Sun, 07 Oct 2001 23:13:21 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv7871/python/Lib
Modified Files:
pstats.py
Log Message:
Widespread random code cleanup.
Most of this code was old enough to vote. Examples of cleanups:
+ Backslashes were used for line continuation even inside unclosed
bracket structures, from back in the days that was still needed.
+ There was no use of % formats, and e.g. the old fpformat module was
still used to format floats "by hand" in conjunction with rjust().
+ There was even use of a do-nothing .ignore() method to tack on to the
end of a chain of method calls, else way back when Python would print
the non-None result (as it does now in an interactive session -- it
*used* to do that in batch mode too).
+ Perhaps controversial (although I can't imagine why for real <wink>),
used augmented assignment where helpful. Stuff like
self.total_calls = self.total_calls + other.total_calls
is just plain harder to follow than
self.total_calls += other.total_calls
Index: pstats.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pstats.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** pstats.py 2001/08/13 14:47:49 1.21
--- pstats.py 2001/10/08 06:13:19 1.22
***************
*** 38,43 ****
import re
- import fpformat
-
__all__ = ["Stats"]
--- 38,41 ----
***************
*** 79,83 ****
args = args[1:]
self.init(arg)
! apply(self.add, args).ignore()
def init(self, arg):
--- 77,81 ----
args = args[1:]
self.init(arg)
! apply(self.add, args)
def init(self, arg):
***************
*** 103,107 ****
print
-
def load_stats(self, arg):
if not arg: self.stats = {}
--- 101,104 ----
***************
*** 127,135 ****
def get_top_level_stats(self):
! for func in self.stats.keys():
! cc, nc, tt, ct, callers = self.stats[func]
! self.total_calls = self.total_calls + nc
! self.prim_calls = self.prim_calls + cc
! self.total_tt = self.total_tt + tt
if callers.has_key(("jprofile", 0, "profiler")):
self.top_level[func] = None
--- 124,131 ----
def get_top_level_stats(self):
! for func, (cc, nc, tt, ct, callers) in self.stats.items():
! self.total_calls += nc
! self.prim_calls += cc
! self.total_tt += tt
if callers.has_key(("jprofile", 0, "profiler")):
self.top_level[func] = None
***************
*** 141,151 ****
if len(arg_list) > 1: apply(self.add, arg_list[1:])
other = arg_list[0]
! if type(self) != type(other) or \
! self.__class__ != other.__class__:
other = Stats(other)
! self.files = self.files + other.files
! self.total_calls = self.total_calls + other.total_calls
! self.prim_calls = self.prim_calls + other.prim_calls
! self.total_tt = self.total_tt + other.total_tt
for func in other.top_level.keys():
self.top_level[func] = None
--- 137,146 ----
if len(arg_list) > 1: apply(self.add, arg_list[1:])
other = arg_list[0]
! if type(self) != type(other) or self.__class__ != other.__class__:
other = Stats(other)
! self.files += other.files
! self.total_calls += other.total_calls
! self.prim_calls += other.prim_calls
! self.total_tt += other.total_tt
for func in other.top_level.keys():
self.top_level[func] = None
***************
*** 161,183 ****
else:
old_func_stat = (0, 0, 0, 0, {},)
! self.stats[func] = add_func_stats(old_func_stat, \
! other.stats[func])
return self
-
-
# list the tuple indices and directions for sorting,
# along with some printable description
! sort_arg_dict_default = {\
! "calls" : (((1,-1), ), "call count"),\
! "cumulative": (((3,-1), ), "cumulative time"),\
! "file" : (((4, 1), ), "file name"),\
! "line" : (((5, 1), ), "line number"),\
! "module" : (((4, 1), ), "file name"),\
! "name" : (((6, 1), ), "function name"),\
! "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"), \
! "pcalls" : (((0,-1), ), "call count"),\
! "stdname" : (((7, 1), ), "standard name"),\
! "time" : (((2,-1), ), "internal time"),\
}
--- 156,175 ----
else:
old_func_stat = (0, 0, 0, 0, {},)
! self.stats[func] = add_func_stats(old_func_stat, other.stats[func])
return self
# list the tuple indices and directions for sorting,
# along with some printable description
! sort_arg_dict_default = {
! "calls" : (((1,-1), ), "call count"),
! "cumulative": (((3,-1), ), "cumulative time"),
! "file" : (((4, 1), ), "file name"),
! "line" : (((5, 1), ), "line number"),
! "module" : (((4, 1), ), "file name"),
! "name" : (((6, 1), ), "function name"),
! "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
! "pcalls" : (((0,-1), ), "call count"),
! "stdname" : (((7, 1), ), "standard name"),
! "time" : (((2,-1), ), "internal time"),
}
***************
*** 195,200 ****
bad_list[fragment] = 0
break
! dict[fragment] = self. \
! sort_arg_dict_default[word]
fragment = fragment[:-1]
for word in bad_list.keys():
--- 187,191 ----
bad_list[fragment] = 0
break
! dict[fragment] = self.sort_arg_dict_default[word]
fragment = fragment[:-1]
for word in bad_list.keys():
***************
*** 202,206 ****
return self.sort_arg_dict
-
def sort_stats(self, *field):
if not field:
--- 193,196 ----
***************
*** 209,215 ****
if len(field) == 1 and type(field[0]) == type(1):
# Be compatible with old profiler
! field = [ {-1: "stdname", \
! 0:"calls", \
! 1:"time", \
2: "cumulative" } [ field[0] ] ]
--- 199,205 ----
if len(field) == 1 and type(field[0]) == type(1):
# Be compatible with old profiler
! field = [ {-1: "stdname",
! 0:"calls",
! 1:"time",
2: "cumulative" } [ field[0] ] ]
***************
*** 220,225 ****
for word in field:
sort_tuple = sort_tuple + sort_arg_defs[word][0]
! self.sort_type = self.sort_type + connector + \
! sort_arg_defs[word][1]
connector = ", "
--- 210,214 ----
for word in field:
sort_tuple = sort_tuple + sort_arg_defs[word][0]
! self.sort_type += connector + sort_arg_defs[word][1]
connector = ", "
***************
*** 227,232 ****
for func in self.stats.keys():
cc, nc, tt, ct, callers = self.stats[func]
! stats_list.append((cc, nc, tt, ct) + func_split(func) \
! + (func_std_string(func), func,) )
stats_list.sort(TupleComp(sort_tuple).compare)
--- 216,221 ----
for func in self.stats.keys():
cc, nc, tt, ct, callers = self.stats[func]
! stats_list.append((cc, nc, tt, ct) + func +
! (func_std_string(func), func))
stats_list.sort(TupleComp(sort_tuple).compare)
***************
*** 237,243 ****
return self
-
def reverse_order(self):
! if self.fcn_list: self.fcn_list.reverse()
return self
--- 226,232 ----
return self
def reverse_order(self):
! if self.fcn_list:
! self.fcn_list.reverse()
return self
***************
*** 253,263 ****
newcallers = {}
for func2 in callers.keys():
! newcallers[func_strip_path(func2)] = \
! callers[func2]
if newstats.has_key(newfunc):
! newstats[newfunc] = add_func_stats( \
! newstats[newfunc],\
! (cc, nc, tt, ct, newcallers))
else:
newstats[newfunc] = (cc, nc, tt, ct, newcallers)
--- 242,251 ----
newcallers = {}
for func2 in callers.keys():
! newcallers[func_strip_path(func2)] = callers[func2]
if newstats.has_key(newfunc):
! newstats[newfunc] = add_func_stats(
! newstats[newfunc],
! (cc, nc, tt, ct, newcallers))
else:
newstats[newfunc] = (cc, nc, tt, ct, newcallers)
***************
*** 273,278 ****
return self
-
-
def calc_callees(self):
if self.all_callees: return
--- 261,264 ----
***************
*** 304,308 ****
count = len(list)
if type(sel) == type(1.0) and 0.0 <= sel < 1.0:
! count = int (count * sel + .5)
new_list = list[:count]
elif type(sel) == type(1) and 0 <= sel < count:
--- 290,294 ----
count = len(list)
if type(sel) == type(1.0) and 0.0 <= sel < 1.0:
! count = int(count * sel + .5)
new_list = list[:count]
elif type(sel) == type(1) and 0 <= sel < count:
***************
*** 316,321 ****
return new_list, msg
-
-
def get_print_list(self, sel_list):
width = self.max_name_len
--- 302,305 ----
***************
*** 328,332 ****
for selection in sel_list:
! list,msg = self.eval_print_amount(selection, list, msg)
count = len(list)
--- 312,316 ----
for selection in sel_list:
! list, msg = self.eval_print_amount(selection, list, msg)
count = len(list)
***************
*** 346,357 ****
print filename
if self.files: print
! indent = " "
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 "(" + `self.prim_calls`, "primitive calls)",
! print "in", fpformat.fix(self.total_tt, 3), "CPU seconds"
print
width, list = self.get_print_list(amount)
--- 330,341 ----
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)
***************
*** 364,368 ****
return self
-
def print_callees(self, *amount):
width, list = self.get_print_list(amount)
--- 348,351 ----
***************
*** 373,378 ****
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, {})
--- 356,360 ----
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, {})
***************
*** 395,399 ****
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),
--- 377,380 ----
***************
*** 412,431 ****
indent = " "
-
-
def print_title(self):
! print 'ncalls'.rjust(9),
! print 'tottime'.rjust(8),
! print 'percall'.rjust(8),
! print 'cumtime'.rjust(8),
! print 'percall'.rjust(8),
! print 'filename:lineno(function)'
!
def print_line(self, func): # hack : should print percentages
cc, nc, tt, ct, callers = self.stats[func]
! c = `nc`
if nc != cc:
! c = c + '/' + `cc`
print c.rjust(9),
print f8(tt),
--- 393,405 ----
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),
***************
*** 441,449 ****
print func_std_string(func)
-
- def ignore(self):
- pass # has no return value, so use at end of line :-)
-
-
class TupleComp:
"""This class provides a generic function for comparing any two tuples.
--- 415,418 ----
***************
*** 466,473 ****
return direction
return 0
-
#**************************************************************************
def func_strip_path(func_name):
--- 435,445 ----
return direction
return 0
+ def ignore(self):
+ # Deprecated since 1.5.1 -- see the docs.
+ pass # has no return value, so use at end of line :-)
#**************************************************************************
+ # func_name is a triple (file:string, line:int, name:string)
def func_strip_path(func_name):
***************
*** 479,487 ****
def func_std_string(func_name): # match what old profile produced
! file, line, name = func_name
! return file + ":" + `line` + "(" + name + ")"
!
! def func_split(func_name):
! return func_name
#**************************************************************************
--- 451,455 ----
def func_std_string(func_name): # match what old profile produced
! return "%s:%d(%s)" % func_name
#**************************************************************************
***************
*** 495,502 ****
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."""
--- 463,469 ----
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."""
***************
*** 515,519 ****
nc = 0
for func in callers.keys():
! nc = nc + callers[func]
return nc
--- 482,486 ----
nc = 0
for func in callers.keys():
! nc += callers[func]
return nc
***************
*** 523,527 ****
def f8(x):
! return fpformat.fix(x, 3).rjust(8)
#**************************************************************************
--- 490,494 ----
def f8(x):
! return "%8.3f" % x
#**************************************************************************