<div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">2017-01-10 8:57 GMT-08:00 Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="m_5412359806943567025gmail-">On 01/10/2017 08:36 AM, Thane Brimhall wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Does anyone have thoughts on this topic? I assume the silence is because<br>
this suggestion is too trivial to matter.<br>
</blockquote>
<br></span>
Sometimes it's just a matter of timing. :)<span class="m_5412359806943567025gmail-"><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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>
</blockquote>
<br></span>
Absolutely.<span class="m_5412359806943567025gmail-"><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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>
</blockquote>
<br></span>
I don't have much experience with cProfile, but this seems reasonable.<span class="m_5412359806943567025gmail-"><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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>
</blockquote>
<br></span>
Absolutely! :)<span class="m_5412359806943567025gmail-"><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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.So<wbr>rt.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>
</blockquote>
<br></span>
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></blockquote></div></div><div>These changes may not even require C code, since (contrary to its name) cProfile actually is implemented partly in Python. For example, the context manager change could be made simply by adding __enter__ and __exit__ to the cProfile.Profile class.</div><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
--<br>
~Ethan~<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
</blockquote></span></div><br></div></div>
</div><br></div>