Messages with a time stamp

Michiel Overtoom motoom at xs4all.nl
Sun May 3 05:10:40 EDT 2015


On May 3, 2015, at 10:22, Cecil Westerhof wrote:

> For testing I want my messages time stamped like:

For progress reporting, I often use the module below (eta.py), which also gives a projected time of completion:

import datetime, time, sys
etastart = 0

def eta(done, total, s, reportinterval=100, datetimeformat="%d %b %H:%M:%S"):
    global etastart
    if done == 0:
        etastart = datetime.datetime.now()
    if not done % reportinterval:
        noww = datetime.datetime.now()
        prtnow = noww.strftime(datetimeformat)
        if done:
            if not isinstance(etastart, datetime.datetime): raise RuntimeError("You should call eta() at least once with done=0")
            elapsed = noww - etastart
            secsperitem = float(elapsed.seconds) / done    
            totalsecs = secsperitem * total
            eta = etastart + datetime.timedelta(0, totalsecs)
            prteta = eta.strftime(datetimeformat)
            msg = "now %s, eta %s (%d/%d) %s" % (prtnow, prteta, done, total, s)
        else:
            msg = "now %s (%d/%d) %s" % (prtnow, done, total, s)
        sys.stderr.write(msg + "\n")
        
if __name__ == "__main__":
    for i in range(10):
        eta(i, 10, "idling for ten seconds", 1, "%H:%M:%S")
        time.sleep(1)



-- 
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes




More information about the Python-list mailing list