← Back to DAGDA | Support | Source: https://github.com/python/cpython/pull/5103/commits/5553944a2988516c9c5dd454f309f041abfab0d1
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change enums member to uppercase and other review comments
  • Loading branch information
mwidjaja committed Jan 26, 2018
commit 5553944a2988516c9c5dd454f309f041abfab0d1
9 changes: 2 additions & 7 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,12 @@ entries according to the standard module/line/name string that is printed. The
:meth:`~pstats.Stats.print_stats` method printed out all the statistics. You
might try the following sort calls::

p.sort_stats('name')
p.print_stats()

p.sort_stats(SortKey.name)
p.print_stats()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello and thanks for the PR!

Here I would recomment keeping only the example with the enum, since it’s the way that we want to recommend.

To match the import style (see import pstats in the previous code block), the example would need to be p.sort_stats(pstats.SortKey.NAME) (assuming the upper-case fix asked by the other reviewer is done).

I wonder if the enum should be defined on the Stats class, to make code shorter: p.sort_stats(p.SortKey.NAME). Maybe worth asking on the ticket to get other people’s opinion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the import to

from pstats import Stats, SortKey

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pstats module is all about stats processing, so there is no need to hide SortKey inside the Stats class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comment. Re: 'Here I would recomment keeping only the example with the enum, since it’s the way that we want to recommend.' I could do that, but I have also received comment about having both example as the string argument is still valid. Showing only the enum would make the doc incomplete.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there is a need to also show an example with a string form, but not all code examples need both forms.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mwidjaja

When you make the duplicate SortKeys actually duplicates, please put the better name first -- something like:

class SortKey(str, Enum):
    calls = 'calls'
    ncalls = 'calls'
    cumulative = 'cumulative'
    cumtime = 'cumulative'
    module = 'module'
    file = 'module'
    filename = 'module'
    line = 'line'
    name = 'name'
    nfl = 'nfl'
    pcalls = 'pcalls'
    stdname = 'stdname'
    time = 'time'
    tottime = 'time'


The first call will actually sort the list by function name, and the second call
will print out the statistics. The next two calls perform the same function, but
the call to :meth:`~pstats.Stats.sort_stats` uses SortKey enum argument which is
less error prone than the string version. The following are some interesting calls
to experiment with::
will print out the statistics. The following are some interesting calls to
experiment with::

p.sort_stats('cumulative').print_stats(10)
p.sort_stats(SortKey.cumulative).print_stats(10)
Expand Down
60 changes: 30 additions & 30 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@


class SortKey(str, Enum):
calls = 'calls'
ncalls = 'ncalls'
cumtime = 'cumtime'
cumulative = 'cumulative'
file = 'file'
filename = 'filename'
line = 'line'
module = 'module'
name = 'name'
nfl = 'nfl'
pcalls = 'pcalls'
stdname = 'stdname'
time = 'time'
tottime = 'tottime'
CALLS = 'calls'
NCALLS = 'ncalls'
CUMULATIVE = 'cumulative'
CUMTIME = 'cumtime'
MODULE = 'module'
FILE = 'file'
FILENAME = 'filename'
LINE = 'line'
NAME = 'name'
NFL = 'nfl'
PCALLS = 'pcalls'
STDNAME = 'stdname'
TIME = 'time'
TOTTIME = 'tottime'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial understanding of this was flawed. Since we need to support the old string values we can't just have CUMTIME and CUMULATIVE map to "cumulative" because then "cumtime" is gone (unless we do extra work, which I see you did).

What we need here is a MultiValueEnum, so SortKey.CUMULATIVE maps to both "cumulative" and "cumtime".

The Enum should look like this:

class SortKey(str, Enum):

    CALLS = 'calls', 'ncalls'
    CUMULATIVE = 'cumulative', "cumtime"
    MODULE = 'module', 'filename', 'file'
    LINE = 'line'
    NAME = 'name'
    NFL = 'nfl'
    PCALLS = 'pcalls'
    STDNAME = 'stdname'
    TIME = 'time', 'tottime'

    def __new__(cls, *values):
        obj = object.__new__(cls)
        # first value is canonical value
        obj._value_ = values[0]
        for other_value in values[1:]:
            cls._value2member_map_[other_value] = obj
        obj._all_values = values
        return obj

Operations like SortKey('file') will return SortKey.MODULE. The downside is that SortKey.FILE is undefined, but I think that is an acceptable trade-off since we are still supporting the original string values, and those values can be used to get a correct SortKey member.


class Stats:
Expand All @@ -72,8 +72,8 @@ class Stats:
arbitrary number of quoted strings or SortKey enum to select the sort
order.

For example sort_stats('time', 'name') or sort_stats(SortKey.time,
SortKey.name) sorts on the major key of 'internal function time', and on
For example sort_stats('time', 'name') or sort_stats(SortKey.TIME,
SortKey.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.

Expand Down Expand Up @@ -177,20 +177,20 @@ def dump_stats(self, filename):
# list the tuple indices and directions for sorting,
# along with some printable description
sort_arg_dict_default = {
SortKey.calls : (((1,-1), ), "call count"),
SortKey.ncalls : (((1,-1), ), "call count"),
SortKey.cumtime : (((3,-1), ), "cumulative time"),
SortKey.cumulative: (((3,-1), ), "cumulative time"),
SortKey.file : (((4, 1), ), "file name"),
SortKey.filename : (((4, 1), ), "file name"),
SortKey.line : (((5, 1), ), "line number"),
SortKey.module : (((4, 1), ), "file name"),
SortKey.name : (((6, 1), ), "function name"),
SortKey.nfl : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
SortKey.pcalls : (((0,-1), ), "primitive call count"),
SortKey.stdname : (((7, 1), ), "standard name"),
SortKey.time : (((2,-1), ), "internal time"),
SortKey.tottime : (((2,-1), ), "internal time"),
SortKey.CALLS : (((1,-1), ), "call count"),
SortKey.NCALLS : (((1,-1), ), "call count"),
SortKey.CUMTIME : (((3,-1), ), "cumulative time"),
SortKey.CUMULATIVE: (((3,-1), ), "cumulative time"),
SortKey.FILE : (((4, 1), ), "file name"),
SortKey.FILENAME : (((4, 1), ), "file name"),
SortKey.LINE : (((5, 1), ), "line number"),
SortKey.MODULE : (((4, 1), ), "file name"),
SortKey.NAME : (((6, 1), ), "function name"),
SortKey.NFL : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
SortKey.PCALLS : (((0,-1), ), "primitive call count"),
SortKey.STDNAME : (((7, 1), ), "standard name"),
SortKey.TIME : (((2,-1), ), "internal time"),
SortKey.TOTTIME : (((2,-1), ), "internal time"),
}

def get_sort_arg_defs(self):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def test_sort_stats_enum(self):
def test_sort_starts_mix(self):
self.assertRaises(TypeError, self.stats.sort_stats,
'calls',
SortKey.time)
SortKey.TIME)
self.assertRaises(TypeError, self.stats.sort_stats,
SortKey.time,
SortKey.TIME,
'calls')


Expand Down