40
39
return "%i %s" (self.current, self.units)
42
def show_progress(pi, prog):
43
pi.update(prog.units, prog.current, prog.total)
41
class ProgressBar(object):
46
def __call__(self, progress):
47
if self.start is None:
48
self.start = datetime.datetime.now()
49
progress_bar(progress, start_time=self.start)
51
def divide_timedelta(delt, divisor):
52
"""Divides a timedelta object"""
53
return datetime.timedelta(float(delt.days)/divisor,
54
float(delt.seconds)/divisor,
55
float(delt.microseconds)/divisor)
60
return str(datetime.timedelta(delt.days, delt.seconds))
62
def get_eta(start_time, progress, enough_samples=20):
63
if start_time is None or progress.current == 0:
65
elif progress.current < enough_samples:
67
elapsed = datetime.datetime.now() - start_time
68
total_duration = divide_timedelta((elapsed) * long(progress.total),
70
if elapsed < total_duration:
71
eta = total_duration - elapsed
73
eta = total_duration - total_duration
76
def progress_bar(progress, start_time=None):
77
eta = get_eta(start_time, progress)
78
if start_time is not None:
79
eta_str = " "+str_tdelta(eta)
83
fmt = " %i of %i %s (%.1f%%)"
84
f = fmt % (progress.total, progress.total, progress.units, 100.0)
87
if start_time is not None:
89
markers = int (float(cols) * progress.current / progress.total)
90
txt = fmt % (progress.current, progress.total, progress.units,
92
sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt,
45
95
def clear_progress_bar():
46
96
sys.stderr.write('\r%s\r' % (' '*79))
98
def spinner_str(progress, show_text=False):
100
Produces the string for a textual "spinner" progress indicator
101
:param progress: an object represinting current progress
102
:param show_text: If true, show progress text as well
103
:return: The spinner string
105
>>> spinner_str(Progress("baloons", 0))
107
>>> spinner_str(Progress("baloons", 5))
109
>>> spinner_str(Progress("baloons", 6), show_text=True)
112
positions = ('|', '/', '-', '\\')
113
text = positions[progress.current % 4]
115
text+=" %i %s" % (progress.current, progress.units)
118
def spinner(progress, show_text=False, output=sys.stderr):
120
Update a spinner progress indicator on an output
121
:param progress: The progress to display
122
:param show_text: If true, show text as well as spinner
123
:param output: The output to write to
125
>>> spinner(Progress("baloons", 6), show_text=True, output=sys.stdout)
128
output.write('\r%s' % spinner_str(progress, show_text))
132
result = doctest.testmod()
135
print "All tests passed"
137
print "No tests to run"
139
def rewriting_supported(out=sys.stdout):
140
return hasattr(out, 'isatty') and out.isatty()
142
if __name__ == "__main__":