-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitbar.py
85 lines (67 loc) · 2.16 KB
/
waitbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from sys import stdout
from time import time
class Waitbar(object):
def __init__( self, amount=0., barwidth=50, totalwidth=75, title=None,
showETA=False):
self.showETA = showETA
self._bw = barwidth
self._tw = totalwidth
self._stime = time()
self._bar = ''
self._eta = ''
if title is not None:
print title
self.update(amount)
def update(self,newamount):
self._done = newamount
n = int(round(self._done * self._bw))
# bar = u'\u25AE'*n
bar = '#' * n
pad = '-' * (self._bw - n)
self._bar = '[' + bar + pad + '] %2i%%' % (self._done * 100.)
if self.showETA:
if self._done == 0:
self._eta = ' ETA: ?'
else:
dt = time() - self._stime
eta = (dt / self._done) * (1. - self._done)
self._eta = ' ETA: %s' %s2h(eta)
self.display()
def display(self):
stdout.write('\r' + ' '*self._tw)
if self._done == 1.:
ftime = s2h(time() - self._stime)
stdout.write('\r>>> Completed: %s\n' %ftime)
else:
nspace = max(
(0,self._tw - (len(self._bar) + len(self._eta)))
)
stdout.write('\r' + self._bar + self._eta + ' '*nspace)
stdout.flush()
class ElapsedTimer(object):
def __init__(self, title=None, width=75):
self.title = title
self._npad = width - len(title)
def start(self):
self._stime = time()
stdout.write('\r' + self.title + ' ' * self._npad)
stdout.flush()
def done(self):
elapsed = s2h(time() - self._stime)
donestr = 'done: ' + elapsed + '\n'
stdout.write('\r' + self.title + ' '* self._npad + donestr)
stdout.flush()
def __enter__(self):
self.start()
def __exit__(self, *args):
self.done()
def s2h(ss):
mm,ss = divmod(ss,60)
hh,mm = divmod(mm,60)
dd,hh = divmod(hh,24)
tstr = "%02i:%05.2f" %(mm,ss)
if hh > 0:
tstr = ("%02i:" %hh) + tstr
if dd > 0:
tstr = ("%id " %dd) + tstr
return tstr