20
20
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25
+class Progress(object):
25
26
+ def __init__(self, units, current, total=None):
26
27
+ self.units = units
27
28
+ self.current = current
28
29
+ self.total = total
30
+ if self.total is not None:
31
+ self.percent = 100.0 * current / 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)
33
37
+ def __str__(self):
34
38
+ if self.total is not None:
38
42
+ return "%i %s" (self.current, self.units)
41
+def progress_bar(progress):
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)
42
86
+ fmt = " %i of %i %s (%.1f%%)"
43
87
+ f = fmt % (progress.total, progress.total, progress.units, 100.0)
90
+ if start_time is not None:
91
+ cols -= len(eta_str)
46
92
+ markers = int (float(cols) * progress.current / progress.total)
47
93
+ txt = fmt % (progress.current, progress.total, progress.units,
48
94
+ progress.percent)
49
+ sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
95
+ sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt,
51
98
+def clear_progress_bar():
52
99
+ sys.stderr.write('\r%s\r' % (' '*79))