[issue29237] Create enum for pstats sorting options
Thane Brimhall
report at bugs.python.org
Tue Jan 10 20:31:34 EST 2017
New submission from Thane Brimhall:
When using the cProfile and pstats modules, I often forget which string keys are available for sorting. It would be nice to add an enum for these so a user could have their linter and IDE check that value pre-runtime.
By subclassing both `str` and `Enum` this proposal would be backwards-compatible with all existing code.
The patch for such a change would be trivial:
1. Add a new Sort class to the pstats module:
class Sort(str, enum.Enum):
calls = 'calls' # call count
cumulative = 'cumulative' # cumulative time
cumtime = 'cumtime' # cumulative time
file = 'file' # file name
filename = 'filename' # file name
module = 'module' # file name
ncalls = 'ncalls' # call count
pcalls = 'pcalls' # primitive call count
line = 'line' # line number
name = 'name' # function name
nfl = 'nfl' # name/file/line
stdname = 'stdname' # standard name
time = 'time' # internal time
tottime = 'tottime' # internal time
2. Change the print_stats method signature on the profiler base and subclasses to look like this:
def print_stats(self, sort: Sort=Sort.stdname):
Optionally, you could build the Sort enum like below to remove redundant options and increase explicitness:
class Sort(str, enum.Enum):
call_count = 'calls'
cumulative_time = 'cumulative'
filename = 'filename'
primitive_call_count = 'pcalls'
line_number = 'line'
function_name = 'name'
name_file_line = 'nfl'
standard_name = 'stdname'
internal_time = 'time'
----------
components: Library (Lib)
messages: 285178
nosy: Thane Brimhall
priority: normal
severity: normal
status: open
title: Create enum for pstats sorting options
type: enhancement
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29237>
_______________________________________
More information about the Python-bugs-list
mailing list