This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Create enum for pstats sorting options
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mariatta, Thane Brimhall, cheryl.sabella, ethan.furman, louielu, mawidjaj, rhettinger, rtnpro
Priority: normal Keywords: patch

Created on 2017-01-11 01:31 by Thane Brimhall, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5103 merged mawidjaj, 2018-01-05 03:52
PR 5336 closed ethan.furman, 2018-01-26 05:09
Messages (13)
msg285178 - (view) Author: Thane Brimhall (Thane Brimhall) * Date: 2017-01-11 01:31
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'
msg285179 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2017-01-11 01:45
To keep backwards compatibility and lesson the burden on new code, simply make the value of the duplicate names be the same:

    cumulative = 'cumulative'
    cumtime = 'cumulative'

This way the standard name is 'cumulative' but 'cumtime' works as well.
msg286390 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-01-27 22:45
Mariatta, would you like to prepare a patch for this.  Ethan and I will review it.
msg288087 - (view) Author: Ratnadeep Debnath (rtnpro) * Date: 2017-02-18 19:47
I am taking up to create a patch/pull request for this to get it reviewed.
msg288135 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-02-19 14:05
Thank you, Ratnadeep.
I've been assigned to this issue, and have made some progress on it. I'm away for a conference right now, and I plan on getting back once I'm back.
Will it be possible for you to work on a different issue?
msg288285 - (view) Author: Ratnadeep Debnath (rtnpro) * Date: 2017-02-21 10:17
It's good to hear that you're working on it. You can go ahead with this. Anyways, I will be around to see and learn how you fix this issue :)
msg292146 - (view) Author: Louie Lu (louielu) * Date: 2017-04-23 02:41
Mariatta, is there any movement on this issue?

msg295458 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2017-06-08 18:04
Marriatta, perhaps it is time to let Ratnadeep work on this issue?
msg295459 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-06-08 18:34
Thanks for checking in, Ethan.

Sorry, since this was first assigned to me, I got pre-occupied with other issues and projects in the core-workflow.

Ratnadeep, if you are still interested, please go ahead with this issue. I've unassigned myself.

msg309268 - (view) Author: Marcel Widjaja (mawidjaj) * Date: 2017-12-31 05:31
I'd like to try to work on this one
msg310735 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2018-01-26 04:49
New changeset 863b1e4d0e95036bca4e97c1b8b2ca72c19790fb by Ethan Furman (mwidjaja) in branch 'master':
bpo-29237: Create enum for pstats sorting options (GH-5103)
msg312865 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-02-25 21:31
Can this one be closed since it was an enhancement and doesn't need to be backported?
msg312870 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2018-02-26 01:23
Thanks for catching that!
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73423
2018-02-26 01:23:43ethan.furmansetstatus: open -> closed
resolution: fixed
messages: + msg312870

stage: patch review -> resolved
2018-02-25 21:31:25cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg312865
2018-01-26 05:09:18ethan.furmansetpull_requests: + pull_request5183
2018-01-26 04:49:58ethan.furmansetmessages: + msg310735
2018-01-05 03:52:40mawidjajsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request4972
2017-12-31 05:31:06mawidjajsetnosy: + mawidjaj
messages: + msg309268
2017-06-08 18:34:17Mariattasetassignee: Mariatta ->
messages: + msg295459
2017-06-08 18:04:48ethan.furmansetmessages: + msg295458
2017-04-23 02:41:40louielusetnosy: + louielu
messages: + msg292146
2017-02-21 10:17:30rtnprosetmessages: + msg288285
2017-02-19 14:05:23Mariattasetmessages: + msg288135
2017-02-18 19:47:02rtnprosetnosy: + rtnpro
messages: + msg288087
2017-01-27 22:45:29rhettingersetassignee: Mariatta

messages: + msg286390
nosy: + Mariatta, rhettinger
2017-01-11 01:45:23ethan.furmansetnosy: + ethan.furman

messages: + msg285179
stage: needs patch
2017-01-11 01:31:34Thane Brimhallcreate