Spade
Mini Shell
3
\�g�@sfdZddlZddlZddlZddlZddlZddlmZdgZGdd�d�Z Gdd�d�Z
dd �Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zedk�rbddlZyddlZWnek
r�YnXGdd�dej�Zeej�dkr�ejdZndZyTee�Zx"ejdd�D]Zeje��qWedejd�ej
�edejd�Wne!k
�r`YnXdS)z3Class for printing reports on profiled python
code.�N)�
cmp_to_key�Statsc@s�eZdZdZdd�dd�Zdd�Zdd �Zd
d�Zdd
�Zdd�Z dBdFdJdNdQdTdWdZd]dbdfdidmdqd!�Z
d"d#�Zd$d%�Zd&d'�Z
d(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zdrd9d:�Zd;d<�Zd=d>�ZdS)sra�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 that are respectively interpreted as
'stdname', 'calls', 'time', and
'cumulative'). 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)
N)�streamcGsF|ptj|_t|�sd}n|d}|dd�}|j|�|j|�dS)Nr�)�sys�stdoutr�len�init�add)�selfr�args�arg�r�/usr/lib64/python3.6/pstats.py�__init__As
zStats.__init__cCs�d|_g|_d|_d|_d|_d|_d|_t�|_i|_ i|_
|j|�y|j�Wn8t
k
r�td|jrx|jdnd|jd��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)rr
rrrr Ks"
z
Stats.initcCs�|dkri|_dSt|t�rxt|d��}tj|�|_WdQRXy"tj|�}tj |j
�d|}WnYnX|g|_n
t|d�r�|j
�|j|_i|_|js�td|j|f��dS)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__)rr
�fZ
file_statsrrrr^s(
zStats.load_statscCs�x~|jj�D]p\}\}}}}}|j|7_|j|7_|j|7_d|kr\|jj|�tt|��|j krtt|��|_ qWdS)N�jprofiler�profiler)r2rr3)
r�itemsrrrrr
r�func_std_stringr)r�func�cc�nc�tt�ct�callersrrrr
tszStats.get_top_level_statscGs�|s|Sx�t|�D]�}t|�t|�kr.t|�}|j|j7_|j|j7_|j|j7_|j|j7_x|jD]}|jj|�qvW|j |j kr�|j |_ d|_
xJ|jj�D]<\}}||jkr�|j|}nddddif}t
||�|j|<q�WqW|S)Nr)�reversed�typerrrrrrr
rrrr4�add_func_stats)r�arg_list�itemr6r+Z
old_func_statrrrr
~s(
z Stats.addc
Cs(t|d��}tj|j|�WdQRXdS)z:Write the profile data to a
file we know how to load
back.�wbN)r'r(�dumpr)r�filenamer1rrr�
dump_stats�szStats.dump_statsr�
call count��cumulative time�� file name��line
number��
function name�name/file/liner�primitive call
count��
standard name��
internal
time)�callsZncallsZcumtime�
cumulativerrC�line�module�nameZnflZpcalls�stdnamer,ZtottimecCs�|jszi|_}i}xP|jj�D]B\}}|}x4|r`|s8P||krJd||<P|||<|dd�}q.Wq
Wx|D]
}||=qlW|jS)z)Expand all abbreviations that are
unique.rNrr)r�sort_arg_dict_defaultr4)r�dictZbad_list�word�tupZfragmentrrr�get_sort_arg_defs�s
zStats.get_sort_arg_defscGs"|sd|_|St|�dkrFt|dt�rFddddddddi|dg}|j�}f}d|_d}x:|D]2}|||d}|j|||d7_d }qbWg}xB|jj�D]4\}\}} }
}}|j|| |
|f|t |�|f�q�W|j
tt|�j
�d
�g|_}
x|D]}|
j|d��qW|S)
NrrrYrTr,rRrUrz,
)�keyrr)rrr%�intr^� sort_typerr4�appendr5�sortr� TupleComp�compare)rZfieldZ
sort_arg_defsZ
sort_tupleZ connectorr\Z
stats_listr6r7r8r9r:r;r�tuplerrr�
sort_stats�s2
zStats.sort_statscCs|jr|jj�|S)N)r�reverse)rrrr�
reverse_order�s
zStats.reverse_ordercCs�|j}i|_}d}x�|j�D]�\}\}}}}} t|�}
tt|
��|krTtt|
��}i}x | j�D]\}}
|
|t|�<qbW|
|kr�t||
|||||f�||
<q|||||f||
<qW|j}t�|_}x|D]}|jt|��q�W||_ d|_
d|_|S)Nr)rr4�func_strip_pathrr5r>rrr
rrr)rZoldstatsZnewstatsrr6r7r8r9r:r;ZnewfuncZ
newcallers�func2�callerZold_topZnew_toprrr�
strip_dirs�s.
zStats.strip_dirsc
Cs||jr
dSi|_}xb|jj�D]T\}\}}}}}||krBi||<x0|j�D]$\}} ||krdi||<| |||<qLWq
WdS)N)rrr4)
rrr6r7r8r9r:r;rkrlrrr�calc_calleess
zStats.calc_calleesc
Cs|}t|t�rpytj|�}Wn&tjk
rB|d|7}||fSXg}x�|D]}|jt|��rN|j|�qNWnzt|�}t|t �r�d|ko�dknr�t
||d�}|d|�}n2t|t
�r�d|ko�|knr�|}|d|�}t|�t|�k�r|dt|�t|�|f7}||fS)Nz#
<Invalid regular expression %r>
gg�?g�?rz6 List reduced from %r to %r due to restriction <%r>
)r%r&�re�compile�error�searchr5rbr�floatr`)rZsel�list�msgZnew_listZrexr6�countrrr�eval_print_amounts,
""zStats.eval_print_amountcCs�|j}|jr*|jdd�}d|jd}nt|jj��}d}x|D]}|j|||�\}}qBWt|�}|spd|fSt||j d�|t|j�kr�d}x(|D]
}tt
|��|kr�tt
|��}q�W|d|fS)Nz Ordered by: �
z! Random listing order was used
r)rrR)rrrartr�keysrwrr"rr5)rZsel_list�widthZ stat_listruZ selectionrvr6rrr�get_print_list2s$
zStats.get_print_listcGs�x|jD]}t||jd�qW|jr0t|jd�d}x"|jD]}t|t|�|jd�q<Wt||jdd|jd�|j|jkr�td|jd|jd�td|j|jd�t|jd�|j|�\}}|r�|j �x|D]}|j
|�q�Wt|jd�t|jd�|S) N)r� �zfunction
calls)�endrz(%d primitive calls)zin %.3f secondsz
)rr"rr�func_get_function_namerrrr{�print_title�
print_line)r�amountrC�indentr6rzrtrrr�print_statsJs(
zStats.print_statscGs~|j|�\}}|rz|j�|j|d�x:|D]2}||jkrP|j|||j|�q,|j||i�q,Wt|jd�t|jd�|S)Nz called...)r)r{rn�print_call_headingr�print_call_liner"r)rr�rzrtr6rrr�
print_calleesas
zStats.print_calleesc
Gsl|j|�\}}|rh|j|d�x0|D](}|j|\}}}}} |j||| d�q$Wt|jd�t|jd�|S)Nzwas
called by...z<-)r)r{r�rr�r"r)
rr�rzrtr6r7r8r9r:r;rrr�
print_callersps
zStats.print_callersc
Csxtdj|�||jd�d}x<|jj�D].\}}}}}|r(tt|j���} t| t�}Pq(W|rttd|d|jd�dS)Nz Function
)rFr|z ncalls tottime
cumtime) r"�ljustrr�values�next�iterr%rf)
r� name_sizeZcolumn_titleZ subheaderr7r8r9r:r;�valuerrrr�{s
zStats.print_call_heading�->cCstt|�j|�|d|jd�|s2t|jd�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||||jd�d}qHWdS)
Nr|)r~r)rrz%d/%dz%dz%s
%s %s %srPrRrz %s(%r)
%srF)r"r5r�r�sortedryr%rf�rjustr�f8r)rr��sourceZ call_dictZarrowZclistr�r6rXr�r8r7r9r:ZsubstatsZ
left_widthrrrr��s*
zStats.print_call_linecCs"tdd|jd�td|jd�dS)Nz-
ncalls tottime percall cumtime
percallr|)r~rzfilename:lineno(function))r)r"r)rrrrr��szStats.print_titlecCs�|j|\}}}}}t|�}||kr4|dt|�}t|jd�d|jd�tt|�d|jd�|dkrxtdd|jd�ntt||�d|jd�tt|�d|jd�|dkr�td d|jd�ntt||�d|jd�tt|�|jd�dS)
N�/� r|)r~rrr})rz z
)rr&r"r�rr�r5)rr6r7r8r9r:r;�crrrr��szStats.print_liner�rr�r�)r�rEr�rr�r�)r�rEr�rFr�r�)r�rGr�rFr�r�)r�rG�rHr�r�)r�rI�rHr�r�)r�rI�rJr�r�)r�rK�rHr�r�)r�rI�rLr�r�)r�rM�rLr�rHr�rJr�r�r�r�)r�rNr�rr�r�)r�rO�rPr�r�)r�rQr�rRr�r�)r�rSr�rRr�r�)r�rS)r�)�__name__�
__module__�__qualname__�__doc__rr rr r
rDrZr^rgrirmrnrwr{r�r�r�r�r�r�r�rrrrr sF
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)rr�rrrr�szTupleComp.__init__cCsBx<|jD]2\}}||}||}||kr.|S||kr|SqWdS)Nr)r�)r�left�right�index� direction�l�rrrrre�szTupleComp.compareN)r�r�r�r�rrerrrrrd�srdcCs|\}}}tjj|�||fS)N)r*�path�basename)� func_namerCrVrXrrrrj�s
rjcCs|dS)NrRr)r6rrrr�srcCsN|dd�d krB|d}|jd�r<|jd�r<d|dd
�S|Snd|SdS)NrR�~r�<�>z{%s}rz %s:%d(%s))r�rr)�
startswith�endswith)r�rXrrrr5�sr5cCs@|\}}}}}|\}}} }
}|||||| ||
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]\}}|||<qWxb|j�D]V\}}||krzt|t�rhtdd�t|||�D��||<q�|||7<q,|||<q,W|S)z*Combine
two caller lists in a single
list.cSsg|]}|d|d�qS)rrr)�.0�irrr�
<listcomp>�szadd_callers.<locals>.<listcomp>)r4r%rf�zip)r�r�Znew_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;r8rTrrr�count_callssr�cCsd|S)Nz%8.3fr)�xrrrr�sr��__main__c@s�eZdZd6dd�Zdd�Zdd�Zdd �Zd
d�Zdd
�Zdd�Z dd�Z
dd�Zdd�Zdd�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)7�ProfileBrowserNcCs6tjj|�d|_d|_tj|_|dk r2|j|�dS)Nz%
) �cmd�Cmdr�promptrrrr�do_read)r�profilerrrrszProfileBrowser.__init__cCs�|j�}g}x�|D]�}y|jt|��wWntk
r>YnXy8t|�}|dksZ|dkrjtd|jd�w|j|�wWntk
r�YnX|j|�qW|jr�t|j|�|�ntd|jd�dS)Nrrz#Fraction
argument must be in [0, 1])rzNo statistics object is
loaded.) �splitrbr`�
ValueErrorrsr"rr�getattr)r�fnrVrZ processedZtermZfracrrr�generic"s,
zProfileBrowser.genericcCsXtd|jd�td|jd�td|jd�td|jd�td|jd�td|jd�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)rrrr�generic_help:szProfileBrowser.generic_helpcCsd|jrRy|jj|�Wq`tk
rN}ztd||f|jd�WYdd}~Xq`Xntd|jd�dS)Nz$Failed to
load statistics for %s: %s)rzNo statistics object is loaded.r)rr
�IOErrorr"r)rrV�errr�do_addBs*zProfileBrowser.do_addcCstd|jd�dS)Nz>Add
profile info from given file to current statistics
object.)r)r"r)rrrr�help_addKszProfileBrowser.help_addcCs|jd|�S)Nr�)r�)rrVrrr�
do_calleesNszProfileBrowser.do_calleescCstd|jd�|j�dS)Nz6Print
callees statistics from the current stat
object.)r)r"rr�)rrrr�help_calleesPszProfileBrowser.help_calleescCs|jd|�S)Nr�)r�)rrVrrr�
do_callersTszProfileBrowser.do_callerscCstd|jd�|j�dS)Nz6Print
callers statistics from the current stat
object.)r)r"rr�)rrrr�help_callersVszProfileBrowser.help_callerscCstd|jd�dS)Nr)rr)r"r)rrVrrr�do_EOFZszProfileBrowser.do_EOFcCstd|jd�dS)NzLeave
the profile
brower.)r)r"r)rrrr�help_EOF]szProfileBrowser.help_EOFcCsdS)Nrr)rrVrrr�do_quit`szProfileBrowser.do_quitcCstd|jd�dS)NzLeave
the profile
brower.)r)r"r)rrrr� help_quitbszProfileBrowser.help_quitcCs�|r�yt|�|_Wnjtk
rF}zt|jd|jd�dSd}~Xn8tk
r|}zt|jjd||jd�dSd}~XnX|d|_ n6t
|j �dkr�|j dd�}|j|�ntd|jd�dS) Nr)r�:z%
rRz1No statistics object is current -- cannot
reload.r���)rr�OSErrorr"rrr!r0r�r�rr�)rrV�errrrrr�eszProfileBrowser.do_readcCs
td|jd�td|jd�dS)Nz+Read in profile data from a specified
file.)rz*Without argument, reload the current
file.)r"r)rrrr� help_readvszProfileBrowser.help_readcCs$|jr|jj�ntd|jd�dS)NzNo
statistics object is loaded.)rr)rrir"r)rrVrrr�
do_reversezszProfileBrowser.do_reversecCstd|jd�dS)Nz/Reverse
the sort order of the profiling
report.)r)r"r)rrrr�help_reverse�szProfileBrowser.help_reversecs�|jstd|jd�dS|jj��|rRt�fdd�|j�D��rR|jj|j��n@td|jd�x0tjj �D]"\}}td||df|jd�qlWdS) NzNo
statistics object is
loaded.)rc3s|]}|�kVqdS)Nr)r�r�)�abbrevsrr� <genexpr>�sz)ProfileBrowser.do_sort.<locals>.<genexpr>z/Valid
sort keys (unique prefixes are accepted):z%s -- %srr)
rr"rr^�allr�rgrrZr4)rrVr_r�r)r�r�do_sort�s
zProfileBrowser.do_sortcCs
td|jd�td|jd�dS)Nz.Sort profile data according to specified
keys.)rz3(Typing `sort' without arguments lists valid
keys.))r"r)rrrr� help_sort�szProfileBrowser.help_sortcs�fdd�tjD�S)Ncsg|]}|j��r|�qSr)r�)r��a)�textrrr��sz0ProfileBrowser.complete_sort.<locals>.<listcomp>)rrZ)rr�rr)r�r�
complete_sort�szProfileBrowser.complete_sortcCs|jd|�S)Nr�)r�)rrVrrr�do_stats�szProfileBrowser.do_statscCstd|jd�|j�dS)Nz.Print
statistics from the current stat object.)r)r"rr�)rrrr�
help_stats�szProfileBrowser.help_statscCs$|jr|jj�ntd|jd�dS)NzNo
statistics object is
loaded.)r)rrmr"r)rrVrrr�do_strip�szProfileBrowser.do_stripcCstd|jd�dS)Nz<Strip
leading path information from filenames in the
report.)r)r"r)rrrr�
help_strip�szProfileBrowser.help_stripcCstd|jd�dS)NzShow
help for a given
command.)r)r"r)rrrr� help_help�szProfileBrowser.help_helpcCs|r|SdS)Nr)r�stoprVrrr�postcmd�szProfileBrowser.postcmd)N)r�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�s4
r�rrRz*Welcome to the
profile statistics
browser.)rzGoodbye.)"r�rr*r,r(ro� functoolsr�__all__rrdrjrr5r>r�r�r�r�r��readline�ImportErrorr�r�r�argvZinitprofileZbrowserr�r�r"rZcmdloop�KeyboardInterruptrrrr�<module>sN