strange error I can't figure out...



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[0:percentPlace] + 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)


.



Relevant Pages

  • Re: Threads and Progress Bar
    ... The is the progress bar code. ... if newAmount < self.min: newAmount = self.min ... # Create two Queues for the requests and responses ... # Queue up the requests. ...
    (comp.lang.python)
  • Re: strange error I cant figure out...
    ... get rid of the comma at the end of that last print statement. ... The program is a progress bar code I got at the Python Cookbook: ... if newAmount < self.min: newAmount = self.min ... percentDone = round ...
    (comp.lang.python)
  • Re: visual basic 6 progress bar?
    ... What I'm trying to do is display a progress bar for 1 Do Until loop within my program. ... When I put the progress bar code on the out side of the loop, it ran the whole Do Until loop first and then displayed the progress bar filling up afterward: ...
    (microsoft.public.vb.controls)