66
class Progress(object):
67
"""Description of progress through a task.
69
Basically just a fancy tuple holding:
72
noun string describing what is being traversed, e.g.
76
how many objects have been processed so far
79
total number of objects to process, if known.
82
def __init__(self, units, current, total=None):
84
self.current = current
87
def _get_percent(self):
88
if self.total is not None and self.current is not None:
89
return 100.0 * self.current / self.total
91
percent = property(_get_percent)
94
if self.total is not None:
95
return "%i of %i %s %.1f%%" % (self.current, self.total, self.units,
98
return "%i %s" (self.current, self.units)
102
66
class ProgressBar(object):
103
def __init__(self, to_file=sys.stderr):
67
"""Progress bar display object.
69
Several options are available to control the display. These can
70
be passed as parameters to the constructor or assigned at any time:
73
Show percentage complete.
75
Show rotating baton. This ticks over on every update even
76
if the values don't change.
78
Show predicted time-to-completion.
82
Show numerical counts.
84
The output file should be in line-buffered or unbuffered mode.
104
95
object.__init__(self)
96
self.start_time = None
106
97
self.to_file = to_file
107
98
self.suppressed = not _supports_progress(self.to_file)
110
def __call__(self, progress):
111
if self.start is None:
112
self.start = datetime.datetime.now()
113
if not self.suppressed:
114
draw_progress_bar(progress, start_time=self.start,
115
to_file=self.to_file)
101
self.show_pct = show_pct
102
self.show_spinner = show_spinner
103
self.show_eta = show_eta
104
self.show_bar = show_bar
105
self.show_count = show_count
109
self.update(self.last_msg, self.last_cnt, self.last_total)
113
def update(self, msg, current_cnt, total_cnt=None):
114
"""Update and redraw progress bar."""
115
if self.start_time is None:
116
self.start_time = datetime.datetime.now()
118
# save these for the tick() function
120
self.last_cnt = current_cnt
121
self.last_total = total_cnt
129
assert current_cnt <= total_cnt
131
assert current_cnt >= 0
133
if self.show_eta and self.start_time and total_cnt:
134
eta = get_eta(self.start_time, current_cnt, total_cnt)
135
eta_str = " " + str_tdelta(eta)
139
if self.show_spinner:
140
spin_str = self.SPIN_CHARS[self.spin_pos % 4] + ' '
144
# always update this; it's also used for the bar
147
if self.show_pct and total_cnt and current_cnt:
148
pct = 100.0 * current_cnt / total_cnt
149
pct_str = ' (%5.1f%%)' % pct
153
if not self.show_count:
155
elif current_cnt is None:
157
elif total_cnt is None:
158
count_str = ' %i' % (current_cnt)
160
# make both fields the same size
161
t = '%i' % (total_cnt)
162
c = '%*i' % (len(t), current_cnt)
163
count_str = ' ' + c + '/' + t
166
# progress bar, if present, soaks up all remaining space
167
cols = width - 1 - len(msg) - len(spin_str) - len(pct_str) \
168
- len(eta_str) - len(count_str) - 3
171
# number of markers highlighted in bar
172
markers = int(round(float(cols) * current_cnt / total_cnt))
173
bar_str = '[' + ('=' * markers).ljust(cols) + '] '
175
# don't know total, so can't show completion.
176
# so just show an expanded spinning thingy
177
m = self.spin_pos % cols
181
bar_str = '[' + ms + '] '
185
m = spin_str + bar_str + msg + count_str + pct_str + eta_str
187
assert len(m) < width
188
self.to_file.write('\r' + m.ljust(width - 1))
189
#self.to_file.flush()
118
if not self.suppressed:
119
clear_progress_bar(self.to_file)
196
self.to_file.write('\r%s\r' % (' ' * (_width() - 1)))
197
#self.to_file.flush()
132
210
return str(datetime.timedelta(delt.days, delt.seconds))
135
def get_eta(start_time, progress, enough_samples=20):
136
if start_time is None or progress.current == 0:
213
def get_eta(start_time, current, total, enough_samples=20):
214
if start_time is None or current == 0:
138
elif progress.current < enough_samples:
216
elif current < enough_samples:
217
# FIXME: No good if it's not a count
140
219
elapsed = datetime.datetime.now() - start_time
141
total_duration = divide_timedelta((elapsed) * long(progress.total),
220
total_duration = divide_timedelta(elapsed * long(total),
143
222
if elapsed < total_duration:
144
223
eta = total_duration - elapsed
150
def draw_progress_bar(progress, start_time=None, to_file=sys.stderr):
151
eta = get_eta(start_time, progress)
152
if start_time is not None:
153
eta_str = " "+str_tdelta(eta)
157
fmt = " %i of %i %s (%.1f%%)"
158
f = fmt % (progress.total, progress.total, progress.units, 100.0)
159
cols = _width() - 3 - len(f)
160
if start_time is not None:
162
markers = int(round(float(cols) * progress.current / progress.total))
163
txt = fmt % (progress.current, progress.total, progress.units,
165
to_file.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt,
168
def clear_progress_bar(to_file=sys.stderr):
169
to_file.write('\r%s\r' % (' '*79))
172
def spinner_str(progress, show_text=False):
174
Produces the string for a textual "spinner" progress indicator
175
:param progress: an object represinting current progress
176
:param show_text: If true, show progress text as well
177
:return: The spinner string
179
>>> spinner_str(Progress("baloons", 0))
181
>>> spinner_str(Progress("baloons", 5))
183
>>> spinner_str(Progress("baloons", 6), show_text=True)
186
positions = ('|', '/', '-', '\\')
187
text = positions[progress.current % 4]
189
text+=" %i %s" % (progress.current, progress.units)
193
def spinner(progress, show_text=False, output=sys.stderr):
195
Update a spinner progress indicator on an output
196
:param progress: The progress to display
197
:param show_text: If true, show text as well as spinner
198
:param output: The output to write to
200
>>> spinner(Progress("baloons", 6), show_text=True, output=sys.stdout)
203
output.write('\r%s' % spinner_str(progress, show_text))
208
231
result = doctest.testmod()