home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / cProfile.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  4.7 KB  |  167 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. __all__ = [
  5.     'run',
  6.     'runctx',
  7.     'help',
  8.     'Profile']
  9. import _lsprof
  10.  
  11. def run(statement, filename = None, sort = -1):
  12.     prof = Profile()
  13.     result = None
  14.     
  15.     try:
  16.         prof = prof.run(statement)
  17.     except SystemExit:
  18.         pass
  19.     finally:
  20.         pass
  21.  
  22.     return result
  23.  
  24.  
  25. def runctx(statement, globals, locals, filename = None, sort = -1):
  26.     prof = Profile()
  27.     result = None
  28.     
  29.     try:
  30.         prof = prof.runctx(statement, globals, locals)
  31.     except SystemExit:
  32.         pass
  33.     finally:
  34.         pass
  35.  
  36.     return result
  37.  
  38.  
  39. def help():
  40.     print 'Documentation for the profile/cProfile modules can be found '
  41.     print "in the Python Library Reference, section 'The Python Profiler'."
  42.  
  43.  
  44. class Profile(_lsprof.Profiler):
  45.     
  46.     def print_stats(self, sort = -1):
  47.         import pstats
  48.         pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
  49.  
  50.     
  51.     def dump_stats(self, file):
  52.         import marshal
  53.         f = open(file, 'wb')
  54.         self.create_stats()
  55.         marshal.dump(self.stats, f)
  56.         f.close()
  57.  
  58.     
  59.     def create_stats(self):
  60.         self.disable()
  61.         self.snapshot_stats()
  62.  
  63.     
  64.     def snapshot_stats(self):
  65.         entries = self.getstats()
  66.         self.stats = { }
  67.         callersdicts = { }
  68.         for entry in entries:
  69.             func = label(entry.code)
  70.             nc = entry.callcount
  71.             cc = nc - entry.reccallcount
  72.             tt = entry.inlinetime
  73.             ct = entry.totaltime
  74.             callers = { }
  75.             callersdicts[id(entry.code)] = callers
  76.             self.stats[func] = (cc, nc, tt, ct, callers)
  77.         
  78.         for entry in entries:
  79.             if entry.calls:
  80.                 func = label(entry.code)
  81.                 for subentry in entry.calls:
  82.                     
  83.                     try:
  84.                         callers = callersdicts[id(subentry.code)]
  85.                     except KeyError:
  86.                         continue
  87.  
  88.                     nc = subentry.callcount
  89.                     cc = nc - subentry.reccallcount
  90.                     tt = subentry.inlinetime
  91.                     ct = subentry.totaltime
  92.                     if func in callers:
  93.                         prev = callers[func]
  94.                         nc += prev[0]
  95.                         cc += prev[1]
  96.                         tt += prev[2]
  97.                         ct += prev[3]
  98.                     callers[func] = (nc, cc, tt, ct)
  99.                 
  100.  
  101.     
  102.     def run(self, cmd):
  103.         import __main__
  104.         dict = __main__.__dict__
  105.         return self.runctx(cmd, dict, dict)
  106.  
  107.     
  108.     def runctx(self, cmd, globals, locals):
  109.         self.enable()
  110.         
  111.         try:
  112.             exec cmd in globals, locals
  113.         finally:
  114.             self.disable()
  115.  
  116.         return self
  117.  
  118.     
  119.     def runcall(self, func, *args, **kw):
  120.         self.enable()
  121.         
  122.         try:
  123.             return func(*args, **kw)
  124.         finally:
  125.             self.disable()
  126.  
  127.  
  128.  
  129.  
  130. def label(code):
  131.     if isinstance(code, str):
  132.         return ('~', 0, code)
  133.     return (None.co_filename, code.co_firstlineno, code.co_name)
  134.  
  135.  
  136. def main():
  137.     import os
  138.     import sys
  139.     OptionParser = OptionParser
  140.     import optparse
  141.     usage = 'cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ...'
  142.     parser = OptionParser(usage = usage)
  143.     parser.allow_interspersed_args = False
  144.     parser.add_option('-o', '--outfile', dest = 'outfile', help = 'Save stats to <outfile>', default = None)
  145.     parser.add_option('-s', '--sort', dest = 'sort', help = 'Sort order when printing to stdout, based on pstats.Stats class', default = -1)
  146.     if not sys.argv[1:]:
  147.         parser.print_usage()
  148.         sys.exit(2)
  149.     (options, args) = parser.parse_args()
  150.     sys.argv[:] = args
  151.     if len(args) > 0:
  152.         progname = args[0]
  153.         sys.path.insert(0, os.path.dirname(progname))
  154.         with open(progname, 'rb') as fp:
  155.             code = compile(fp.read(), progname, 'exec')
  156.         globs = {
  157.             '__file__': progname,
  158.             '__name__': '__main__',
  159.             '__package__': None }
  160.         runctx(code, globs, None, options.outfile, options.sort)
  161.     else:
  162.         parser.print_usage()
  163.     return parser
  164.  
  165. if __name__ == '__main__':
  166.     main()
  167.