B
Brian Blais
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/Cookbook/Python/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.amount - 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.updateAmount(0) # Build progress bar string
def updateAmount(self, 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.amount - self.min)
percentDone = (diffFromMin / float(self.span)) * 100.0
percentDone = round(percentDone)
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(numHashes))
# 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.progBar) / 2) - len(str(percentDone))
percentString = str(percentDone) + "%"
# slice the percentage into the bar
self.progBar = self.progBar[0ercentPlace] + percentString +
self.progBar[percentPlace+len(percentString):]
def __str__(self):
return str(self.progBar)
if __name__ == "__main__":
import time
prog = progressBar(0, 100, 77)
for i in xrange(101):
prog.updateAmount(i)
print prog, "\r",
time.sleep(.05)
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/Cookbook/Python/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.amount - 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.updateAmount(0) # Build progress bar string
def updateAmount(self, 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.amount - self.min)
percentDone = (diffFromMin / float(self.span)) * 100.0
percentDone = round(percentDone)
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(numHashes))
# 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.progBar) / 2) - len(str(percentDone))
percentString = str(percentDone) + "%"
# slice the percentage into the bar
self.progBar = self.progBar[0ercentPlace] + percentString +
self.progBar[percentPlace+len(percentString):]
def __str__(self):
return str(self.progBar)
if __name__ == "__main__":
import time
prog = progressBar(0, 100, 77)
for i in xrange(101):
prog.updateAmount(i)
print prog, "\r",
time.sleep(.05)