Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-69990: Make Profile.print_stats support sorting by multiple values #104590

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

Merged
merged 24 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
212b668
Make Profile.print_stats support sorting by mutiple values
furkanonder May 17, 2023
da1b596
📜🤖 Added by blurb_it.
blurb-it[bot] May 17, 2023
1ac24af
Merge branch 'main' into issue-69990
furkanonder May 17, 2023
9f18231
Merge branch 'main' into issue-69990
furkanonder May 25, 2023
f771358
update documentation according to the new feature
furkanonder Jan 28, 2024
1d8c4e4
fix code format
furkanonder Jan 28, 2024
82facb0
update the news entry
furkanonder Jan 28, 2024
80bb65b
fix code format
furkanonder Jan 29, 2024
0253706
update documentation according to the new feature
furkanonder Jan 29, 2024
58c7e35
update the news entry
furkanonder Jan 29, 2024
1905e3f
fix merge conflict
furkanonder Feb 10, 2024
fb4b8cb
use contextlib's redirect_stdout
furkanonder Feb 10, 2024
66e1e82
use isinstance check for the sorting tuple
furkanonder Feb 10, 2024
dfa668d
update documentation of print_stats method
furkanonder Feb 10, 2024
96b1bdd
remove whitespace
furkanonder Feb 10, 2024
e1a70e0
Merge branch 'main' into issue-69990
furkanonder Feb 10, 2024
f6b235f
Update Doc/library/profile.rst
furkanonder Feb 10, 2024
bb0507b
Update Doc/library/profile.rst
furkanonder Feb 10, 2024
4cccb55
Merge branch 'main' into issue-69990
furkanonder Feb 10, 2024
b083716
fix the syntax of the documentation grammar in print_stats
furkanonder Feb 10, 2024
7fdbfab
clarify valid key types in print_stats method
furkanonder Feb 10, 2024
49ca018
clarify valid key types in print_stats method
furkanonder Feb 13, 2024
4ea5e4c
update versionadded section
furkanonder Feb 13, 2024
9978802
Update Doc/library/profile.rst
serhiy-storchaka Feb 16, 2024
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
7 changes: 7 additions & 0 deletions Doc/library/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ functions:
Create a :class:`~pstats.Stats` object based on the current
profile and print the results to stdout.

The *sort* parameter specifies the sorting order of the displayed
statistics. It accepts a single key or a tuple of keys to enable
multi-level sorting, as in :func:`Stats.sort_stats <pstats.Stats.sort_stats>`.

.. versionadded:: 3.13
:meth:`~Profile.print_stats` now accepts a tuple of keys.

.. method:: dump_stats(filename)

Write the results of the current profile to *filename*.
Expand Down
4 changes: 3 additions & 1 deletion Lib/cProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class Profile(_lsprof.Profiler):

def print_stats(self, sort=-1):
import pstats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
if not isinstance(sort, tuple):
sort = (sort,)
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()

def dump_stats(self, file):
import marshal
Expand Down
5 changes: 3 additions & 2 deletions Lib/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ def simulate_cmd_complete(self):

def print_stats(self, sort=-1):
import pstats
pstats.Stats(self).strip_dirs().sort_stats(sort). \
print_stats()
if not isinstance(sort, tuple):
sort = (sort,)
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()

def dump_stats(self, file):
with open(file, 'wb') as f:
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from difflib import unified_diff
from io import StringIO
from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
from contextlib import contextmanager
from contextlib import contextmanager, redirect_stdout

import profile
from test.profilee import testfunc, timer
Expand Down Expand Up @@ -92,6 +92,11 @@ def test_run(self):
self.profilermodule.run("int('1')", filename=TESTFN)
self.assertTrue(os.path.exists(TESTFN))

def test_run_with_sort_by_values(self):
with redirect_stdout(StringIO()) as f:
self.profilermodule.run("int('1')", sort=('tottime', 'stdname'))
self.assertIn("Ordered by: internal time, standard name", f.getvalue())

def test_runctx(self):
with silent():
self.profilermodule.runctx("testfunc()", globals(), locals())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`Profile.print_stats` has been improved to accept multiple sort arguments. Patched by Chiu-Hsiang Hsu and Furkan Onder.
Loading