Commit 530a0878 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: Simplify util.timed API - hours/minutes/seconds auto-calculated

parent a621e5ed
......@@ -105,8 +105,7 @@ def main(argv=None):
else: workdir = args.work_dir
try:
with util.timed(
None, log, fmt='Total time: %i minutes, %i seconds (%+iMB)'):
with util.timed(None, log, fmt='Total time: %s (%+iMB)'):
dtable, unknowns, unprocessed, drop = doImport(args, pool, mgr)
......@@ -152,7 +151,7 @@ def doImport(args, pool, mgr):
rules specified on them.
"""
with util.timed('Table import', log, minutes=False):
with util.timed('Table import', log):
vartable, proctable, cattable, unknowns, unprocessed = \
loadtables.loadTables(
args.infile,
......
......@@ -166,7 +166,7 @@ def parseMatlabRange(r):
@contextlib.contextmanager
def timed(op=None, logger=None, lvl=None, fmt=None, minutes=True):
def timed(op=None, logger=None, lvl=None, fmt=None, minutes=None):
"""Context manager which times a section of code, and prints a log
message afterwards.
......@@ -178,26 +178,24 @@ def timed(op=None, logger=None, lvl=None, fmt=None, minutes=True):
:arg fmt: Custom message. If not provided, a default message is used.
Must be a ``'%'``-style format string which accepts two
(``minutes is False``) or three (``minutes is True``)
parameters, the elapsed minutes and seconds, and the
memory usage.
parameters: the elapsed time (``%s``), and the memory usage
(``%i``)..
:arg minutes: If ``True`` (the default), elapsed minutes and seconds are
printed. Otherwise elapsed seconds are printed.
:arg minutes: Deprecated, has no effect.
"""
if minutes is not None:
warnings.warn('The "minutes" argument is deprecated and has '
'no effect', DeprecationWarning, stacklevel=2)
if fmt is None:
fmt = '[{}] completed in %s (%+iMB)'.format(op)
if logger is None:
logger = log
if lvl is None:
lvl = logging.INFO
if fmt is None:
if minutes:
fmt = '[{}] completed in %i minutes, %i seconds (%+iMB)'.format(op)
else:
fmt = '[{}] completed in %i seconds (%+iMB)'.format(op)
if op is not None:
logger.log(lvl, 'Running task [%s]', op)
......@@ -217,16 +215,23 @@ def timed(op=None, logger=None, lvl=None, fmt=None, minutes=True):
endtime = time.time()
hours = int( (endtime - starttime) / 3600)
minutes = int(((endtime - starttime) % 3600) / 60)
seconds = int(((endtime - starttime) % 3600) % 60)
timestr = '{:d} seconds'.format(seconds)
if minutes > 0: timestr = '{} minutes, {}'.format(minutes, time)
if hours > 0: timestr = '{} hours, {}' .format(hours, time)
if resource is not None:
endmem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
else:
endmem = 0
secs = endtime - starttime
mbytes = (endmem - startmem) / memdenom
if minutes: logger.log(lvl, fmt, secs / 60.0, secs % 60.0, mbytes)
else: logger.log(lvl, fmt, secs, mbytes)
if minutes: logger.log(lvl, fmt, timestr, mbytes)
else: logger.log(lvl, fmt, timestr, mbytes)
def deprecated(message):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment