@contextmanager
def log_timing(logger, task, level=logging.INFO, final_msg=None, callbacks=None):
 start = datetime.datetime.now()
 if (task is not None):
  logger.log(level, (str(task) + '...'))
 (yield)
 end = datetime.datetime.now()
 delta = (end - start)
 total = total_seconds(delta)
 if (total < 60):
  delta_str = ('%f seconds' % total)
 else:
  delta_str = str(delta)
 if (final_msg is None):
  logger.log(level, (str(task) + (' done. Time elapsed: %s' % delta_str)))
 else:
  logger.log(level, ' '.join((final_msg, delta_str)))
 if (callbacks is not None):
  for callback in callbacks:
   callback(total)

@ contextmanager def log_timing ( logger , task , level = logging . INFO , final_msg = None , callbacks = None ) : start = datetime . datetime . now ( ) if ( task is not None ) : logger . log ( level , ( str ( task ) + '...' ) ) ( yield ) end = datetime . datetime . now ( ) delta = ( end - start ) total = total_seconds ( delta ) if ( total < 60 ) : delta_str = ( '%f seconds' % total ) else : delta_str = str ( delta ) if ( final_msg is None ) : logger . log ( level , ( str ( task ) + ( ' done. Time elapsed: %s' % delta_str ) ) ) else : logger . log ( level , ' ' . join ( ( final_msg , delta_str ) ) ) if ( callbacks is not None ) : for callback in callbacks : callback ( total )
context manager that logs the start/end of an operation .