bpo-29237: Create enum for pstats sorting options#5103
Conversation
ethanfurman
left a comment
There was a problem hiding this comment.
Enum member names need to be in all caps: SortKey.TIME.
Where several values mean the same thing, have one canonical member, and the rest duplicates, e.g.:
CUMULATIVE = 'cumulative'
CUMTIME = 'cumulative'
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I would change the import to
from pstats import Stats, SortKey
There was a problem hiding this comment.
The pstats module is all about stats processing, so there is no need to hide SortKey inside the Stats class.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yes there is a need to also show an example with a string form, but not all code examples need both forms.
There was a problem hiding this comment.
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'
|
Thank you for your review. Regarding the request to change CUMTIME to 'cumulative', wouldn't this break the existing code that uses 'cumtime' as the sort criteria? I guess I can internally map the string 'cumtime' to 'cumulative' to allow it. If you have other suggestion, let me know. |
|
On 01/05/2018 09:40 AM, mwidjaja wrote:
Thank you for your review. Regarding the request to change CUMTIME to 'cumulative', wouldn't this break the existing
code that uses 'cumtime' as the sort criteria? I guess I can internally map the string 'cumtime' to 'cumulative' to
allow it. If you have other suggestion, let me know.
There is a separate code path for the string arguments, so any string-based code would continue to work.
|
|
@ethanfurman: |
91d8117 to
b098e7d
Compare
There was a problem hiding this comment.
I still think showing both ways in these examples is not useful and could create confusion.
It would be enough to have one example here and the changes that you already did in the method docs later in this file.
There was a problem hiding this comment.
Ok. unless there're objections from anyone else, I'll remove them and just keep the one in the method doc.
|
@ethanfurman, would you be able to give me some hints on your comments about separate code path. Thank you. |
2a79106 to
9413145
Compare
|
I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @ethanfurman: please review the changes made to this pull request. |
|
@merwok, @ethanfurman : If there's anything else I need to do, let me know, otherwise this PR is ready for another review. Thank you |
There was a problem hiding this comment.
If people follow the code examples here, SortKey will be undefined. It needs to be imported.
There was a problem hiding this comment.
Since we still have to support string arguments, and the SortKey members are also strings, I would just leave the original code alone here.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
With the new SortKey enum, these three lines are no longer needed.
There was a problem hiding this comment.
When dealing with Enums, member is a better name than arg.
There was a problem hiding this comment.
I don't understand -- 'file', 'filename', and 'module' should all create the same sort -- what ambiguity is there?
There was a problem hiding this comment.
While what you're saying is essentially correct, however as it is currently implemented (without any of my changes), we're allowed to abbreviate the string argument into the sort_stats as long as the abbreviation is unambiguous. For example, sort_stats would work if either 'filename' or 'filena' string is passed in. But, if string 'file' is passed, then sort_stats can't differentiate between 'filename' or 'file' and sort_stats will fail with KeyError exception. We should probably remove 'file'. I can do that, if you agree. I'm guessing that no existing code is using 'file' argument at this time.
There was a problem hiding this comment.
file and filename map to the same stat, so if we removed file and somebody passed in file it should still work.
Remove "file" and write a test to make sure it still works as intended.
There was a problem hiding this comment.
Ok. Will make another pass to remove "file".
There was a problem hiding this comment.
Since this is the string test, we should be iterating through all the possible strings (no longer a given with the new SortKey enum). Listing them all is probably the best way to go:
for arg in ('calls', 'ncalls', 'cumtime', 'cumulative', ... ):
ethanfurman
left a comment
There was a problem hiding this comment.
Some changes with regards to how the SortKey enum is created, and the cascading changes from that.
My apologies for my initial misunderstanding of the problem.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
4e26d4f to
385b383
Compare
|
I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @ethanfurman: please review the changes made to this pull request. |
There was a problem hiding this comment.
Move SortKey.FILENAME up one line to the 'filename' entry, change the 'module' entry to N/A, and we're done! Thank you!
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
385b383 to
ad7a395
Compare
|
Thank you @ethanfurman @merwok for the review comments. I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @ethanfurman: please review the changes made to this pull request. |
https://bugs.python.org/issue29237
https://bugs.python.org/issue29237