Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
Stats Class Reference

Public Member Functions

def __init__
 
def init
 
def load_stats
 
def get_top_level_stats
 
def add
 
def get_sort_arg_defs
 
def sort_stats
 
def reverse_order
 
def strip_dirs
 
def calc_callees
 
def eval_print_amount
 
def get_print_list
 
def print_stats
 
def print_callees
 
def print_callers
 
def print_call_heading
 
def print_call_line
 
def print_title
 
def print_line
 
def ignore
 

Data Fields

 all_callees
 
 files
 
 fcn_list
 
 total_tt
 
 total_calls
 
 prim_calls
 
 max_name_len
 
 top_level
 
 stats
 
 sort_arg_dict
 
 sort_type
 

Static Public Attributes

dictionary sort_arg_dict_default
 

Detailed Description

This class is used for creating reports from data generated by the
Profile class.  It is a "friend" of that class, and imports data either
by direct access to members of Profile class, or by reading in a dictionary
that was emitted (via marshal) from the Profile class.

The big change from the previous Profiler (in terms of raw functionality)
is that an "add()" method has been provided to combine Stats from
several distinct profile runs.  Both the constructor and the add()
method now take arbitrarily many file names as arguments.

All the print methods now take an argument that indicates how many lines
to print.  If the arg is a floating point number between 0 and 1.0, then
it is taken as a decimal percentage of the available lines to be printed
(e.g., .1 means print 10% of all available lines).  If it is an integer,
it is taken to mean the number of lines of data that you wish to have
printed.

The sort_stats() method now processes some additional options (i.e., in
addition to the old -1, 0, 1, or 2).  It takes an arbitrary number of quoted
strings to select the sort order.  For example sort_stats('time', 'name')
sorts on the major key of "internal function time", and on the minor
key of 'the name of the function'.  Look at the two tables in sort_stats()
and get_sort_arg_defs(self) for more examples.

All methods now return "self",  so you can string together commands like:
    Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
                        print_stats(5).print_callers(5)

Definition at line 42 of file pstats.py.

Constructor & Destructor Documentation

def __init__ (   self,
  args 
)

Definition at line 72 of file pstats.py.

References Stats.add(), MyAI.init(), TrafficAI.init(), waitjump.init(), ai_escortpatrol.init(), and Stats.init().

72 
73  def __init__(self, *args):
74  if not len(args):
75  arg = None
76  else:
77  arg = args[0]
78  args = args[1:]
79  self.init(arg)
80  apply(self.add, args)

Member Function Documentation

def add (   self,
  arg_list 
)

Definition at line 135 of file pstats.py.

References SymbolTable.__class__, Stats.add(), pstats.add_func_stats(), Stats.fcn_list, Stats.files, Stats.max_name_len, Stats.prim_calls, Stats.stats, Profile.stats, Stats.top_level, Stats.total_calls, and Stats.total_tt.

136  def add(self, *arg_list):
137  if not arg_list: return self
138  if len(arg_list) > 1: apply(self.add, arg_list[1:])
139  other = arg_list[0]
140  if type(self) != type(other) or self.__class__ != other.__class__:
141  other = Stats(other)
142  self.files += other.files
143  self.total_calls += other.total_calls
144  self.prim_calls += other.prim_calls
145  self.total_tt += other.total_tt
146  for func in other.top_level.keys():
147  self.top_level[func] = None
148 
149  if self.max_name_len < other.max_name_len:
150  self.max_name_len = other.max_name_len
151 
152  self.fcn_list = None
153 
154  for func in other.stats.keys():
155  if self.stats.has_key(func):
156  old_func_stat = self.stats[func]
157  else:
158  old_func_stat = (0, 0, 0, 0, {},)
159  self.stats[func] = add_func_stats(old_func_stat, other.stats[func])
160  return self
def calc_callees (   self)

Definition at line 263 of file pstats.py.

References Stats.all_callees, Stats.stats, and Profile.stats.

264  def calc_callees(self):
265  if self.all_callees: return
266  self.all_callees = all_callees = {}
267  for func in self.stats.keys():
268  if not all_callees.has_key(func):
269  all_callees[func] = {}
270  cc, nc, tt, ct, callers = self.stats[func]
271  for func2 in callers.keys():
272  if not all_callees.has_key(func2):
273  all_callees[func2] = {}
274  all_callees[func2][func] = callers[func2]
275  return
def eval_print_amount (   self,
  sel,
  list,
  msg 
)

Definition at line 282 of file pstats.py.

References pstats.func_std_string().

283  def eval_print_amount(self, sel, list, msg):
284  new_list = list
285  if type(sel) == type(""):
286  new_list = []
287  for func in list:
288  if re.search(sel, func_std_string(func)):
289  new_list.append(func)
290  else:
291  count = len(list)
292  if type(sel) == type(1.0) and 0.0 <= sel < 1.0:
293  count = int(count * sel + .5)
294  new_list = list[:count]
295  elif type(sel) == type(1) and 0 <= sel < count:
296  count = sel
297  new_list = list[:count]
298  if len(list) != len(new_list):
299  msg = msg + " List reduced from " + `len(list)` \
300  + " to " + `len(new_list)` + \
301  " due to restriction <" + `sel` + ">\n"
302 
303  return new_list, msg
def get_print_list (   self,
  sel_list 
)

Definition at line 304 of file pstats.py.

References Stats.eval_print_amount(), Stats.fcn_list, pstats.func_std_string(), Stats.max_name_len, Stats.sort_type, Stats.stats, and Profile.stats.

305  def get_print_list(self, sel_list):
306  width = self.max_name_len
307  if self.fcn_list:
308  list = self.fcn_list[:]
309  msg = " Ordered by: " + self.sort_type + '\n'
310  else:
311  list = self.stats.keys()
312  msg = " Random listing order was used\n"
313 
314  for selection in sel_list:
315  list, msg = self.eval_print_amount(selection, list, msg)
316 
317  count = len(list)
318 
319  if not list:
320  return 0, list
321  print msg
322  if count < len(self.stats):
323  width = 0
324  for func in list:
325  if len(func_std_string(func)) > width:
326  width = len(func_std_string(func))
327  return width+2, list
def get_sort_arg_defs (   self)
Expand all abbreviations that are unique.

Definition at line 176 of file pstats.py.

References Stats.sort_arg_dict, and Stats.sort_arg_dict_default.

177  def get_sort_arg_defs(self):
178  """Expand all abbreviations that are unique."""
179  if not self.sort_arg_dict:
180  self.sort_arg_dict = dict = {}
181  bad_list = {}
182  for word in self.sort_arg_dict_default.keys():
183  fragment = word
184  while fragment:
185  if not fragment:
186  break
187  if dict.has_key(fragment):
188  bad_list[fragment] = 0
189  break
190  dict[fragment] = self.sort_arg_dict_default[word]
191  fragment = fragment[:-1]
192  for word in bad_list.keys():
193  del dict[word]
194  return self.sort_arg_dict
def get_top_level_stats (   self)

Definition at line 125 of file pstats.py.

References pstats.func_std_string(), Stats.max_name_len, Stats.prim_calls, Stats.top_level, Stats.total_calls, and Stats.total_tt.

126  def get_top_level_stats(self):
127  for func, (cc, nc, tt, ct, callers) in self.stats.items():
128  self.total_calls += nc
129  self.prim_calls += cc
130  self.total_tt += tt
131  if callers.has_key(("jprofile", 0, "profiler")):
132  self.top_level[func] = None
133  if len(func_std_string(func)) > self.max_name_len:
134  self.max_name_len = len(func_std_string(func))
def ignore (   self)

Definition at line 417 of file pstats.py.

418  def ignore(self):
419  # Deprecated since 1.5.1 -- see the docs.
420  pass # has no return value, so use at end of line :-)
def init (   self,
  arg 
)

Definition at line 81 of file pstats.py.

81 
82  def init(self, arg):
83  self.all_callees = None # calc only if needed
84  self.files = []
85  self.fcn_list = None
86  self.total_tt = 0
87  self.total_calls = 0
88  self.prim_calls = 0
89  self.max_name_len = 0
90  self.top_level = {}
91  self.stats = {}
92  self.sort_arg_dict = {}
93  self.load_stats(arg)
94  trouble = 1
95  try:
96  self.get_top_level_stats()
97  trouble = 0
98  finally:
99  if trouble:
100  print "Invalid timing data",
101  if self.files: print self.files[-1],
102  print
def load_stats (   self,
  arg 
)

Definition at line 103 of file pstats.py.

References SymbolTable.__class__, Stats.files, aifc.open(), Stats.stats, and Profile.stats.

104  def load_stats(self, arg):
105  if not arg: self.stats = {}
106  elif type(arg) == type(""):
107  f = open(arg, 'rb')
108  self.stats = marshal.load(f)
109  f.close()
110  try:
111  file_stats = os.stat(arg)
112  arg = time.ctime(file_stats[8]) + " " + arg
113  except: # in case this is not unix
114  pass
115  self.files = [ arg ]
116  elif hasattr(arg, 'create_stats'):
117  arg.create_stats()
118  self.stats = arg.stats
119  arg.stats = {}
120  if not self.stats:
121  raise TypeError, "Cannot create or construct a " \
122  + `self.__class__` \
123  + " object from '" + `arg` + "'"
124  return
def print_call_heading (   self,
  name_size,
  column_title 
)

Definition at line 376 of file pstats.py.

References string.ljust().

377  def print_call_heading(self, name_size, column_title):
378  print "Function ".ljust(name_size) + column_title
def print_call_line (   self,
  name_size,
  source,
  call_dict 
)

Definition at line 379 of file pstats.py.

References pstats.f8(), pstats.func_std_string(), string.ljust(), Stats.stats, and Profile.stats.

380  def print_call_line(self, name_size, source, call_dict):
381  print func_std_string(source).ljust(name_size),
382  if not call_dict:
383  print "--"
384  return
385  clist = call_dict.keys()
386  clist.sort()
387  name_size = name_size + 1
388  indent = ""
389  for func in clist:
390  name = func_std_string(func)
391  print indent*name_size + name + '(' \
392  + `call_dict[func]`+')', \
393  f8(self.stats[func][3])
394  indent = " "
def print_callees (   self,
  amount 
)

Definition at line 350 of file pstats.py.

References Stats.all_callees, Stats.calc_callees(), Stats.get_print_list(), Stats.print_call_heading(), and Stats.print_call_line().

351  def print_callees(self, *amount):
352  width, list = self.get_print_list(amount)
353  if list:
354  self.calc_callees()
355 
356  self.print_call_heading(width, "called...")
357  for func in list:
358  if self.all_callees.has_key(func):
359  self.print_call_line(width, func, self.all_callees[func])
360  else:
361  self.print_call_line(width, func, {})
362  print
363  print
364  return self
def print_callers (   self,
  amount 
)

Definition at line 365 of file pstats.py.

References Stats.get_print_list(), Stats.print_call_heading(), Stats.print_call_line(), Stats.stats, and Profile.stats.

366  def print_callers(self, *amount):
367  width, list = self.get_print_list(amount)
368  if list:
369  self.print_call_heading(width, "was called by...")
370  for func in list:
371  cc, nc, tt, ct, callers = self.stats[func]
372  self.print_call_line(width, func, callers)
373  print
374  print
375  return self
def print_line (   self,
  func 
)

Definition at line 399 of file pstats.py.

References pstats.f8(), pstats.func_std_string(), Stats.stats, Profile.stats, and locale.str().

400  def print_line(self, func): # hack : should print percentages
401  cc, nc, tt, ct, callers = self.stats[func]
402  c = str(nc)
403  if nc != cc:
404  c = c + '/' + str(cc)
405  print c.rjust(9),
406  print f8(tt),
407  if nc == 0:
408  print ' '*8,
409  else:
410  print f8(tt/nc),
411  print f8(ct),
412  if cc == 0:
413  print ' '*8,
414  else:
415  print f8(ct/cc),
416  print func_std_string(func)
def print_stats (   self,
  amount 
)

Definition at line 328 of file pstats.py.

References Stats.files, pstats.func_get_function_name(), Stats.get_print_list(), Stats.prim_calls, Stats.print_line(), Stats.print_title(), Stats.total_calls, and Stats.total_tt.

329  def print_stats(self, *amount):
330  for filename in self.files:
331  print filename
332  if self.files: print
333  indent = ' ' * 8
334  for func in self.top_level.keys():
335  print indent, func_get_function_name(func)
336 
337  print indent, self.total_calls, "function calls",
338  if self.total_calls != self.prim_calls:
339  print "(%d primitive calls)" % self.prim_calls,
340  print "in %.3f CPU seconds" % self.total_tt
341  print
342  width, list = self.get_print_list(amount)
343  if list:
344  self.print_title()
345  for func in list:
346  self.print_line(func)
347  print
348  print
349  return self
def print_title (   self)

Definition at line 395 of file pstats.py.

396  def print_title(self):
397  print ' ncalls tottime percall cumtime percall', \
398  'filename:lineno(function)'
def reverse_order (   self)

Definition at line 228 of file pstats.py.

References Stats.fcn_list.

229  def reverse_order(self):
230  if self.fcn_list:
231  self.fcn_list.reverse()
232  return self
def sort_stats (   self,
  field 
)

Definition at line 195 of file pstats.py.

References Stats.fcn_list, and Stats.get_sort_arg_defs().

196  def sort_stats(self, *field):
197  if not field:
198  self.fcn_list = 0
199  return self
200  if len(field) == 1 and type(field[0]) == type(1):
201  # Be compatible with old profiler
202  field = [ {-1: "stdname",
203  0:"calls",
204  1:"time",
205  2: "cumulative" } [ field[0] ] ]
206 
207  sort_arg_defs = self.get_sort_arg_defs()
208  sort_tuple = ()
209  self.sort_type = ""
210  connector = ""
211  for word in field:
212  sort_tuple = sort_tuple + sort_arg_defs[word][0]
213  self.sort_type += connector + sort_arg_defs[word][1]
214  connector = ", "
215 
216  stats_list = []
217  for func in self.stats.keys():
218  cc, nc, tt, ct, callers = self.stats[func]
219  stats_list.append((cc, nc, tt, ct) + func +
220  (func_std_string(func), func))
221 
222  stats_list.sort(TupleComp(sort_tuple).compare)
223 
224  self.fcn_list = fcn_list = []
225  for tuple in stats_list:
226  fcn_list.append(tuple[-1])
227  return self
def strip_dirs (   self)

Definition at line 233 of file pstats.py.

References pstats.add_func_stats(), Stats.all_callees, Stats.fcn_list, pstats.func_std_string(), pstats.func_strip_path(), Stats.max_name_len, Stats.stats, Profile.stats, and Stats.top_level.

234  def strip_dirs(self):
235  oldstats = self.stats
236  self.stats = newstats = {}
237  max_name_len = 0
238  for func in oldstats.keys():
239  cc, nc, tt, ct, callers = oldstats[func]
240  newfunc = func_strip_path(func)
241  if len(func_std_string(newfunc)) > max_name_len:
242  max_name_len = len(func_std_string(newfunc))
243  newcallers = {}
244  for func2 in callers.keys():
245  newcallers[func_strip_path(func2)] = callers[func2]
246 
247  if newstats.has_key(newfunc):
248  newstats[newfunc] = add_func_stats(
249  newstats[newfunc],
250  (cc, nc, tt, ct, newcallers))
251  else:
252  newstats[newfunc] = (cc, nc, tt, ct, newcallers)
253  old_top = self.top_level
254  self.top_level = new_top = {}
255  for func in old_top.keys():
256  new_top[func_strip_path(func)] = None
257 
258  self.max_name_len = max_name_len
259 
260  self.fcn_list = None
261  self.all_callees = None
262  return self

Field Documentation

all_callees

Definition at line 82 of file pstats.py.

fcn_list

Definition at line 84 of file pstats.py.

files

Definition at line 83 of file pstats.py.

max_name_len

Definition at line 88 of file pstats.py.

prim_calls

Definition at line 87 of file pstats.py.

sort_arg_dict

Definition at line 91 of file pstats.py.

dictionary sort_arg_dict_default
static
Initial value:
1 = {
2  "calls" : (((1,-1), ), "call count"),
3  "cumulative": (((3,-1), ), "cumulative time"),
4  "file" : (((4, 1), ), "file name"),
5  "line" : (((5, 1), ), "line number"),
6  "module" : (((4, 1), ), "file name"),
7  "name" : (((6, 1), ), "function name"),
8  "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
9  "pcalls" : (((0,-1), ), "call count"),
10  "stdname" : (((7, 1), ), "standard name"),
11  "time" : (((2,-1), ), "internal time"),
12  }

Definition at line 163 of file pstats.py.

sort_type

Definition at line 208 of file pstats.py.

stats

Definition at line 90 of file pstats.py.

top_level

Definition at line 89 of file pstats.py.

total_calls

Definition at line 86 of file pstats.py.

total_tt

Definition at line 85 of file pstats.py.


The documentation for this class was generated from the following file: