Use some elif.
[tabakrolletjie.git] / tabakrolletjie / utils.py
index bc5aef2572cf42e3c7ba20146fe2bd80f45974ad..0775204345e60a94146164feb2966bdecb886642 100644 (file)
@@ -6,7 +6,7 @@ import time
 from .constants import DEBUG
 
 
-def debug_timer(label):
+def debug_timer(label, debug=False):
     """ A decorator for printing how long a function took if debug is true.
     """
     def debug_inner(f):
@@ -17,7 +17,38 @@ def debug_timer(label):
                 return f(*args, **kw)
             finally:
                 duration = time.time() - start_time
-                if DEBUG:
+                if DEBUG or debug:
                     print "%s [%g seconds]" % (label, duration)
         return wrapper
     return debug_inner
+
+
+class DetailedTimer(object):
+    """ A detailed timer for overly complex functions.
+    """
+    def __init__(self, title, debug=False):
+        if DEBUG or debug:
+            self.title = title
+            self.times = []
+        else:
+            # fast funtions with the correct signature
+            self.start = int
+            self.show = str
+            self.lap = str
+            self.end = int
+
+    def start(self):
+        print "---- %s ----" % self.title
+        self.times.append(time.time())
+
+    def lap(self, label):
+        now = time.time()
+        print "  %s: %s" % (label, now - self.times[-1])
+        self.times.append(now)
+
+    def show(self, text):
+        print "  %s" % text
+
+    def end(self):
+        print "  %s: %s" % ("total", time.time() - self.times[0])
+        print "---- ----"