<div dir="ltr">Thanks for getting back to me on this! Yes timing can be a big factor. :) Turns out this gave me opportunity to look a little further back in the archives and someone suggested a very similar API change in November, so maybe more people than just me would want a feature like this.<div><br></div><div>Good call on putting the print_stats outside of the context block. Kinda meta to profile the profiler...</div><div><br></div><div>If the next step is to open an issue on the tracker, I'll do that. I can work on a Python proof-of-concept to attach there as well.</div><div><br></div><div>Again, thanks for your feedback!</div><div><br></div><div>/Thane<br><br>On Tuesday, January 10, 2017 at 9:57:54 AM UTC-7, Ethan Furman wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 01/10/2017 08:36 AM, Thane Brimhall wrote:
<br>
<br>> Does anyone have thoughts on this topic? I assume the silence is because
<br>> this suggestion is too trivial to matter.
<br>
<br>Sometimes it's just a matter of timing. :)
<br>
<br>> I use cProfile a lot, and would like to suggest three backwards-compatible
<br>> improvements to the API.
<br>>
<br>> 1: When using cProfile on a specific piece of code I often use the
<br>> enable() and disable() methods. It occurred to me that this would
<br>> be an obvious place to use a context manager.
<br>
<br>Absolutely.
<br>
<br>> 2: Enhance the `print_stats` method on Profile to accept more options
<br>> currently available only through the pstats.Stats class. For example,
<br>> strip_dirs could be a boolean argument, and limit could accept an int.
<br>> This would reduce the number of cases you'd need to use the more complex
<br>> API.
<br>
<br>I don't have much experience with cProfile, but this seems reasonable.
<br>
<br>> 3: I often forget which string keys are available for sorting. It would
<br>> be nice to add an enum for these so a user could have their linter and
<br>> IDE check that value pre-runtime. Since it would subclass `str` and
<br>> `Enum` it would still work with all currently existing code.
<br>
<br>Absolutely! :)
<br>
<br>> The current documentation contains the following code:
<br>>
<br>> import cProfile, pstats, io
<br>> pr = cProfile.Profile()
<br>> pr.enable()
<br>> # ... do something ...
<br>> pr.disable()
<br>> s = io.StringIO()
<br>> sortby = 'cumulative'
<br>> ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
<br>> ps.print_stats()
<br>> print(s.getvalue())
<br>>
<br>> While the code below doesn't exactly match the functionality above (eg. not
<br>> using StringIO), I envision the context manager working like this, along
<br>> with some adjustments on how to get the stats from the profiler:
<br>>
<br>> import cProfile, pstats
<br>> with cProfile.Profile() as pr:
<br>> # ... do something ...
<br>> pr.print_stats(sort=pstats.<wbr>Sort.cumulative, limit=10, strip_dirs=True)
<br>>
<br>> As you can see, the code is shorter and somewhat more self-documenting. The
<br>> best thing about these suggestions is that as far as I can tell they would
<br>> be backwards-compatible API additions.
<br>
<br>The `pr.print_stats... line should not be inside the `with` block unless you want to profile that part as well.
<br>
<br>These suggestions seem fairly uncontroversial. Have you opened an issue on the issue tracker?
<br>
<br>The fun part of the patch will be the C code, but a Python proof-of-concept would be useful.
<br>
<br>--
<br>~Ethan~
<br>______________________________<wbr>_________________
<br>Python-ideas mailing list
<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="OreOSkAGFAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a>
<br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a>
<br></blockquote></div></div>