Reputation: 1749
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
Upvotes: 1
Views: 1314
Reputation: 83032
(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)
Upvotes: 1
Reputation: 115011
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))
Upvotes: 5
Reputation: 54935
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.
Upvotes: 2
Reputation: 600026
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
Upvotes: 3