Add timing utility.
[tabakrolletjie.git] / tabakrolletjie / utils.py
1 """ Tabakutilities. """
2
3 import functools
4 import time
5
6 from .constants import DEBUG
7
8
9 def debug_timer(label):
10     """ A decorator for printing how long a function took if debug is true.
11     """
12     def debug_inner(f):
13         @functools.wraps(f)
14         def wrapper(*args, **kw):
15             start_time = time.time()
16             try:
17                 return f(*args, **kw)
18             finally:
19                 duration = time.time() - start_time
20                 if DEBUG:
21                     print "%s [%g seconds]" % (label, duration)
22         return wrapper
23     return debug_inner