|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
How do I do a progress indicator in Python?
What's the model for creating a Python program that runs in a shell, that updates a line on the screen to indicate progress?
If I keep using print, then it will write a new line from the shell. What I want is something that looks like this: Code:
[ ] [= ] [== ] [=== ] [==== ] ... but so that each line overwrites the previous one? Thanks, Antun |
|
#2
|
|||
|
|||
|
You can print "\r" which is a carriage return and will take you back to the beginning of the line.
|
|
#3
|
||||
|
||||
|
what you're probabl looking for is curses although i'm not sure that is really a shell script since on windows we open a curses GUI window.
I'm sure strikes idea does work although i cant for teh life of me work out how to change prints end char right now so i'm just using sys.stdout to output the values. Anyway a few small examples... Code:
import sys, time
for each in range(5):
sys.stdout.write('[' + '=' * each + ']\r')
time.sleep(0.5)
or you can clear the crean (on windows) using the cls command (dont know about on *nix) Code:
import os, time
for each in range(5):
os.system('cls')
print '[' + '=' * each + ']'
time.sleep(0.5)
Mark. |
|
#4
|
|||
|
|||
|
On *nix it's "clear"
![]() |
|
#5
|
|||
|
|||
|
Thanks guys! I like the first method that netytan posted the best (using carriage returns). Clearing the screen is not practical, because it means I'd lose anything else in the terminal.
-Antun |
|
#6
|
||||
|
||||
|
No prob Kar, thanks for the info X
if i'm ever get into *nix it might come in handy.Mark. |
|
#7
|
||||
|
||||
|
You can also use \b to move backwards one character at a time.
Code:
import sys
sys.stdout.write("Hello World")
sys.stdout.write("\b\b\b\b\bUniverse")
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#8
|
||||
|
||||
|
Here's a twirly bar progress indicator:
Code:
class TwirlyBar:
def __init__(self):
import sys
self.__state = 0
self.__bar = ('|', '/', '-', '\\\')
def ShowProgress(self):
sys.stdout.write('\b' + self.__bar[self.__state])
self.__state = self.__state + 1
if self.__state > 3: self.__state = 0
if __name__ == "__main__":
import sys, time
sys.stdout.write ("Processing ... ")
tb = TwirlyBar()
for x in range(10):
tb.ShowProgress()
time.sleep(0.5)
sys.stdout.write("\bdone\n")
Last edited by Scorpions4ever : January 27th, 2004 at 01:54 PM. |
|
#9
|
||||
|
||||
|
Way, *nix style! Nice work scorpy, pretty sure you can do that more easily though
will give it a try...Oh, the reason for this, you have an error or line 5, need to escape that backslash. Mark. |
|
#10
|
||||
|
||||
|
>> Oh, the reason for this, you have an error or line 5, need to escape that backslash.
I did escape the backslash in my code when I cut and pasted it here. The forum software is displaying only one backslash, but when you edit the code you can see the doubled backslash. The solution was to edit my thread and triple the backslashes, so that the forum software would then show a doubled backslash. .>> Way, *nix style! Actually I tested this on a Windows box. Dunno if this will work on a *nix box unless I set the terminal to raw mode . |
|
#11
|
|||
|
|||
|
I think he meant it was *nix style.. you see that on *nix boxes often
![]() btw, I tried it on OS X, and it didn't work ![]() Last edited by XxChris : January 27th, 2004 at 02:34 PM. |
|
#12
|
||||
|
||||
|
>> btw, I tried it on OS X, and it didn't work
Probably because you have to set output to be unbuffered. I couldn't get my code (or netytan's code either) to work, unless i started python with the -u option on my *nix box . Try doing it like this:python -u foo.py and see if it works. |
|
#13
|
||||
|
||||
|
Ok, here's my version of scorpies twirl class... only as a function and 4 x smaller (arpox)
Code:
#!/usr/bin/env python
import sys, time
def twirl(loops, chars = ('|', '/', '-', '\\\')):
for each in range(loops):
for char in chars:
sys.stdout.write(char + '\b')
time.sleep(0.2)
if __name__ == '__main__':
sys.stdout.write('Processing ... ')
twirl(10)
Mark. Last edited by netytan : January 27th, 2004 at 05:22 PM. |
|
#14
|