1

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

1
  • Are you saying the value of count is not changing?
    – S.Lott
    Aug 21, 2009 at 15:06

4 Answers 4

5

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))
1
  • 1
    Please let me know if you find any bug. I'll be more than happy to fix it. Aug 21, 2009 at 15:33
3

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
4
  • 1
    Users won't be happy with "Processing 0 of 100" :-) Aug 21, 2009 at 15:10
  • 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.
    – FrancisV
    Aug 21, 2009 at 15:11
  • 1
    It isn't "stuck" @Francis. Please provide the rest of the code in your question.
    – S.Lott
    Aug 21, 2009 at 15:17
  • Thanks @S.Lott, I noticed that too when I paused the output of "print 'Processing..."
    – FrancisV
    Aug 21, 2009 at 15:22
2

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.

2
1

(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)

1
  • Thanks for the feedback. I understand your point re: #2. Indeed, the loop doesn't process until 'maxrecords' -- it's always less than 1
    – FrancisV
    Aug 21, 2009 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.