strange error I can't figure out...

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[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)
 
J

John Zenger

It works fine for me. You must be having an indentation problem.

Also, get rid of the comma at the end of that last print statement.
 
B

Brian Blais

John said:
It works fine for me. You must be having an indentation problem.

Also, get rid of the comma at the end of that last print statement.

talk about a Heisenbug...it disappeared after reading this post...without changing
anything. :)

It reminds me of the many times that IT problems disappear then the tech looks at it.


thanks,


bb
 
B

Brian Beck

John said:
Also, get rid of the comma at the end of that last print statement.

This would break the progress bar functionality I think, which is meant
to update a single line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top