views:

440

answers:

4

I have a script that uses a simple while loop to display a progress bar but it doesn't seem to be working as I expected:

count = 1
maxrecords = len(international)
p = ProgressBar("Blue")
t = time
while count < maxrecords:
    print 'Processing %d of %d' % (count, maxrecords)
    percent = float(count) / float(maxrecords) * 100
    p.render(int(percent))
    t.sleep(0.5)
    count += 1

It appears to be looping at "p.render..." and does not go back to "print 'Processing %d of %d...'".

UPDATE: My apologies. It appears that ProgressBar.render() removes the output of "print 'Processing..." when it renders the progress bar. The progress bar is from http://nadiana.com/animated-terminal-progress-bar-in-python

+3  A: 

That's not the way to write a loop in Python.

maxrecords = len(international)
p = ProgressBar("Blue")
for count in range(1, maxrecords):
    print 'Processing %d of %d' % (count, maxrecords)
    percent = float(count) / float(maxrecords) * 100
    p.render(int(percent))
    time.sleep(0.5)

If you actually want to do something with the record, rather than just render the bar, you would do this:

maxrecords = len(international)
for count, record in enumerate(international):
    print 'Processing %d of %d' % (count, maxrecords)
    percent = float(count) / float(maxrecords) * 100
    p.render(int(percent))
    process_record(record)   # or whatever the function is
Daniel Roseman
Users won't be happy with "Processing 0 of 100" :-)
John Machin
Thanks for the quick response. I tried your first suggestion, it still doesn't go back to the "print 'Processing..." line. It's stuck at the "p.render..." line.
Francis
It isn't "stuck" @Francis. Please provide the rest of the code in your question.
S.Lott
Thanks @S.Lott, I noticed that too when I paused the output of "print 'Processing..."
Francis
+2  A: 

What is the implementation for ProgressBar.render()? I assume that it is outputting terminal control characters that move the cursor so that previous output is overwritten. This can create the false impression that the control flow isn't working as it should be.

Ants Aasma
Here's the ouput: Processing 1 of 179090 % 0 % 0 % 0 % 0 % 0 % 0 %
Francis
The ProgressBar is from http://nadiana.com/animated-terminal-progress-bar-in-python
Francis
+5  A: 

I see you are using the ProgressBar implementation on my website. If you want to print a message you can use the message argument in render

p.render(percent, message='Processing %d of %d' % (count, maxrecords))
Nadia Alramli
Thanks for the ProgressBar ;)
Francis
Please let me know if you find any bug. I'll be more than happy to fix it.
Nadia Alramli
Works perfectly. Thanks for the tip!
Francis
+1  A: 

(1) [not part of the problem, but ...] t = time followed much later by t.sleep(0.5) would be a source of annoyance to anyone seeing the bare t and having to read backwards to find what it is.

(2) [not part of the problem, but ...] count can never enter the loop with the same value as maxrecords. E.g. if maxrecords is 10, the code in the loop is eexcuted only 9 times.

(3) There is nothing in the code that you showed that would support the idea that it is "looping at p.render()" -- unless the render method itself loops if its arg is zero, which will be the case if maxrecords is 17909. Try replacing the p.render(....) temporarily with (say)

print "pretend-render: pct =", int(percent)

John Machin
Thanks for the feedback. I understand your point re: #2. Indeed, the loop doesn't process until 'maxrecords' -- it's always less than 1
Francis