1 import sys
2 import time
3
5 hours = int(seconds/3600.0)
6 seconds -= hours*3600.0
7 minutes = int(seconds/60.0)
8 seconds -= minutes*60.0
9 seconds = int(seconds)
10 return hours,minutes,seconds
11
13 '''
14 Modified from: http://code.activestate.com/recipes/168639-progress-bar-class/
15 '''
16 - def __init__(self, minValue = 0, maxValue = 10, totalWidth=60):
17 self.start_time = time.time()
18 self.progBar = "[]"
19 self.min = minValue
20 self.max = maxValue
21 self.span = maxValue - minValue
22 if self.span == 0:
23 self.span = 1
24 self.width = totalWidth
25 self.amount = 0
26 self.updateAmount(0)
27 self.last_show = 0.0
28
30 '''
31 Update the progress bar. By default this will increment the amount by 1.
32
33 After calling this make sure to call show() to display the progress bar.
34 '''
35 if newAmount == None: newAmount = self.amount + 1
36 if newAmount < self.min: newAmount = self.min
37 if newAmount > self.max: newAmount = self.max
38 self.amount = newAmount
39
40
41 diffFromMin = float(self.amount - self.min)
42 fracDone = diffFromMin / float(self.span)
43 percentDone = (diffFromMin / float(self.span)) * 100.0
44 percentDone = round(percentDone)
45 percentDone = int(percentDone)
46
47
48 allFull = self.width - 2
49 numHashes = (percentDone / 100.0) * allFull
50 numHashes = int(round(numHashes))
51
52
53 self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
54
55
56 percentPlace = (len(self.progBar) / 2) - len(str(percentDone))
57 percentString = str(percentDone) + "%"
58
59
60 self.progBar = self.progBar[0:percentPlace-1] + " " + percentString + " " + self.progBar[percentPlace+len(percentString)+1:]
61
62
63 current_time = time.time()
64 elapsed_time = current_time - self.start_time
65 if fracDone > 0.01:
66 remaining_time = elapsed_time * (1-fracDone) / fracDone
67 hours,minutes,seconds = secToHMS(remaining_time)
68 self.time_string = "%02dh %02dm %02ds"%(hours,minutes,seconds)
69 else:
70 self.time_string = "--h --m --s"
71
72
74 return str(self.progBar)
75
77 '''Displays the progress bar on stdout.'''
78 current_time = time.time()
79 if current_time - self.last_show > 1.0:
80 sys.stdout.write(self.__str__()+" " + self.time_string + "\r")
81 sys.stdout.flush()
82 self.last_show = current_time
83
85 '''Conducts Cleanup and new line.'''
86 self.updateAmount(self.max)
87 elapsed_time = time.time() - self.start_time
88 hours,minutes,seconds = secToHMS(elapsed_time)
89
90 sys.stdout.write(self.__str__()+" %02dh %02dm %02ds"%(hours,minutes,seconds) + "\n")
91 sys.stdout.flush()
92
93 if __name__ == '__main__':
94 prog = ProgressBar(0, 100, 60)
95 print dir(prog)
96 for i in xrange(101):
97 prog.updateAmount(i)
98 prog.show()
99 time.sleep(.05)
100 prog.finish()
101