20
20
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
24
+class Progress(object):
26
25
+ def __init__(self, units, current, total=None):
27
26
+ self.units = units
28
27
+ self.current = current
29
28
+ self.total = total
31
+ def _get_percent(self):
32
+ if self.total is not None and self.current is not None:
33
+ return 100.0 * self.current / self.total
35
+ percent = property(_get_percent)
30
+ if self.total is not None:
31
+ self.percent = 100.0 * current / total
37
33
+ def __str__(self):
38
34
+ if self.total is not None:
42
38
+ return "%i %s" (self.current, self.units)
44
+class ProgressBar(object):
47
+ object.__init__(self)
49
+ def __call__(self, progress):
50
+ if self.start is None:
51
+ self.start = datetime.datetime.now()
52
+ progress_bar(progress, start_time=self.start)
54
+def divide_timedelta(delt, divisor):
55
+ """Divides a timedelta object"""
56
+ return datetime.timedelta(float(delt.days)/divisor,
57
+ float(delt.seconds)/divisor,
58
+ float(delt.microseconds)/divisor)
60
+def str_tdelta(delt):
63
+ return str(datetime.timedelta(delt.days, delt.seconds))
65
+def get_eta(start_time, progress, enough_samples=20):
66
+ if start_time is None or progress.current == 0:
68
+ elif progress.current < enough_samples:
70
+ elapsed = datetime.datetime.now() - start_time
71
+ total_duration = divide_timedelta((elapsed) * long(progress.total),
73
+ if elapsed < total_duration:
74
+ eta = total_duration - elapsed
76
+ eta = total_duration - total_duration
79
+def progress_bar(progress, start_time=None):
80
+ eta = get_eta(start_time, progress)
81
+ if start_time is not None:
82
+ eta_str = " "+str_tdelta(eta)
41
+def progress_bar(progress):
86
42
+ fmt = " %i of %i %s (%.1f%%)"
87
43
+ f = fmt % (progress.total, progress.total, progress.units, 100.0)
90
+ if start_time is not None:
91
+ cols -= len(eta_str)
92
46
+ markers = int (float(cols) * progress.current / progress.total)
93
47
+ txt = fmt % (progress.current, progress.total, progress.units,
94
48
+ progress.percent)
95
+ sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt,
49
+ sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
98
51
+def clear_progress_bar():
99
52
+ sys.stderr.write('\r%s\r' % (' '*79))