Skip to content

Commit 44bd46b

Browse files
committed
added an absolute ETA to the widgets (estimates the time when it will finish)
1 parent b4c76b7 commit 44bd46b

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

examples.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, \
1010
FileTransferSpeed, FormatLabel, Percentage, \
1111
ProgressBar, ReverseBar, RotatingMarker, \
12-
SimpleProgress, Timer, AdaptiveETA, AdaptiveTransferSpeed
12+
SimpleProgress, Timer, AdaptiveETA, AbsoluteETA, AdaptiveTransferSpeed
1313

1414
examples = []
1515

@@ -353,6 +353,16 @@ def example28():
353353
pass
354354

355355

356+
@example
357+
def example29():
358+
widgets = ['Test: ', Percentage(), ' | ', ETA(), ' | ', AbsoluteETA()]
359+
pbar = ProgressBar(widgets=widgets, maxval=500).start()
360+
for i in range(500):
361+
time.sleep(0.01)
362+
pbar.update(i+1)
363+
pbar.finish()
364+
365+
356366
def test():
357367
for example in examples:
358368
example()

progressbar/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
Timer,
4848
ETA,
4949
AdaptiveETA,
50+
AbsoluteETA,
5051
FileTransferSpeed,
5152
AdaptiveTransferSpeed,
5253
AnimatedMarker,
@@ -74,6 +75,7 @@
7475
'Timer',
7576
'ETA',
7677
'AdaptiveETA',
78+
'AbsoluteETA',
7779
'FileTransferSpeed',
7880
'AdaptiveTransferSpeed',
7981
'AnimatedMarker',
@@ -89,4 +91,3 @@
8991
'format_updatable',
9092
'RotatingMarker',
9193
]
92-

progressbar/widgets.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ def __call__(self, progress, data):
170170
data['total_seconds_elapsed'])
171171

172172

173+
class AbsoluteETA(Timer):
174+
'''Widget which attempts to estimate the absolute time of arrival.'''
175+
176+
def _eta(self, progress, data, value, elapsed):
177+
"""Update the widget to show the ETA or total time when finished."""
178+
now = datetime.datetime.now()
179+
if value == progress.min_value:
180+
return 'Estimated finish time: ----/--/-- --:--:--'
181+
elif progress.end_time:
182+
return 'Finished at: %s' % self._format(datetime.datetime.now())
183+
else:
184+
eta = elapsed * progress.max_value / value - elapsed
185+
eta_abs = now + datetime.timedelta(seconds=eta)
186+
return 'Estimated finish time: %s' % self._format(eta_abs)
187+
188+
def _format(self, t):
189+
return t.strftime("%Y/%m/%d %H:%M:%S")
190+
191+
def __call__(self, progress, data):
192+
'''Updates the widget to show the ETA or total time when finished.'''
193+
return self._eta(progress, data, data['value'],
194+
data['total_seconds_elapsed'])
195+
173196
class AdaptiveETA(ETA, SamplesMixin):
174197
'''WidgetBase which attempts to estimate the time of arrival.
175198
@@ -427,4 +450,3 @@ def update(self, progress, width):
427450
rpad, lpad = lpad, rpad
428451

429452
return '%s%s%s%s%s' % (left, lpad, marker, rpad, right)
430-

0 commit comments

Comments
 (0)