Skip to content

Instantly share code, notes, and snippets.

@cluther
Last active February 5, 2016 20:13
Show Gist options
  • Save cluther/6799127 to your computer and use it in GitHub Desktop.
Save cluther/6799127 to your computer and use it in GitHub Desktop.
Python Progress Logger
INFO:root:progress: 8 of 1000, elapsed=0:00:01, remaining=0:02:09
INFO:root:progress: 16 of 1000, elapsed=0:00:02, remaining=0:02:08
INFO:root:progress: 24 of 1000, elapsed=0:00:03, remaining=0:02:07
INFO:root:progress: 32 of 1000, elapsed=0:00:04, remaining=0:02:06
INFO:root:progress: 40 of 1000, elapsed=0:00:05, remaining=0:02:05
INFO:root:progress: 48 of 1000, elapsed=0:00:06, remaining=0:02:04
INFO:root:progress: 56 of 1000, elapsed=0:00:07, remaining=0:02:03
INFO:root:progress: 64 of 1000, elapsed=0:00:08, remaining=0:02:02
INFO:root:progress: 72 of 1000, elapsed=0:00:09, remaining=0:02:01
INFO:root:progress: 80 of 1000, elapsed=0:00:10, remaining=0:02:00
import logging
import time
import progresslog
def main():
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
things = range(1000)
progress = progresslog.ProgressLogger(
log, prefix='progress', total=len(things), interval=1)
for thing in things:
time.sleep(.13)
progress.increment()
if __name__ == '__main__':
main()
import datetime
import logging
class ProgressLogger(object):
def __init__(
self,
logger,
level=logging.INFO,
prefix='',
total=None,
interval=60):
self.logger = logger
self.level = level
self.prefix = prefix
self.total = total
self.interval = datetime.timedelta(seconds=interval)
self.pos = 0
self.start_time = datetime.datetime.now()
self.last_time = self.start_time
def increment(self, by=1):
self.pos += by
now = datetime.datetime.now()
if now - self.last_time >= self.interval:
self.last_time = now
progress = '{} of {}'.format(
self.pos,
self.total if self.total else '?')
elapsed = now - self.start_time
if self.total:
per = elapsed / self.pos
remaining = per * (self.total - self.pos)
msg = '{}, elapsed={}, remaining={}'.format(
progress,
str(elapsed).split('.', 1)[0],
str(remaining).split('.', 1)[0])
else:
msg = '{}, elapsed={}'.format(
progress,
str(elapsed).split('.', 1)[0])
if self.prefix:
msg = '{}: {}'.format(self.prefix, msg)
self.logger.log(self.level, msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment