@@ -139,6 +139,7 @@ The :mod:`pstats` module's :class:`~pstats.Stats` class has a variety of methods
139139for manipulating and printing the data saved into a profile results file::
140140
141141 import pstats
142+ from pstats import SortKey
142143 p = pstats.Stats('restats')
143144 p.strip_dirs().sort_stats(-1).print_stats()
144145
@@ -148,14 +149,14 @@ entries according to the standard module/line/name string that is printed. The
148149:meth: `~pstats.Stats.print_stats ` method printed out all the statistics. You
149150might try the following sort calls::
150151
151- p.sort_stats('name' )
152+ p.sort_stats(SortKey.NAME )
152153 p.print_stats()
153154
154155The first call will actually sort the list by function name, and the second call
155156will print out the statistics. The following are some interesting calls to
156157experiment with::
157158
158- p.sort_stats('cumulative' ).print_stats(10)
159+ p.sort_stats(SortKey.CUMULATIVE ).print_stats(10)
159160
160161This sorts the profile by cumulative time in a function, and then only prints
161162the ten most significant lines. If you want to understand what algorithms are
@@ -164,20 +165,20 @@ taking time, the above line is what you would use.
164165If you were looking to see what functions were looping a lot, and taking a lot
165166of time, you would do::
166167
167- p.sort_stats('time' ).print_stats(10)
168+ p.sort_stats(SortKey.TIME ).print_stats(10)
168169
169170to sort according to time spent within each function, and then print the
170171statistics for the top ten functions.
171172
172173You might also try::
173174
174- p.sort_stats('file' ).print_stats('__init__')
175+ p.sort_stats(SortKey.FILENAME ).print_stats('__init__')
175176
176177This will sort all the statistics by file name, and then print out statistics
177178for only the class init methods (since they are spelled with ``__init__ `` in
178179them). As one final example, you could try::
179180
180- p.sort_stats('time', 'cumulative' ).print_stats(.5, 'init')
181+ p.sort_stats(SortKey.TIME, SortKey.CUMULATIVE ).print_stats(.5, 'init')
181182
182183This line sorts statistics with a primary key of time, and a secondary key of
183184cumulative time, and then prints out some of the statistics. To be specific, the
@@ -250,12 +251,13 @@ functions:
250251 without writing the profile data to a file::
251252
252253 import cProfile, pstats, io
254+ from pstats import SortKey
253255 pr = cProfile.Profile()
254256 pr.enable()
255257 # ... do something ...
256258 pr.disable()
257259 s = io.StringIO()
258- sortby = 'cumulative'
260+ sortby = SortKey.CUMULATIVE
259261 ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
260262 ps.print_stats()
261263 print(s.getvalue())
@@ -361,60 +363,65 @@ Analysis of the profiler data is done using the :class:`~pstats.Stats` class.
361363 .. method :: sort_stats(*keys)
362364
363365 This method modifies the :class: `Stats ` object by sorting it according to
364- the supplied criteria. The argument is typically a string identifying the
365- basis of a sort (example: ``'time' `` or ``'name' ``).
366+ the supplied criteria. The argument can be either a string or a SortKey
367+ enum identifying the basis of a sort (example: ``'time' ``, ``'name' ``,
368+ ``SortKey.TIME `` or ``SortKey.NAME ``). The SortKey enums argument have
369+ advantage over the string argument in that it is more robust and less
370+ error prone.
366371
367372 When more than one key is provided, then additional keys are used as
368373 secondary criteria when there is equality in all keys selected before
369- them. For example, ``sort_stats('name', 'file') `` will sort all the
370- entries according to their function name, and resolve all ties (identical
371- function names) by sorting by file name.
372-
373- Abbreviations can be used for any key names, as long as the abbreviation
374- is unambiguous. The following are the keys currently defined:
375-
376- +------------------+----------------------+
377- | Valid Arg | Meaning |
378- +==================+======================+
379- | ``'calls' `` | call count |
380- +------------------+----------------------+
381- | ``'cumulative' `` | cumulative time |
382- +------------------+----------------------+
383- | ``'cumtime' `` | cumulative time |
384- +------------------+----------------------+
385- | ``'file' `` | file name |
386- +------------------+----------------------+
387- | ``'filename' `` | file name |
388- +------------------+----------------------+
389- | ``'module' `` | file name |
390- +------------------+----------------------+
391- | ``'ncalls' `` | call count |
392- +------------------+----------------------+
393- | ``'pcalls' `` | primitive call count |
394- +------------------+----------------------+
395- | ``'line' `` | line number |
396- +------------------+----------------------+
397- | ``'name' `` | function name |
398- +------------------+----------------------+
399- | ``'nfl' `` | name/file/line |
400- +------------------+----------------------+
401- | ``'stdname' `` | standard name |
402- +------------------+----------------------+
403- | ``'time' `` | internal time |
404- +------------------+----------------------+
405- | ``'tottime' `` | internal time |
406- +------------------+----------------------+
374+ them. For example, ``sort_stats(SortKey.NAME, SortKey.FILE) `` will sort
375+ all the entries according to their function name, and resolve all ties
376+ (identical function names) by sorting by file name.
377+
378+ For the string argument, abbreviations can be used for any key names, as
379+ long as the abbreviation is unambiguous.
380+
381+ The following are the valid string and SortKey:
382+
383+ +------------------+---------------------+----------------------+
384+ | Valid String Arg | Valid enum Arg | Meaning |
385+ +==================+=====================+======================+
386+ | ``'calls' `` | SortKey.CALLS | call count |
387+ +------------------+---------------------+----------------------+
388+ | ``'cumulative' `` | SortKey.CUMULATIVE | cumulative time |
389+ +------------------+---------------------+----------------------+
390+ | ``'cumtime' `` | N/A | cumulative time |
391+ +------------------+---------------------+----------------------+
392+ | ``'file' `` | N/A | file name |
393+ +------------------+---------------------+----------------------+
394+ | ``'filename' `` | SortKey.FILENAME | file name |
395+ +------------------+---------------------+----------------------+
396+ | ``'module' `` | N/A | file name |
397+ +------------------+---------------------+----------------------+
398+ | ``'ncalls' `` | N/A | call count |
399+ +------------------+---------------------+----------------------+
400+ | ``'pcalls' `` | SortKey.PCALLS | primitive call count |
401+ +------------------+---------------------+----------------------+
402+ | ``'line' `` | SortKey.LINE | line number |
403+ +------------------+---------------------+----------------------+
404+ | ``'name' `` | SortKey.NAME | function name |
405+ +------------------+---------------------+----------------------+
406+ | ``'nfl' `` | SortKey.NFL | name/file/line |
407+ +------------------+---------------------+----------------------+
408+ | ``'stdname' `` | SortKey.STDNAME | standard name |
409+ +------------------+---------------------+----------------------+
410+ | ``'time' `` | SortKey.TIME | internal time |
411+ +------------------+---------------------+----------------------+
412+ | ``'tottime' `` | N/A | internal time |
413+ +------------------+---------------------+----------------------+
407414
408415 Note that all sorts on statistics are in descending order (placing most
409416 time consuming items first), where as name, file, and line number searches
410417 are in ascending order (alphabetical). The subtle distinction between
411- ``'nfl' `` and ``'stdname' `` is that the standard name is a sort of the
412- name as printed, which means that the embedded line numbers get compared
413- in an odd way. For example, lines 3, 20, and 40 would (if the file names
414- were the same) appear in the string order 20, 3 and 40. In contrast,
415- `` 'nfl' `` does a numeric compare of the line numbers. In fact,
416- ``sort_stats('nfl' ) `` is the same as `` sort_stats('name', 'file',
417- 'line' ) ``.
418+ ``SortKey.NFL `` and ``SortKey.STDNAME `` is that the standard name is a
419+ sort of the name as printed, which means that the embedded line numbers
420+ get compared in an odd way. For example, lines 3, 20, and 40 would (if
421+ the file names were the same) appear in the string order 20, 3 and 40.
422+ In contrast, `` SortKey.NFL `` does a numeric compare of the line numbers.
423+ In fact, ``sort_stats(SortKey.NFL ) `` is the same as
424+ `` sort_stats(SortKey.NAME, SortKey.FILENAME, SortKey.LINE ) ``.
418425
419426 For backward-compatibility reasons, the numeric arguments ``-1 ``, ``0 ``,
420427 ``1 ``, and ``2 `` are permitted. They are interpreted as ``'stdname' ``,
@@ -424,6 +431,8 @@ Analysis of the profiler data is done using the :class:`~pstats.Stats` class.
424431
425432 .. For compatibility with the old profiler.
426433
434+ .. versionadded :: 3.7
435+ Added the SortKey enum.
427436
428437 .. method :: reverse_order()
429438
0 commit comments