Re: [Tutor] carriage return on windows
On Mon, 31 Jan 2005 18:01:59 -0600, Victor Rex [EMAIL PROTECTED] wrote: I played around with this output issue and I love the way it works. Now, how do you do this in *nix? I tried the same approach and I get a blank line for 5 seconds (or whatever number of cycles you have on your example) and the a final line with the last value of the iterable. Do you happen to know how this in done? you might want to flush stdout after printing to it. print will cares for this only when not using that trailing comma. flush means to write immedatly instead to wait for a fair amount of data. import sys,time for i in range(8): sys.stdout.write( step: %s\r % i) # or: print step: %s\r % i, sys.stdout.flush() time.sleep(.5) There's still another possibilty using ansi control sequences. The dirty way is to print (without trailing comma) and go back to previous line: import time for i in range(8): print step: %s\033[A % i # print subsystem has done stdout.flush time.sleep(.5) It's dirty cause the next line was already entered and thus is written (as an empty line) and content of older steps will stay an screen, when subsequents lines aren't long enough to overwrite them. Which also applies to \r: import sys,time for i in range(8,0,-1): # printing 8**8, ..., 0**0 on line. Forget to overwrite sys.stdout.write( step: %s\r % (i**i)) sys.stdout.flush() time.sleep(.5) Fixes are to add whitespace to the line. Stepping back with \033[D fixes the empty newline issue (which is most often not worth the effort): import sys,time for i in range(8,0,-1): # fixing output length to 30 output = %-30s % step: %s % (i**i) length = len(output) sys.stdout.write(output) # print output, would be different, because of implizit spaces sys.stdout.write(\033[D* (length)) sys.stdout.flush() time.sleep(.5) regards Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
Michael Janssen wrote: On Mon, 31 Jan 2005 18:01:59 -0600, Victor Rex [EMAIL PROTECTED] wrote: I played around with this output issue and I love the way it works. Now, how do you do this in *nix? I tried the same approach and I get a blank line for 5 seconds (or whatever number of cycles you have on your example) and the a final line with the last value of the iterable. Do you happen to know how this in done? you might want to flush stdout after printing to it. print will cares for this only when not using that trailing comma. flush means to write immedatly instead to wait for a fair amount of data. import sys,time for i in range(8): sys.stdout.write( step: %s\r % i) # or: print step: %s\r % i, sys.stdout.flush() time.sleep(.5) There's still another possibilty using ansi control sequences. The dirty way is to print (without trailing comma) and go back to previous line: import time for i in range(8): print step: %s\033[A % i # print subsystem has done stdout.flush time.sleep(.5) It's dirty cause the next line was already entered and thus is written (as an empty line) and content of older steps will stay an screen, when subsequents lines aren't long enough to overwrite them. Which also applies to \r: import sys,time for i in range(8,0,-1): # printing 8**8, ..., 0**0 on line. Forget to overwrite sys.stdout.write( step: %s\r % (i**i)) sys.stdout.flush() time.sleep(.5) Fixes are to add whitespace to the line. Stepping back with \033[D fixes the empty newline issue (which is most often not worth the effort): import sys,time for i in range(8,0,-1): # fixing output length to 30 output = %-30s % step: %s % (i**i) length = len(output) sys.stdout.write(output) # print output, would be different, because of implizit spaces sys.stdout.write(\033[D* (length)) sys.stdout.flush() time.sleep(.5) regards Michael Excellent. Thanks Kent and Michael. Great tutorial ;-) You're right Michael. This last one works nicely but it is probably not worth the effort. I likes the previous one but adding fixed formatting to the number output, something like step: %9s\r instead of just step: %s\r. Thanks again. Victor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
Orri Ganel wrote: Jacob S. wrote: Thanks Kent and Max! Wow, I didn't know it did that. I'm too dumb to figure it out on my own I guess... Oh well! I found a cool new thing to play with at least! Thanks, Jacob On Jan 30, 2005, at 02:40, Jacob S. wrote: I don't think that's what he wants. I think he wants to *overwrite* what's in the shell with new output. For example. so that the whole line is overwritten. In my experience, this is not possible and if anyone can show me how to do it, I would be grateful. HTH, Jacob It *is* possible, that's exactly what my code does (well, as long as you don't run it on Mac OS 9). The carriage return (\r, as opposed to the linefeed \n) moves the cursor to the beginning of the *current* line. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Just a note: This does not work on IDLE, so for those who try this and are frustrated when it fails, try it in the dos-box (command prompt). I played around with this output issue and I love the way it works. Now, how do you do this in *nix? I tried the same approach and I get a blank line for 5 seconds (or whatever number of cycles you have on your example) and the a final line with the last value of the iterable. Do you happen to know how this in done? Thanks. Victor worked around this problem and I love the solution. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
Victor Rex wrote: I played around with this output issue and I love the way it works. Now, how do you do this in *nix? I tried the same approach and I get a blank line for 5 seconds (or whatever number of cycles you have on your example) and the a final line with the last value of the iterable. It sounds like the '\r' is erasing the line, not just moving the cursor. Try putting the '\r' at the beginning of the output rather than the end: for i in range(10): print '\r', 'i is', i time.sleep(1) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
print Percent completed: + str(percent) + \r Which should send me back to the beginning of the line and overwrite it with a new line. But instead I get: Percent completed: 50 Percent completed: 51 Print always adds a newline unless you put a comma at the end. Unfortunately that results in a character being lost too so I recommend using a tab at the front like this: for n in range(20): print \tAt line: %5s % n Also be aware that on sime systems \r is the newline character in which case the method I use is to backspace the number of characters occupied by my data (5 in the case above) using \010. Something like this slightly flawed example: def f(): ... print Label:, ... for n in range(5): ... print %s%3s % ('\010' * 3, n), ... I'll leave debugging it as an excercise for the reader! :-) HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] carriage return on windows
Hello, I'm trying to have a loop in a program print a message so I know it's status. Right now I'm using print Percent completed: + str(percent) + \r Which should send me back to the beginning of the line and overwrite it with a new line. But instead I get: Percent completed: 50 Percent completed: 51 Percent completed: 52 Percent completed: 53 and so on. Am I using this wrong, and if so, what is the right way to implement something like this? Thanks for any help! Bill ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
print Percent completed: + str(percent) + \r Print forces a newline. Try sys.stdout.write instead. Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
On Jan 30, 2005, at 02:18, R. Alan Monroe wrote: print Percent completed: + str(percent) + \r Print forces a newline. Try sys.stdout.write instead. Alan You can also use the following syntax: print Percent completed:, str(percent), \r, The trailing comma is NOT a typo, it is intentional. It prevents print from appending a newline. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
I don't think that's what he wants. I think he wants to *overwrite* what's in the shell with new output. For example. Python 2.4 (#Stuff) ... Percent complete: 50 becomes... Python2.4(#Stuff) ... Percent complete: 51 so that the whole line is overwritten. In my experience, this is not possible and if anyone can show me how to do it, I would be grateful. HTH, Jacob On Jan 30, 2005, at 02:18, R. Alan Monroe wrote: print Percent completed: + str(percent) + \r Print forces a newline. Try sys.stdout.write instead. Alan You can also use the following syntax: print Percent completed:, str(percent), \r, The trailing comma is NOT a typo, it is intentional. It prevents print from appending a newline. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
It seems to work fine in Win2k command shell; try this: import time time.sleep(1) for i in range(9): ... print 'i is', i, '\r', ... time.sleep(1) I get all the output on one line. Kent Jacob S. wrote: I don't think that's what he wants. I think he wants to *overwrite* what's in the shell with new output. For example. Python 2.4 (#Stuff) ... Percent complete: 50 becomes... Python2.4(#Stuff) ... Percent complete: 51 so that the whole line is overwritten. In my experience, this is not possible and if anyone can show me how to do it, I would be grateful. HTH, Jacob On Jan 30, 2005, at 02:18, R. Alan Monroe wrote: print Percent completed: + str(percent) + \r Print forces a newline. Try sys.stdout.write instead. Alan You can also use the following syntax: print Percent completed:, str(percent), \r, The trailing comma is NOT a typo, it is intentional. It prevents print from appending a newline. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] carriage return on windows
Thanks Kent and Max! Wow, I didn't know it did that. I'm too dumb to figure it out on my own I guess... Oh well! I found a cool new thing to play with at least! Thanks, Jacob On Jan 30, 2005, at 02:40, Jacob S. wrote: I don't think that's what he wants. I think he wants to *overwrite* what's in the shell with new output. For example. so that the whole line is overwritten. In my experience, this is not possible and if anyone can show me how to do it, I would be grateful. HTH, Jacob It *is* possible, that's exactly what my code does (well, as long as you don't run it on Mac OS 9). The carriage return (\r, as opposed to the linefeed \n) moves the cursor to the beginning of the *current* line. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor