home *** CD-ROM | disk | FTP | other *** search
- <TITLE>The Stats Class -- Python library reference</TITLE>
- Prev: <A HREF="../r/reference_manual" TYPE="Prev">Reference Manual</A>
- Up: <A HREF="../r/reference_manual" TYPE="Up">Reference Manual</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>9.5.1. The <CODE>Stats</CODE> Class</H2>
- <DL><DT><B>strip_dirs</B> () -- Method on Stats<DD>
- This method for the <CODE>Stats</CODE> class removes all leading path information
- from file names. It is very useful in reducing the size of the
- printout to fit within (close to) 80 columns. This method modifies
- the object, and the stripped information is lost. After performing a
- strip operation, the object is considered to have its entries in a
- ``random'' order, as it was just after object initialization and
- loading. If <CODE>strip_dirs()</CODE> causes two function names to be
- indistinguishable (i.e., they are on the same line of the same
- filename, and have the same function name), then the statistics for
- these two entries are accumulated into a single entry.
- </DL>
- <DL><DT><B>add</B> (<VAR>filename</VAR>[, ...]) -- Method on Stats<DD>
- This method of the <CODE>Stats</CODE> class accumulates additional profiling
- information into the current profiling object. Its arguments should
- refer to filenames created by the corresponding version of
- <CODE>profile.run()</CODE>. Statistics for identically named (re: file,
- line, name) functions are automatically accumulated into single
- function statistics.
- </DL>
- <DL><DT><B>sort_stats</B> (<VAR>key</VAR>[, ...]) -- Method on Stats<DD>
- This method modifies the <CODE>Stats</CODE> object by sorting it according to the
- supplied criteria. The argument is typically a string identifying the
- basis of a sort (example: <CODE>"time"</CODE> or <CODE>"name"</CODE>).
- <P>
- When more than one key is provided, then additional keys are used as
- secondary criteria when the there is equality in all keys selected
- before them. For example, sort_stats('name', 'file') will sort all
- the entries according to their function name, and resolve all ties
- (identical function names) by sorting by file name.
- <P>
- Abbreviations can be used for any key names, as long as the
- abbreviation is unambiguous. The following are the keys currently
- defined:
- <P>
- <DL>
- <DT><I>Valid Arg</I><DD> --- <I>Meaning</I>
- <P>
- <DT><CODE>"calls"</CODE><DD>call count
- <DT><CODE>"cumulative"</CODE><DD>cumulative time
- <DT><CODE>"file"</CODE><DD>file name
- <DT><CODE>"module"</CODE><DD>file name
- <DT><CODE>"pcalls"</CODE><DD>primitive call count
- <DT><CODE>"line"</CODE><DD>line number
- <DT><CODE>"name"</CODE><DD>function name
- <DT><CODE>"nfl"</CODE><DD>name/file/line
- <DT><CODE>"stdname"</CODE><DD>standard name
- <DT><CODE>"time"</CODE><DD>internal time
- </DL>
- Note that all sorts on statistics are in descending order (placing
- most time consuming items first), where as name, file, and line number
- searches are in ascending order (i.e., alphabetical). The subtle
- distinction between <CODE>"nfl"</CODE> and <CODE>"stdname"</CODE> is that the
- standard name is a sort of the name as printed, which means that the
- embedded line numbers get compared in an odd way. For example, lines
- 3, 20, and 40 would (if the file names were the same) appear in the
- string order 20, 3 and 40. In contrast, <CODE>"nfl"</CODE> does a numeric
- compare of the line numbers. In fact, <CODE>sort_stats("nfl")</CODE> is the
- same as <CODE>sort_stats("name", "file", "line")</CODE>.
- <P>
- For compatibility with the old profiler, the numeric arguments
- `<SAMP>-1</SAMP>', `<SAMP>0</SAMP>', `<SAMP>1</SAMP>', and `<SAMP>2</SAMP>' are permitted. They are
- interpreted as <CODE>"stdname"</CODE>, <CODE>"calls"</CODE>, <CODE>"time"</CODE>, and
- <CODE>"cumulative"</CODE> respectively. If this old style format (numeric)
- is used, only one sort key (the numeric key) will be used, and
- additional arguments will be silently ignored.
- </DL>
- <DL><DT><B>reverse_order</B> () -- Method on Stats<DD>
- This method for the <CODE>Stats</CODE> class reverses the ordering of the basic
- list within the object. This method is provided primarily for
- compatibility with the old profiler. Its utility is questionable
- now that ascending vs descending order is properly selected based on
- the sort key of choice.
- </DL>
- <DL><DT><B>print_stats</B> (<VAR>restriction</VAR>[, ...]) -- Method on Stats<DD>
- This method for the <CODE>Stats</CODE> class prints out a report as described
- in the <CODE>profile.run()</CODE> definition.
- <P>
- The order of the printing is based on the last <CODE>sort_stats()</CODE>
- operation done on the object (subject to caveats in <CODE>add()</CODE> and
- <CODE>strip_dirs())</CODE>.
- <P>
- The arguments provided (if any) can be used to limit the list down to
- the significant entries. Initially, the list is taken to be the
- complete set of profiled functions. Each restriction is either an
- integer (to select a count of lines), or a decimal fraction between
- 0.0 and 1.0 inclusive (to select a percentage of lines), or a regular
- expression (to pattern match the standard name that is printed). If
- several restrictions are provided, then they are applied sequentially.
- For example:
- <P>
- <UL COMPACT><CODE> print_stats(.1, "foo:")<P>
- </CODE></UL>
- would first limit the printing to first 10% of list, and then only
- print functions that were part of filename `<SAMP>.*foo:</SAMP>'. In
- contrast, the command:
- <P>
- <UL COMPACT><CODE> print_stats("foo:", .1)<P>
- </CODE></UL>
- would limit the list to all functions having file names `<SAMP>.*foo:</SAMP>',
- and then proceed to only print the first 10% of them.
- </DL>
- <DL><DT><B>print_callers</B> (<VAR>restrictions</VAR>[, ...]) -- Method on Stats<DD>
- This method for the <CODE>Stats</CODE> class prints a list of all functions
- that called each function in the profiled database. The ordering is
- identical to that provided by <CODE>print_stats()</CODE>, and the definition
- of the restricting argument is also identical. For convenience, a
- number is shown in parentheses after each caller to show how many
- times this specific call was made. A second non-parenthesized number
- is the cumulative time spent in the function at the right.
- </DL>
- <DL><DT><B>print_callees</B> (<VAR>restrictions</VAR>[, ...]) -- Method on Stats<DD>
- This method for the <CODE>Stats</CODE> class prints a list of all function
- that were called by the indicated function. Aside from this reversal
- of direction of calls (re: called vs was called by), the arguments and
- ordering are identical to the <CODE>print_callers()</CODE> method.
- </DL>
- <DL><DT><B>ignore</B> () -- Method on Stats<DD>
- This method of the <CODE>Stats</CODE> class is used to dispose of the value
- returned by earlier methods. All standard methods in this class
- return the instance that is being processed, so that the commands can
- be strung together. For example:
- <P>
- <UL COMPACT><CODE>pstats.Stats('foofile').strip_dirs().sort_stats('cum') \<P>
- .print_stats().ignore()<P>
- </CODE></UL>
- would perform all the indicated functions, but it would not return
- the final reference to the <CODE>Stats</CODE> instance.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
- </DL>
- <H2>---------- Footnotes ----------</H2>
- <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
-
- This was once necessary, when Python would print any unused expression
- result that was not <CODE>None</CODE>. The method is still defined for
- backward compatibility.
- <P>
-