progbar.py
Go to the documentation of this file.
1 """
2  progbar.py : a nice progress bar with timer
3 
4  Original author: J. Wolcott <jwolcott@fnal.gov>
5  Date: January 2018
6 """
7 
8 import datetime
9 import sys
10 import time
11 
13  def __init__(self, total, bar_width=25, update_interval=1):
14  self.n = 0
15  self.total = total
16  self.bar_width = bar_width
17 
18  self.last_update = 0
20 
21  self.update_interval = update_interval # in seconds
22 
23  self.start = None
24 
25  def Start(self):
26  self.start = time.time()
27 
28  if self.last_update_time == 0:
29  self.Update(0)
30 
31  def Update(self, n, force=False):
32  if self.start is None:
33  self.Start()
34 
35  interval = float(time.time() - self.last_update_time)
36 
37  if force or interval > self.update_interval:
38 
39  complete_frac = float(n)/self.total
40  num_chars = max(int(complete_frac * self.bar_width) - 1, 0)
41  print "\r",
42  print "%d/%d [%s]" % (n, self.total, "-" * num_chars + ">" + " " * (self.bar_width - num_chars - 1) ),
43 
44  if self.last_update_time > 0:
45  since_start = float(time.time() - self.start)
46  # this rate calculation is a weighted average of the time it's taken so far
47  # and the interval since the last update, weighted by how complete things are now
48  estimated_rate = complete_frac * ( (n-self.last_update) / interval + (self.total-n)/ since_start )
49  if n < self.total:
50  print "[Estimated time remaining: %s]" % datetime.timedelta(seconds = round((self.total - n)/estimated_rate) ),
51  else:
52  # extra whitespace to overwrite the previous display
53  print "[%s]" % datetime.timedelta(seconds=round(since_start)) + " " * 35
54 
55  sys.stdout.flush()
56 
57  self.last_update = n
58  self.last_update_time = time.time()
59 
60  def Finish(self):
61  self.Update(n=self.total, force=True)
def __init__(self, total, bar_width=25, update_interval=1)
Definition: progbar.py:13
def Start(self)
Definition: progbar.py:25
fvar< T > round(const fvar< T > &x)
Definition: round.hpp:23
Definition: novas.h:112
def Update(self, n, force=False)
Definition: progbar.py:31
def Finish(self)
Definition: progbar.py:60
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68