-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.py
More file actions
executable file
·50 lines (44 loc) · 1.39 KB
/
progress.py
File metadata and controls
executable file
·50 lines (44 loc) · 1.39 KB
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
#!/usr/bin/python
from time import sleep, time
class ProgressMonitor(object):
def __init__(self, total=100, print_interval=1, msg=None):
self.tstart = time()
self.total = total
self.print_interval = print_interval
self.progress = 0
self.pending = 0
self.msg = msg
self.print_progress()
def increment(self, amount=1):
self.pending += amount
if ((100*self.pending/self.total)
>= self.print_interval):
self.progress += self.pending
self.pending = 0
self.print_progress()
def print_progress(self):
elapsed = time() - self.tstart
pc = (100*self.progress/self.total)
if pc > 0:
remaining = elapsed*(100. - pc)/pc
rm_str = format_time(remaining)
else:
rm_str = '???'
if self.msg:
print '%s: %3d%% (~%s remaining)' % (self.msg, pc, rm_str)
else:
print '%3d%% (~%s remaining)' % (pc, rm_str)
def format_time(time):
if time >= 3600:
return '%.1f hours' % (time/3600)
elif time >= 60:
return '%.1f minutes' % (time/60)
else:
return '%.1f seconds' % time
if __name__ == '__main__':
totaltime = 10.0
total = 1000
prog = ProgressMonitor(total, 5, "Testing")
for i in xrange(total):
sleep(totaltime/total)
prog.increment()