Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'sys': 0.07; '"__main__":': 0.09; '%s"': 0.09; '__name__': 0.09; 'msg': 0.09; 'def': 0.12; '"\\n")': 0.16; '%s,': 0.16; 'from:addr:xs4all.nl': 0.16; 'received:194.109': 0.16; 'received:194.109.24': 0.16; 'received:194.109.24.22': 0.16; 'received:xs4all.nl': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; '"you': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'raise': 0.29; 'gives': 0.31; 'faster,': 0.31; 'run': 0.32; 'subject:time': 0.33; 'skip:d 20': 0.34; 'subject:with': 0.35; "can't": 0.35; 'done,': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'header:Message-Id:1': 0.63; 'received:194': 0.64; 'total': 0.65; 'computers': 0.72; 'received:nl': 0.74; 'done:': 0.84; 'total,': 0.84 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1085) Subject: Re: Messages with a time stamp From: Michiel Overtoom In-Reply-To: <87r3qykr39.fsf@Equus.decebal.nl> Date: Sun, 3 May 2015 11:10:40 +0200 Content-Transfer-Encoding: quoted-printable References: <87r3qykr39.fsf@Equus.decebal.nl> To: Python X-Mailer: Apple Mail (2.1085) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430644308 news.xs4all.nl 2926 [2001:888:2000:d::a6]:41543 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89841 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 =3D 0 def eta(done, total, s, reportinterval=3D100, datetimeformat=3D"%d %b = %H:%M:%S"): global etastart if done =3D=3D 0: etastart =3D datetime.datetime.now() if not done % reportinterval: noww =3D datetime.datetime.now() prtnow =3D noww.strftime(datetimeformat) if done: if not isinstance(etastart, datetime.datetime): raise = RuntimeError("You should call eta() at least once with done=3D0") elapsed =3D noww - etastart secsperitem =3D float(elapsed.seconds) / done =20 totalsecs =3D secsperitem * total eta =3D etastart + datetime.timedelta(0, totalsecs) prteta =3D eta.strftime(datetimeformat) msg =3D "now %s, eta %s (%d/%d) %s" % (prtnow, prteta, done, = total, s) else: msg =3D "now %s (%d/%d) %s" % (prtnow, done, total, s) sys.stderr.write(msg + "\n") =20 if __name__ =3D=3D "__main__": for i in range(10): eta(i, 10, "idling for ten seconds", 1, "%H:%M:%S") time.sleep(1) --=20 "You can't actually make computers run faster, you can only make them do = less." - RiderOfGiraffes