<s> from IPython . utils import openpy <EOL> from IPython . utils import ulinecache <EOL> from IPython . core . magic import ( Magics , magics_class , line_magic , <EOL> cell_magic , line_cell_magic ) <EOL> from ipywidgets import DOMWidget <EOL> from traitlets import Unicode , Int <EOL> from ipywidgets . widgets . widget import CallbackDispatcher , register <EOL> from pygments import highlight <EOL> from pygments . lexers import PythonLexer <EOL> from pygments . formatters import HtmlFormatter <EOL> import zipfile <EOL> import sys <EOL> from bokeh . charts import Bar <EOL> from bokeh . embed import notebook_div <EOL> from bokeh . charts . attributes import CatAttr <EOL> import bokeh . models . widgets . tables as bokeh_tables <EOL> from bokeh . models import ColumnDataSource <EOL> from bokeh . util . notebook import load_notebook <EOL> from bokeh . io import show , vform <EOL> PY3 = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> <EOL> if PY3 : <EOL> import builtins <EOL> exec_ = getattr ( builtins , "<STR_LIT>" ) <EOL> del builtins <EOL> from html import escape as html_escape <EOL> else : <EOL> def exec_ ( _code_ , _globs_ = None , _locs_ = None ) : <EOL> """<STR_LIT>""" <EOL> if _globs_ is None : <EOL> frame = sys . _getframe ( <NUM_LIT:1> ) <EOL> _globs_ = frame . f_globals <EOL> if _locs_ is None : <EOL> _locs_ = frame . f_locals <EOL> del frame <EOL> elif _locs_ is None : <EOL> _locs_ = _globs_ <EOL> exec ( """<STR_LIT>""" ) <EOL> from cgi import escape as html_escape <EOL> class IProfile ( DOMWidget ) : <EOL> def __init__ ( self , cprofile , lprofile = None , context = None , * args , ** kwargs ) : <EOL> self . generate_cprofile_tree ( cprofile , context ) <EOL> self . lprofile = lprofile <EOL> self . backward = [ ] <EOL> self . forward = [ ] <EOL> self . id_dict = { } <EOL> self . backward . append ( None ) <EOL> self . generate_content ( ) <EOL> self . on_msg ( self . handle_on_msg ) <EOL> super ( IProfile , self ) . __init__ ( value = self . value ) <EOL> def generate_cprofile_tree ( self , cprofile , context = None ) : <EOL> """<STR_LIT>""" <EOL> self . cprofile_tree = { } <EOL> for entry in cprofile : <EOL> function = entry [ <NUM_LIT:0> ] <EOL> calls_raw = entry [ <NUM_LIT:5> ] <EOL> calls = { } <EOL> if calls_raw is not None : <EOL> for call in calls_raw : <EOL> calls [ call [ <NUM_LIT:0> ] ] = { '<STR_LIT>' : call [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : call [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : call [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : call [ <NUM_LIT:4> ] } <EOL> self . cprofile_tree [ function ] = { '<STR_LIT>' : entry [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : entry [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : entry [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : entry [ <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : calls } <EOL> callcounts = dict ( [ ( function , <NUM_LIT:0> ) for function in self . cprofile_tree ] ) <EOL> for function in self . cprofile_tree : <EOL> for call in self . cprofile_tree [ function ] [ '<STR_LIT>' ] : <EOL> callcounts [ call ] += <NUM_LIT:1> <EOL> self . roots = [ ] <EOL> for i in callcounts : <EOL> if callcounts [ i ] == <NUM_LIT:0> : <EOL> self . roots . append ( i ) <EOL> self . delete_top_level ( context ) <EOL> def delete_top_level ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if context == "<STR_LIT>" : <EOL> new_roots = [ ] <EOL> for function in self . cprofile_tree : <EOL> try : <EOL> if function . co_name == "<STR_LIT>" : <EOL> new_roots += self . cprofile_tree [ function ] [ '<STR_LIT>' ] <EOL> except AttributeError : <EOL> pass <EOL> new_roots = [ r for r in new_roots if ( type ( r ) == str or <EOL> r . co_name != "<STR_LIT>" ) ] <EOL> for i in range ( len ( new_roots ) ) : <EOL> function = new_roots [ i ] <EOL> try : <EOL> if function . co_name == "<STR_LIT>" : <EOL> del new_roots [ i ] <EOL> except AttributeError : <EOL> pass <EOL> if context == "<STR_LIT>" : <EOL> new_roots = [ ] <EOL> for function in self . cprofile_tree : <EOL> try : <EOL> if "<STR_LIT>" in function . co_filename : <EOL> new_roots += self . cprofile_tree [ function ] [ '<STR_LIT>' ] <EOL> except AttributeError : <EOL> pass <EOL> new_cprofile_tree = { } <EOL> def populate_new_tree ( roots ) : <EOL> for root in roots : <EOL> if root not in new_cprofile_tree : <EOL> new_cprofile_tree [ root ] = self . cprofile_tree [ root ] <EOL> populate_new_tree ( self . cprofile_tree [ root ] [ '<STR_LIT>' ] ) <EOL> populate_new_tree ( new_roots ) <EOL> self . cprofile_tree = new_cprofile_tree <EOL> self . roots = new_roots <EOL> _view_name = Unicode ( '<STR_LIT>' ) . tag ( sync = True ) <EOL> value = Unicode ( ) . tag ( sync = True ) <EOL> n_table_elements = Int ( <NUM_LIT:0> ) . tag ( sync = True ) <EOL> def generate_content ( self , fun = None ) : <EOL> """<STR_LIT>""" <EOL> self . value_cache = "<STR_LIT>" <EOL> self . generate_nav ( fun ) <EOL> self . generate_heading ( fun ) <EOL> self . generate_table ( fun ) <EOL> if self . lprofile is not None and fun is not None : <EOL> self . generate_lprofile ( fun ) <EOL> self . value = self . value_cache <EOL> def generate_nav ( self , fun = None ) : <EOL> self . value_cache += '<STR_LIT>' <EOL> if fun is None : <EOL> self . value_cache += '<STR_LIT>' <EOL> else : <EOL> self . value_cache += ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if len ( self . backward ) > <NUM_LIT:1> : <EOL> self . value_cache += ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . value_cache += ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if len ( self . forward ) > <NUM_LIT:0> : <EOL> self . value_cache += ( '<STR_LIT>' <EOL> '<STR_LIT>' '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . value_cache += ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def generate_heading ( self , fun ) : <EOL> """<STR_LIT>""" <EOL> if fun is None : <EOL> self . value_cache += "<STR_LIT>" <EOL> return <EOL> try : <EOL> heading = "<STR_LIT>" <EOL> heading = heading . format ( fun . co_name , <EOL> self . cprofile_tree [ fun ] [ '<STR_LIT>' ] , <EOL> self . cprofile_tree [ fun ] [ '<STR_LIT>' ] ) <EOL> heading = html_escape ( heading ) <EOL> self . value_cache += "<STR_LIT>" + heading + "<STR_LIT>" <EOL> self . value_cache += ( "<STR_LIT>" + <EOL> html_escape ( fun . co_filename ) + "<STR_LIT>" ) <EOL> except AttributeError : <EOL> self . value_cache += "<STR_LIT>" + html_escape ( fun ) + "<STR_LIT>" <EOL> self . value_cache += '<STR_LIT>' <EOL> def generate_table ( self , fun ) : <EOL> """<STR_LIT>""" <EOL> if fun is None : <EOL> calls = self . cprofile_tree . keys ( ) <EOL> else : <EOL> calls = [ function for function in self . cprofile_tree [ fun ] [ '<STR_LIT>' ] ] <EOL> self . n_table_elements = len ( calls ) <EOL> names = [ call if type ( call ) == str else call . co_name for call in calls ] <EOL> calls = list ( zip ( range ( len ( calls ) ) , names , <EOL> ( self . cprofile_tree [ x ] [ '<STR_LIT>' ] for x in calls ) , <EOL> ( self . cprofile_tree [ x ] [ '<STR_LIT>' ] for x in calls ) , <EOL> calls ) ) <EOL> self . id_dict = { "<STR_LIT>" + str ( id ) : cprofile_key for <EOL> ( id , name , time , inlinetime , cprofile_key ) in calls } <EOL> calls . sort ( key = lambda x : x [ <NUM_LIT:2> ] ) <EOL> calls . reverse ( ) <EOL> try : <EOL> ids , names , times , inlinetimes = list ( zip ( * calls ) ) [ : - <NUM_LIT:1> ] <EOL> except ValueError : <EOL> return <EOL> time_plot_multiplier = <NUM_LIT:100> / max ( times ) <EOL> plot_inline_times = [ time_plot_multiplier * time for time in <EOL> inlinetimes ] <EOL> plot_extra_times = [ time_plot_multiplier * ( totaltime - inlinetime ) <EOL> for totaltime , inlinetime in zip ( times , <EOL> inlinetimes ) ] <EOL> table_data = dict ( ids = ids , names = names , times = times , <EOL> inlinetimes = inlinetimes , <EOL> plot_inline_times = plot_inline_times , <EOL> plot_extra_times = plot_extra_times ) <EOL> table_data = ColumnDataSource ( table_data ) <EOL> time_format = bokeh_tables . NumberFormatter ( format = '<STR_LIT>' ) <EOL> name_template = '<STR_LIT>' <EOL> name_format = ( bokeh_tables . <EOL> HTMLTemplateFormatter ( template = name_template ) ) <EOL> time_plot_template = ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> time_plot_format = ( bokeh_tables . <EOL> HTMLTemplateFormatter ( template = time_plot_template ) ) <EOL> columns = [ bokeh_tables . TableColumn ( title = "<STR_LIT>" , <EOL> field = "<STR_LIT>" , <EOL> formatter = name_format ) , <EOL> bokeh_tables . TableColumn ( title = "<STR_LIT>" , <EOL> field = "<STR_LIT>" , <EOL> formatter = time_format , <EOL> default_sort = "<STR_LIT>" ) , <EOL> bokeh_tables . TableColumn ( title = "<STR_LIT>" , <EOL> field = "<STR_LIT>" , <EOL> formatter = time_format , <EOL> default_sort = "<STR_LIT>" ) , <EOL> bokeh_tables . TableColumn ( title = "<STR_LIT>" , <EOL> sortable = False , <EOL> formatter = time_plot_format ) ] <EOL> bokeh_table = bokeh_tables . DataTable ( source = table_data , <EOL> columns = columns , <EOL> width = <NUM_LIT> , <EOL> height = '<STR_LIT>' , <EOL> selectable = False ) <EOL> self . value_cache += notebook_div ( bokeh_table ) <EOL> def generate_lprofile ( self , fun ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> filename = fun . co_filename <EOL> firstlineno = fun . co_firstlineno <EOL> name = fun . co_name <EOL> except AttributeError : <EOL> return <EOL> ltimings_key = ( filename , firstlineno , name ) <EOL> try : <EOL> ltimings = self . lprofile . timings [ ltimings_key ] <EOL> except KeyError : <EOL> return <EOL> filename = ltimings [ - <NUM_LIT:1> ] <EOL> if filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> filename = openpy . source_from_cache ( filename ) <EOL> if "<STR_LIT>" in filename : <EOL> add_zipped_file_to_linecache ( filename ) <EOL> raw_code = "<STR_LIT>" <EOL> linenos = range ( firstlineno , ltimings [ - <NUM_LIT:2> ] [ <NUM_LIT:0> ] + <NUM_LIT:1> ) <EOL> for lineno in linenos : <EOL> raw_code += ulinecache . getline ( filename , lineno ) <EOL> formatter = LProfileFormatter ( firstlineno , ltimings , noclasses = True ) <EOL> self . value_cache += highlight ( raw_code , PythonLexer ( ) , formatter ) <EOL> def handle_on_msg ( self , _ , content , buffers ) : <EOL> """<STR_LIT>""" <EOL> if content == "<STR_LIT>" : <EOL> self . backward . append ( None ) <EOL> self . forward = [ ] <EOL> self . generate_content ( ) <EOL> elif content == "<STR_LIT>" : <EOL> self . forward . append ( self . backward . pop ( ) ) <EOL> self . generate_content ( self . backward [ - <NUM_LIT:1> ] ) <EOL> elif content == "<STR_LIT>" : <EOL> self . backward . append ( self . forward . pop ( ) ) <EOL> self . generate_content ( self . backward [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> clicked_fun = self . id_dict [ content ] <EOL> self . backward . append ( clicked_fun ) <EOL> self . forward = [ ] <EOL> self . generate_content ( clicked_fun ) <EOL> def add_zipped_file_to_linecache ( filename ) : <EOL> ( zipped_filename , extension , inner ) = filename . partition ( '<STR_LIT>' ) <EOL> zipped_filename += extension [ : - <NUM_LIT:1> ] <EOL> assert zipfile . is_zipfile ( zipped_filename ) <EOL> zipped_file = zipfile . ZipFile ( zipped_filename ) <EOL> ulinecache . linecache . cache [ filename ] = ( None , None , <EOL> zipped_file . open ( inner , '<STR_LIT:r>' ) . <EOL> readlines ( ) ) <EOL> zipped_file . close ( ) <EOL> class LProfileFormatter ( HtmlFormatter ) : <EOL> def __init__ ( self , firstlineno , ltimings , * args , ** kwargs ) : <EOL> self . lineno = firstlineno <EOL> self . ltimings = ltimings <EOL> super ( LProfileFormatter , self ) . __init__ ( * args , ** kwargs ) <EOL> def wrap ( self , source , outfile ) : <EOL> return super ( LProfileFormatter , <EOL> self ) . wrap ( self . _wrap_code ( source ) , outfile ) <EOL> def _wrap_code ( self , source ) : <EOL> head_template = '<STR_LIT>' <EOL> no_time_template = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> time = '<STR_LIT>' <EOL> yield <NUM_LIT:0> , head_template . format ( time , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> j = <NUM_LIT:0> <EOL> for i , line in source : <EOL> lineno = self . lineno <EOL> if j < len ( self . ltimings ) and lineno == self . ltimings [ j ] [ <NUM_LIT:0> ] : <EOL> lcalls = self . ltimings [ j ] [ <NUM_LIT:1> ] <EOL> ltime = self . ltimings [ j ] [ <NUM_LIT:2> ] * <NUM_LIT> <EOL> yield i , template . format ( ltime , lcalls , lineno , line ) <EOL> j += <NUM_LIT:1> <EOL> else : <EOL> yield i , no_time_template . format ( '<STR_LIT>' , '<STR_LIT>' , lineno , line ) <EOL> self . lineno += <NUM_LIT:1> <EOL> @ magics_class <EOL> class IProfilerMagics ( Magics ) : <EOL> @ line_cell_magic <EOL> def iprofile ( self , line , cell = None ) : <EOL> import _iline_profiler <EOL> import cProfile <EOL> cprofiler = cProfile . Profile ( ) <EOL> lprofiler = _iline_profiler . LineProfiler ( ) <EOL> if cell is None : <EOL> global_ns = self . shell . user_global_ns <EOL> local_ns = self . shell . user_ns <EOL> lprofiler . enable ( ) <EOL> cprofiler . enable ( ) <EOL> exec_ ( line , global_ns , local_ns ) <EOL> cprofiler . disable ( ) <EOL> lprofiler . disable ( ) <EOL> lprofile = lprofiler . get_stats ( ) <EOL> cprofile = cprofiler . getstats ( ) <EOL> iprofile = IProfile ( cprofile , lprofile , context = "<STR_LIT>" ) <EOL> self . shell . user_ns [ '<STR_LIT>' ] = iprofile <EOL> self . shell . run_cell ( '<STR_LIT>' ) <EOL> else : <EOL> lprofiler . enable ( ) <EOL> cprofiler . enable ( ) <EOL> self . shell . run_cell ( cell ) <EOL> cprofiler . disable ( ) <EOL> lprofiler . disable ( ) <EOL> lprofile = lprofiler . get_stats ( ) <EOL> cprofile = cprofiler . getstats ( ) <EOL> iprofile = IProfile ( cprofile , lprofile , context = '<STR_LIT>' ) <EOL> self . shell . user_ns [ '<STR_LIT>' ] = iprofile <EOL> self . shell . run_cell ( '<STR_LIT>' ) <EOL> def load_ipython_extension ( shell ) : <EOL> load_notebook ( hide_banner = True ) <EOL> shell . register_magics ( IProfilerMagics ) <EOL> cell = """<STR_LIT>""" <EOL> shell . run_cell ( cell ) </s>
