Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more kwargs to cProfile's print_stats #73424

Open
ThaneBrimhall mannequin opened this issue Jan 11, 2017 · 1 comment
Open

Add more kwargs to cProfile's print_stats #73424

ThaneBrimhall mannequin opened this issue Jan 11, 2017 · 1 comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ThaneBrimhall
Copy link
Mannequin

ThaneBrimhall mannequin commented Jan 11, 2017

BPO 29238
Nosy @ethanfurman

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2017-01-11.03:00:31.266>
labels = ['3.7', 'type-feature', 'library']
title = "Add more kwargs to cProfile's print_stats"
updated_at = <Date 2017-01-11.03:53:36.414>
user = 'https://bugs.python.org/ThaneBrimhall'

bugs.python.org fields:

activity = <Date 2017-01-11.03:53:36.414>
actor = 'ethan.furman'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2017-01-11.03:00:31.266>
creator = 'Thane Brimhall'
dependencies = []
files = []
hgrepos = []
issue_num = 29238
keywords = []
message_count = 1.0
messages = ['285183']
nosy_count = 2.0
nosy_names = ['ethan.furman', 'Thane Brimhall']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue29238'
versions = ['Python 3.7']

@ThaneBrimhall
Copy link
Mannequin Author

ThaneBrimhall mannequin commented Jan 11, 2017

Using the pstats class to print off profiler results is helpful when you want to filter, order, and limit the number of returned lines. I'd rather see the most commonly-used subset of pstats functionality included in the profiler's print_stats implementation directly.

# The current way
pstats.Stats(profiler).strip_dirs().sort_stats('cumulative').reverse_order().print_stats(10)
    # Proposed way
    profiler.print_stats(strip_dirs=False, sort='cumulative', reverse=True, limit=10)

Currently only the sort kwarg is available. To me this implies that some level of control was originally intended to be available in the print_stats method anyway. I also feel like the proposed API is more readable and explicit.

Note that for complex situations you'd still need to use the pstats class directly, eg. substituting a different stream implementation or filtering/sorting by multiple values.

This would be a backwards-compatible patch and would be implemented something like this:

    def print_stats(self, sort=-1, limit=None, strip_dirs=True, reverse=True):
        import pstats
        stats = pstats.Stats(self)
        if strip_dirs:
            stats = stats.strip_dirs()
        stats = stats.sort_stats(sort)
        if reverse:
            stats = stats.reverse_order()
        stats.print_stats(limit)

@ThaneBrimhall ThaneBrimhall mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jan 11, 2017
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

0 participants