* Minor bug fixes.

Wirawan Purwanto 9 years ago
parent 39dddbd51d
commit 56b5d9ed05
  1. 23

@ -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
def writeout_file(out):
@ -49,21 +56,23 @@ class block_timer(object):
return wrt_dict
#return lambda tm: rec.__setitem__(key, tm)
def bt_file(fobj):
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)
out = fobj
return block_timer(report=block_timer.writeout_file(out))
return cls(report=block_timer.writeout_file(out))
def bt_dict(rec, key):
return block_timer(report=block_timer.writeout_dict(rec, key))
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):