Hello,
I have an odd kind of Heisenbug in what looks like a pretty simple program. The
program is a progress bar code I got at the Python Cookbook:
http://aspn.activestate.com/ASPN/Coo.../Recipe/168639
(including the code below)
If you uncomment the one print statement I added in the progressBar class, you get
the error:
File "test_progress2 .py", line 19
diffFromMin = float(self.amou nt - self.min)
^
SyntaxError: invalid syntax
yet, without the print statement, it works fine. what am I overlooking here?
thanks,
bb
#------------------------------------------------------------------------
class progressBar:
def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
self.progBar = "[]" # This holds the progress bar string
self.min = minValue
self.max = maxValue
self.span = maxValue - minValue
self.width = totalWidth
self.amount = 0 # When amount == max, we are 100% done
self.updateAmou nt(0) # Build progress bar string
def updateAmount(se lf, newAmount = 0):
if newAmount < self.min: newAmount = self.min
if newAmount > self.max: newAmount = self.max
self.amount = newAmount
# print "hello" #<-------------- uncomment line to break
# Figure out the new percent done, round to an integer
diffFromMin = float(self.amou nt - self.min)
percentDone = (diffFromMin / float(self.span )) * 100.0
percentDone = round(percentDo ne)
percentDone = int(percentDone )
# Figure out how many hash bars the percentage should be
allFull = self.width - 2
numHashes = (percentDone / 100.0) * allFull
numHashes = int(round(numHa shes))
# build a progress bar with hashes and spaces
self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
# figure out where to put the percentage, roughly centered
percentPlace = (len(self.progB ar) / 2) - len(str(percent Done))
percentString = str(percentDone ) + "%"
# slice the percentage into the bar
self.progBar = self.progBar[0:percentPlace] + percentString +
self.progBar[percentPlace+le n(percentString ):]
def __str__(self):
return str(self.progBa r)
if __name__ == "__main__":
import time
prog = progressBar(0, 100, 77)
for i in xrange(101):
prog.updateAmou nt(i)
print prog, "\r",
time.sleep(.05)