diff --git a/timer.py b/timer.py index dc090a1..7581531 100644 --- a/timer.py +++ b/timer.py @@ -16,6 +16,7 @@ Simple timer utility. This module is part of wpylib project. """ +import sys import time class timer: @@ -38,6 +39,12 @@ class block_timer(object): with block_timer() as t: + + Fancy options are available to record the timing result, specified when + initializing the new object: + + * report=block_timer.writeout_file :: writes out the timing result to a file + * report=block_timer.writeout_dict :: saves the timing result to a dict-like element """ @staticmethod def writeout_file(out): @@ -49,21 +56,23 @@ class block_timer(object): return wrt_dict #return lambda tm: rec.__setitem__(key, tm) - @staticmethod - def bt_file(fobj): + @classmethod + def bt_file(cls, fobj): + """An object factory to report the timing to a text file. + """ if isinstance(fobj, basestring): from wpylib.iofmt.text_output import text_output out = text_output(fobj) else: out = fobj - return block_timer(report=block_timer.writeout_file(out)) + return cls(report=block_timer.writeout_file(out)) - @staticmethod - def bt_dict(rec, key): - return block_timer(report=block_timer.writeout_dict(rec, key)) + @classmethod + def bt_dict(cls, rec, key): + return cls(report=block_timer.writeout_dict(rec, key)) def __init__(self, report=None): - if report == None: report = block_timer.writeout_file(sys.stdout) + if report is None: report = block_timer.writeout_file(sys.stdout) self.report = report def __enter__(self):