grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î   fÌfã@sÝdZddlZddlZddlZddlZddlZddlmZdgZGdd„dƒZ Gdd„dƒZ dd „Z d d „Z d d „Z dd„Zdd„Zdd„Zdd„ZedkrÙddlZyddlZWnek rYnXGdd„dejƒZeejƒdkrPejdZndZykeeƒZx(ejdd…D]ZejeƒqyWeddejƒej ƒeddejƒWne!k rÕYnXndS)z3Class for printing reports on profiled python code.éN)Ú cmp_to_keyÚStatsc@sweZdZdZdddd„Zdd„Zdd „Zd d „Zd d „Zdd„Z idOd6dSd6dWd6d[d6d^d6dad6ddd6dgd6djd"6dod$6dsd'6dvd*6dzd-6d~d.6Z d/d0„Z d1d2„Z d3d4„Z d5d6„Zd7d8„Zd9d:„Zd;d<„Zd=d>„Zd?d@„ZdAdB„ZdCdD„ZdEdFdG„ZdHdI„ZdJdK„ZdS)ra<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 return self, so you can string together commands like: Stats('foo', 'goo').strip_dirs().sort_stats('calls'). print_stats(5).print_callers(5) ÚstreamNcGs_|p tj|_t|ƒs'd}n|d}|dd…}|j|ƒ|j|ŒdS)Nré)ÚsysÚstdoutrÚlenÚinitÚadd)ÚselfrÚargsÚarg©rú+/opt/alt/python34/lib64/python3.4/pstats.pyÚ__init__>s    zStats.__init__c CsÁd|_g|_d|_d|_d|_d|_d|_tƒ|_i|_ i|_ |j |ƒy|j ƒWnBt k r¼td|jr¤|jdndd|jƒ‚YnXdS)NrzInvalid timing data %srÚÚfileéÿÿÿÿ)Ú all_calleesÚfilesÚfcn_listÚtotal_ttÚ total_callsÚ prim_callsÚ max_name_lenÚsetÚ top_levelÚstatsÚ sort_arg_dictÚ load_statsÚget_top_level_statsÚ ExceptionÚprintr)r r rrrr Hs"            'z Stats.initcCsó|dkri|_dSt|tƒr™t|dƒ�}tj|ƒ|_WdQXy-tj|ƒ}tj |j ƒd|}WnYnX|g|_ n1t |dƒrÊ|j ƒ|j|_i|_n|jsïtd|j|fƒ‚ndS)NÚrbz Ú create_statsz.Cannot create or construct a %r object from %r)rÚ isinstanceÚstrÚopenÚmarshalÚloadÚosÚstatÚtimeZctimeÚst_mtimerÚhasattrr$Ú TypeErrorÚ __class__)r r ÚfZ file_statsrrrr[s(      zStats.load_statscCs²x«|jjƒD]š\}\}}}}}|j|7_|j|7_|j|7_d|krw|jj|ƒntt|ƒƒ|j krtt|ƒƒ|_ qqWdS)NÚjprofilerÚprofiler)r2rr3) rÚitemsrrrrr rÚfunc_std_stringr)r ÚfuncÚccÚncÚttÚctÚcallersrrrr qs+ zStats.get_top_level_statscGsL|s |Sx;t|ƒD]-}t|ƒt|ƒkrDt|ƒ}n|j|j7_|j|j7_|j|j7_|j|j7_x!|jD]}|jj|ƒq–W|j |j krÑ|j |_ nd|_ xg|j j ƒD]V\}}||j kr|j |}nddddif}t ||ƒ|j |rrr rrr)r ZoldstatsZnewstatsrr6r7r8r9r:r;ZnewfuncZ newcallersÚfunc2ÚcallerZold_topZnew_toprrrÚ strip_dirsås.  (       zStats.strip_dirsc Csª|jr dSi|_}x‰|jjƒD]x\}\}}}}}||kr^i|| ggð?gà?rz6 List reduced from %r to %r due to restriction <%r> ) r%r&ÚreÚcompileÚerrorÚsearchr5rbrÚfloatr`)r ZselÚlistÚmsgZnew_listZrexr6ÚcountrrrÚeval_print_amounts,  ++ zStats.eval_print_amountcCs|j}|jr9|jdd…}d|jd}nt|jjƒƒ}d}x)|D]!}|j|||ƒ\}}q[Wt|ƒ}|sœd|fSt|d|j ƒ|t|jƒkr d}x>|D]3}tt |ƒƒ|krÑtt |ƒƒ}qÑqÑWn|d|fS)Nz Ordered by: Ú z! Random listing order was used rrrX) rrrartrÚkeysrwrr"rr5)r Zsel_listÚwidthZ stat_listruZ selectionrvr6rrrÚget_print_list/s$      zStats.get_print_listcGsdx$|jD]}t|d|jƒq W|jrCtd|jƒnd}x-|jD]"}t|t|ƒd|jƒqSWt||jdddd|jƒ|j|jkrÐtd|jddd|jƒntd|jd|jƒtd|jƒ|j|ƒ\}}|r`|j ƒx|D]}|j |ƒq&Wtd|jƒtd|jƒn|S) Nrú ézfunction callsÚendz(%d primitive calls)zin %.3f secondsz ) rr"rrÚfunc_get_function_namerrrr{Ú print_titleÚ print_line)r ÚamountrCÚindentr6rzrtrrrÚ print_statsGs(  "#  zStats.print_statscGs¬|j|ƒ\}}|r¨|jƒ|j|dƒxM|D]E}||jkrn|j|||j|ƒq<|j||iƒq<Wtd|jƒtd|jƒn|S)Nz called...r)r{rnÚprint_call_headingrÚprint_call_liner"r)r r‚rzrtr6rrrÚ print_callees^s  zStats.print_calleesc Gs•|j|ƒ\}}|r‘|j|dƒx@|D]8}|j|\}}}}} |j||| dƒq2Wtd|jƒtd|jƒn|S)Nzwas called by...z<-r)r{r…rr†r"r) r r‚rzrtr6r7r8r9r:r;rrrÚ print_callersms zStats.print_callersc Cs¨tdj|ƒ|d|jƒd}xW|jjƒD]F\}}}}}|r6tt|jƒƒƒ} t| tƒ}Pq6q6W|r¤td|dd|jƒndS)Nz Function rFr|z ncalls tottime cumtime) r"ÚljustrrÚvaluesÚnextÚiterr%rf) r Ú name_sizeZ column_titleZ subheaderr7r8r9r:r;Úvaluerrrr…xs %zStats.print_call_headingz->cCsktt|ƒj|ƒ|ddd|jƒ|sFtd|jƒdSt|jƒƒ}d}x|D]þ}t|ƒ}||} t| tƒr| \} } } } | | krÁd| | f}n d| f}d|jdd t |ƒƒt | ƒt | ƒ|f}|d }n.d || t |j |d ƒf}|d }t|||d|jƒd}qeWdS) Nr~r|rrz%d/%dz%dz %s %s %s %srUrXrz %s(%r) %srG) r"r5r‰rÚsortedryr%rfÚrjustrÚf8r)r r�ÚsourceZ call_dictZarrowZclistrƒr6rRrŽr8r7r9r:ZsubstatsZ left_widthrrrr†„s*,      $ zStats.print_call_linecCs0tdddd|jƒtdd|jƒdS)Nz- ncalls tottime percall cumtime percallr~r|rzfilename:lineno(function))r"r)r rrrr€�szStats.print_titlecCs^|j|\}}}}}t|ƒ}||krK|dt|ƒ}nt|jdƒddd|jƒtt|ƒddd|jƒ|dkr´tdddd|jƒn#tt||ƒddd|jƒtt|ƒddd|jƒ|dkrtd ddd|jƒn#tt||ƒddd|jƒtt|ƒd|jƒdS) Nú/é r~r|rrr}z z )rr&r"r�rr‘r5)r r6r7r8r9r:r;Úcrrrr�¡s  " # #zStats.print_liner©rr©r–)r—rEr©rr©r˜)r™rEr©rGr©rš)r›rHr©rGr©rœ)r�rH©rJr©rž)rŸrK©rJr©r )r¡rK©rLr©r¢)r£rM©rJr©r¤)r¥rK©rPr©r¦)r§rQ©rPr©rJr©rLr©r¨r©rª)r«rSr©rr©r¬)r­rT©rUr©r®)r¯rVr©rXr©r°)r±rYr©rXr©r²)r³rY)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr rr r rDrZr^rgrirmrnrwr{r„r‡rˆr…r†r€r�rrrrr sH                c@s.eZdZdZdd„Zdd„ZdS)rda•This class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or decending) for each tuple-index. The compare functions can then be used as the function argument to the system sort() function when a list of tuples need to be sorted in the instances order.cCs ||_dS)N)Úcomp_select_list)r r¸rrrr»szTupleComp.__init__cCsSxL|jD]A\}}||}||}||kr;| S||kr |Sq WdS)Nr)r¸)r ÚleftÚrightÚindexZ directionÚlÚrrrrre¾s    zTupleComp.compareN)r´rµr¶r·rrerrrrrd³s  rdcCs(|\}}}tjj|ƒ||fS)N)r*ÚpathÚbasename)Ú func_namerCrNrRrrrrjÌsrjcCs|dS)NrXr)r6rrrrÐsrcCsc|dd…d krW|d}|jdƒrP|jdƒrPd|dd …S|Snd|SdS) NrXú~rú<ú>z{%s}rz %s:%d(%s))rÁrr)Ú startswithÚendswith)rÀrRrrrr5Ós  r5c CsV|\}}}}}|\}}} } } |||||| || t| |ƒfS)z3Add together all the stats for two profile entries.)Ú add_callers) Útargetr’r7r8r9r:r;Zt_ccZt_ncZt_ttZt_ctZ t_callersrrrr>äsr>cCs±i}x$|jƒD]\}}|||ôs zadd_callers..)r4r%rfÚzip)rÇr’Z new_callersr6rlrrrrÆës  !rÆcCs+d}x|jƒD]}||7}qW|S)z@Sum the caller statistics to get total number of calls received.r)rŠ)r;r8rFrrrÚ count_callsýsrÌcCsd|S)Nz%8.3fr)Úxrrrr‘sr‘Ú__main__c@sKeZdZddd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„ZdS)6ÚProfileBrowserNcCsNtjj|ƒd|_d|_tj|_|dk rJ|j|ƒndS)Nz% ) ÚcmdÚCmdrÚpromptrrrrÚdo_read)r Úprofilerrrrs     zProfileBrowser.__init__cCsþ|jƒ}g}x°|D]¨}y|jt|ƒƒwWntk rMYnXyQt|ƒ}|dksu|dkrŽtdd|jƒwn|j|ƒwWntk r³YnX|j|ƒqW|jrçt|j|ƒ|Œntdd|jƒdS)Nrrz#Fraction argument must be in [0, 1]rzNo statistics object is loaded.) Úsplitrbr`Ú ValueErrorrsr"rrÚgetattr)r ÚfnrNr Z processedZtermZfracrrrÚgenerics,       zProfileBrowser.genericcCsvtdd|jƒtdd|jƒtdd|jƒtdd|jƒtdd|jƒtdd|jƒdS)NzArguments may be:rz0* An integer maximum number of entries to print.z:* A decimal fractional number between 0 and 1, controllingz- what fraction of selected entries to print.z8* A regular expression; only entries with function namesz that match it are printed.)r"r)r rrrÚ generic_help7s zProfileBrowser.generic_helpcCs3|jr|jj|ƒntdd|jƒdS)NzNo statistics object is loaded.rr)rr r"r)r rNrrrÚdo_add?s zProfileBrowser.do_addcCstdd|jƒdS)Nz>Add profile info from given file to current statistics object.r)r"r)r rrrÚhelp_addEszProfileBrowser.help_addcCs|jd|ƒS)Nr‡)rÙ)r rNrrrÚ do_calleesHszProfileBrowser.do_calleescCs!tdd|jƒ|jƒdS)Nz6Print callees statistics from the current stat object.r)r"rrÚ)r rrrÚ help_calleesJszProfileBrowser.help_calleescCs|jd|ƒS)Nrˆ)rÙ)r rNrrrÚ do_callersNszProfileBrowser.do_callerscCs!tdd|jƒ|jƒdS)Nz6Print callers statistics from the current stat object.r)r"rrÚ)r rrrÚ help_callersPszProfileBrowser.help_callerscCstdd|jƒdS)Nrrr)r"r)r rNrrrÚdo_EOFTszProfileBrowser.do_EOFcCstdd|jƒdS)NzLeave the profile brower.r)r"r)r rrrÚhelp_EOFWszProfileBrowser.help_EOFcCsdS)Nrr)r rNrrrÚdo_quitZszProfileBrowser.do_quitcCstdd|jƒdS)NzLeave the profile brower.r)r"r)r rrrÚ help_quit\szProfileBrowser.help_quitcCs|r·yt|ƒ|_Wn‹tk r^}z#t|jdd|jƒdSWYdd}~XnItk r¦}z)t|jjd|d|jƒdSWYdd}~XnX|d|_ nKt |j ƒdkrï|j dd…}|j |ƒntdd|jƒdS) Nrrú:z% rXz1No statistics object is current -- cannot reload.réþÿÿÿ) rrÚOSErrorr"r rr!r0r´rÒrrÓ)r rNÚerrrrrrÓ_s zProfileBrowser.do_readcCs*tdd|jƒtdd|jƒdS)Nz+Read in profile data from a specified file.rz*Without argument, reload the current file.)r"r)r rrrÚ help_readpszProfileBrowser.help_readcCs0|jr|jjƒntdd|jƒdS)NzNo statistics object is loaded.rr)rrir"r)r rNrrrÚ do_reversets zProfileBrowser.do_reversecCstdd|jƒdS)Nz/Reverse the sort order of the profiling report.r)r"r)r rrrÚ help_reversezszProfileBrowser.help_reversecsË|js tdd|jƒdS|jjƒ‰|rst‡fdd†|jƒDƒƒrs|jj|jƒŒnTtdd|jƒx>tjj ƒD]-\}}td||dfd|jƒq–WdS) NzNo statistics object is loaded.rc3s|]}|ˆkVqdS)Nr)rÈrÍ)Úabbrevsrrú ‚sz)ProfileBrowser.do_sort..z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) rr"rr^ÚallrÕrgrrZr4)r rNr_rŽr)rìrÚdo_sort}s +%zProfileBrowser.do_sortcCs*tdd|jƒtdd|jƒdS)Nz.Sort profile data according to specified keys.rz3(Typing `sort' without arguments lists valid keys.))r"r)r rrrÚ help_sort‰szProfileBrowser.help_sortcs‡fdd†tjDƒS)Ncs%g|]}|jˆƒr|‘qSr)rÄ)rÈÚa)ÚtextrrrÊ�s z0ProfileBrowser.complete_sort..)rrZ)r ròr r)ròrÚ complete_sortŒszProfileBrowser.complete_sortcCs|jd|ƒS)Nr„)rÙ)r rNrrrÚdo_stats�szProfileBrowser.do_statscCs!tdd|jƒ|jƒdS)Nz.Print statistics from the current stat object.r)r"rrÚ)r rrrÚ help_stats‘szProfileBrowser.help_statscCs0|jr|jjƒntdd|jƒdS)NzNo statistics object is loaded.r)rrmr"r)r rNrrrÚdo_strip•s zProfileBrowser.do_stripcCstdd|jƒdS)NzrÆrÌr‘r´rÐÚreadlineÚ ImportErrorrÑrÏrÚargvZ initprofileZbrowserrÔrÛr"rZcmdloopÚKeyboardInterruptrrrrÚsH      ÿ”         �