<s> """<STR_LIT>""" <EOL> import cProfile <EOL> import pstats <EOL> import sys <EOL> from collections import defaultdict <EOL> from vprof import base_profile <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class RuntimeProfilerRunError ( Error , base_profile . ProfilerRuntimeException ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class RuntimeProfile ( base_profile . BaseProfile ) : <EOL> """<STR_LIT>""" <EOL> def _build_callees ( self , stats ) : <EOL> """<STR_LIT>""" <EOL> callees = defaultdict ( list ) <EOL> for func , ( _ , _ , _ , _ , callers ) in sorted ( stats . items ( ) ) : <EOL> for caller in callers : <EOL> callees [ caller ] . append ( func ) <EOL> return callees <EOL> def _build_call_tree ( self , node , callees , stats , seen = set ( ) ) : <EOL> """<STR_LIT>""" <EOL> seen . add ( node ) <EOL> module_name , lineno , func_name = node <EOL> cum_calls , num_calls , time_per_call , cum_time , _ = stats [ node ] <EOL> return { <EOL> '<STR_LIT>' : module_name , <EOL> '<STR_LIT>' : lineno , <EOL> '<STR_LIT>' : func_name , <EOL> '<STR_LIT>' : cum_calls , <EOL> '<STR_LIT>' : num_calls , <EOL> '<STR_LIT>' : time_per_call , <EOL> '<STR_LIT>' : cum_time , <EOL> '<STR_LIT>' : [ self . _build_call_tree ( child , callees , stats , seen ) <EOL> for child in callees [ node ] if child not in seen ] <EOL> } <EOL> def _transform_stats ( self , stats ) : <EOL> """<STR_LIT>""" <EOL> def _statcmp ( stat ) : <EOL> """<STR_LIT>""" <EOL> _ , params = stat <EOL> return params [ <NUM_LIT:3> ] <EOL> stats . calc_callees ( ) <EOL> callees = self . _build_callees ( stats . stats ) <EOL> root , _ = max ( stats . stats . items ( ) , key = _statcmp ) <EOL> return self . _build_call_tree ( root , callees , stats . stats ) <EOL> def run_as_package_path ( self , prof ) : <EOL> """<STR_LIT>""" <EOL> import runpy <EOL> prof . enable ( ) <EOL> try : <EOL> runpy . run_path ( self . _run_object , run_name = '<STR_LIT:__main__>' ) <EOL> except ImportError : <EOL> raise RuntimeProfilerRunError ( <EOL> '<STR_LIT>' % self . _run_object ) <EOL> except SystemExit : <EOL> pass <EOL> prof . disable ( ) <EOL> self . _object_name = self . _run_object <EOL> def run_as_module ( self , prof ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( self . _run_object , '<STR_LIT:rb>' ) as srcfile : <EOL> code = compile ( srcfile . read ( ) , self . _run_object , '<STR_LIT>' ) <EOL> prof . runctx ( code , self . _globs , None ) <EOL> except SystemExit : <EOL> pass <EOL> self . _object_name = self . _run_object <EOL> def run_as_package_in_namespace ( self , prof ) : <EOL> """<STR_LIT>""" <EOL> import runpy <EOL> prof . enable ( ) <EOL> try : <EOL> runpy . run_module ( self . _run_object , run_name = '<STR_LIT:__main__>' ) <EOL> except ImportError : <EOL> raise RuntimeProfilerRunError ( <EOL> '<STR_LIT>' % self . _run_object ) <EOL> except SystemExit : <EOL> pass <EOL> finally : <EOL> prof . disable ( ) <EOL> self . _object_name = self . _run_object <EOL> def run_as_function ( self , prof ) : <EOL> """<STR_LIT>""" <EOL> prof . enable ( ) <EOL> self . _run_object ( * self . _run_args , ** self . _run_kwargs ) <EOL> prof . disable ( ) <EOL> self . _object_name = '<STR_LIT>' % self . _run_object . __name__ <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _run_args : <EOL> sys . argv [ : ] = [ self . _run_object , self . _run_args ] <EOL> else : <EOL> sys . argv [ : ] = [ self . _run_object ] <EOL> prof = cProfile . Profile ( ) <EOL> run_dispatcher = self . get_run_dispatcher ( ) <EOL> run_dispatcher ( prof ) <EOL> prof . create_stats ( ) <EOL> cprofile_stats = pstats . Stats ( prof ) <EOL> return { <EOL> '<STR_LIT>' : self . _object_name , <EOL> '<STR_LIT>' : cprofile_stats . total_tt , <EOL> '<STR_LIT>' : cprofile_stats . prim_calls , <EOL> '<STR_LIT>' : cprofile_stats . total_calls , <EOL> '<STR_LIT>' : self . _transform_stats ( cprofile_stats ) <EOL> } </s>
