Skip to content

Commit

Permalink
Also allow profiling timer to work as decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
vidartf committed Apr 21, 2017
1 parent 4e8b96a commit a5f5423
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions nbdime/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import time
import contextlib
from tabulate import tabulate
from functools import wraps


def _sort_time(value):
Expand Down Expand Up @@ -68,6 +69,18 @@ def time(self, key):
else:
self.map[key] = dict(time=secs, calls=1)

def profile(self, key=None):
def decorator(function):
nonlocal key
if key is None:
key = function.__name__ or 'unknown'
@wraps(function)
def inner(*args, **kwargs):
with self.time(key):
return function(*args, **kwargs)
return inner
return decorator

@contextlib.contextmanager
def enable(self):
old = self.enabled
Expand Down

0 comments on commit a5f5423

Please sign in to comment.