Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add profile contextmanager #5

Merged
merged 1 commit into from

2 participants

@cyberdelia

Add profile contextmanager for easier use :

import statprof
import test.pystone

with statprof.profile():
     test.pystone.pystones()
@bos bos merged commit d017747 into bos:master
@bos
Owner

Applied, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 10, 2012
  1. @cyberdelia

    add profile contextmanager

    cyberdelia authored
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 3 deletions.
  1. +8 −0 README.rst
  2. +13 −3 statprof.py
View
8 README.rst
@@ -34,6 +34,14 @@ It's easy to get started with ``statprof``: ::
statprof.stop()
statprof.display()
+Or with a contextmanager : ::
+
+ import statprof
+
+ with statprof.profile():
+ my_questionable_function()
+
+
For more comprehensive help, run ``pydoc statprof``.
View
16 statprof.py
@@ -100,15 +100,15 @@
significantly off if other threads' work patterns are not similar to the
main thread's work patterns.
"""
-
-
from __future__ import division
import os
import signal
+from contextlib import contextmanager
+
-__all__ = ['start', 'stop', 'reset', 'display']
+__all__ = ['start', 'stop', 'reset', 'display', 'profile']
###########################################################################
@@ -273,6 +273,16 @@ def reset(frequency=None):
state.reset(frequency)
+@contextmanager
+def profile():
+ start()
+ try:
+ yield
+ finally:
+ stop()
+ display()
+
+
###########################################################################
## Reporting API
Something went wrong with that request. Please try again.