Re: [Tutor] Displaying Status on the Command Line
On Thu, Nov 8, 2018 at 10:10 AM Steven D'Aprano wrote: > Sorry, I don't understand that. Maybe its too early in the morning for > my brain, but given that you've imported the Python 3 print function > from the __future__ why do you need the customer wrapper? > > from __future__ import print_function > > alone should give you exactly the Python 3 print function, with all its > bells and whistles. > > What have I missed? The `flush` parameter was added in 3.3. -- Zach ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On Thu, Nov 08, 2018 at 09:36:53AM -0600, Zachary Ware wrote: > You can use any of the `print` function tricks above in Python 2 with > the following boilerplate: > > from __future__ import print_function > > import sys > > _orig_print = print > > def print(*args, **kwargs): > flush = kwargs.pop('flush', False) > _orig_print(*args, **kwargs) > if flush: > file = kwargs.get('file', sys.stdout) > file.flush() Sorry, I don't understand that. Maybe its too early in the morning for my brain, but given that you've imported the Python 3 print function from the __future__ why do you need the customer wrapper? from __future__ import print_function alone should give you exactly the Python 3 print function, with all its bells and whistles. What have I missed? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On Thu, Nov 8, 2018 at 4:12 AM Chip Wachob wrote: > I should have mentioned that I'm working with Python 2, but I think I > can parse my way through these examples. You can use any of the `print` function tricks above in Python 2 with the following boilerplate: from __future__ import print_function import sys _orig_print = print def print(*args, **kwargs): flush = kwargs.pop('flush', False) _orig_print(*args, **kwargs) if flush: file = kwargs.get('file', sys.stdout) file.flush() When you get to upgrade to Python 3, just throw the above code away and things will work just the same :) -- Zach ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 08/11/2018 04:06, Chip Wachob wrote: > I should have mentioned that I'm working with Python 2, but I think I > can parse my way through these examples. OK, In that case you may want to investigate the sys.stdout approach. Just remember it's a pre opened file and use the write() method. But it won't do any of the print type things, you need to convert all values to strings and add in new lines etc yourself. IN fact if you go that way you might want to use sys.stderr instead since that will mean your progress indicator does not appear in the true output should you pipe it into a file. And finally, don't forget the >>> prompt is your friend for this kind of thing. Experiment there to get it right before committing it to a larger script. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
Wow! Thank you! Lots of things for me to try. I should have mentioned that I'm working with Python 2, but I think I can parse my way through these examples. Best, On 11/7/18, Cameron Simpson wrote: > On 08Nov2018 10:00, Steven D'Aprano wrote: >>> Note that I need this to be platform agnostic. >> >>That's hard, even on a single platform like Linux. > > Most, nearly all, terminal honour carriage return and backspace. That is > technically enough. Even terminals with a destructive backspace (rare - > it is normally just a cursor motion) can get by (backspace, overwrite > the new text). > >>Most xterminal windows use either the xterm or vt1000 set of commands, >>which are broadly similar, but that's not guaranteed. If somebody >>happens to be running a different terminal type, they'll see something >>weird. >> >>And I have no idea what happens on Windows. > > I'd sort of expect Windows terminals, even cmd.exe, to accept the ANSI > sequences, which is what vt100 and xterms use. But that is expectation, > not knowledge. > > Cheers, > Cameron Simpson > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
What Alan wrote makes sense if you just want to put out one mark per second till you stop. But if you want a percentage of progress, you need some way to estimate what percent of the way you are to being done. You need to determine how many marks represent 100% such as 50 periods. You need to have your code pause periodically and assess if the current percentage requires more characters than have been put out so far. If you were at 10% last time and had put out 5 dots and are now at 16% and need 3 more dots, you might say something like this with n=3, and ch="." print(ch*n, end="", flush=True), sep="") Note you want to make sure the buffers are flushed to make it real time and when done may want to print a newline so any further output is not left dangling. The above print statement is for Python 3.x, and as Alan explained, in version 2.X you may need to use alternate syntax. Not sure how you flush buffers but you can skip the print statement and write in other ways to sys.stdout. -Original Message- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Wednesday, November 7, 2018 2:15 PM To: tutor@python.org Subject: Re: [Tutor] Displaying Status on the Command Line On 07/11/2018 16:22, Chip Wachob wrote: > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? It depends on your Python version. In Python v2 you simply put a comma after your output character to turn off the auto newline while someProcess():# should probably be in a separate thread... time.sleep(1) # 1 second pause print '.', # comma suppresses newline If you want to suppress the spaces tyoo things get a tad more complex and you are probably best writing direct to sys.stdout In Python 3 there are parameters to print() while someProcess(): time.sleep(1) print('.', end='', sep='') # no newline and no spaces You shouldn't need any special escape codes. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 08Nov2018 10:00, Steven D'Aprano wrote: Note that I need this to be platform agnostic. That's hard, even on a single platform like Linux. Most, nearly all, terminal honour carriage return and backspace. That is technically enough. Even terminals with a destructive backspace (rare - it is normally just a cursor motion) can get by (backspace, overwrite the new text). Most xterminal windows use either the xterm or vt1000 set of commands, which are broadly similar, but that's not guaranteed. If somebody happens to be running a different terminal type, they'll see something weird. And I have no idea what happens on Windows. I'd sort of expect Windows terminals, even cmd.exe, to accept the ANSI sequences, which is what vt100 and xterms use. But that is expectation, not knowledge. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On Wed, Nov 07, 2018 at 11:22:22AM -0500, Chip Wachob wrote: > Hello, > > I'm sure that this is simple and my searches have just not used the > correct words. > > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? Here's a quick and easy(?) way to do this which works on typical Linux systems. I've written it for Python 3, let us know if you need help getting it to work on Python 2. import time def spinner_demo(): for i in range(1, 101): c = r'-\|/-\|/'[i % 8] print("Spinner: %c %d%%\r" % (c, i), flush=True, end='') time.sleep(0.15) print('\n') To display a throbber instead, change the first line inside the loop to c = '.oOo'[i % 4] Here's a progress bar: def progress_demo(): N = 250 template = "Working hard... %- 40s %3.0f%% complete\r" for i in range(N): perc = i*100/N bar = '#'*(i*40//N + 1) print(template % (bar, perc), flush=True, end='') time.sleep(0.1) print('\n') > Note that I need this to be platform agnostic. That's hard, even on a single platform like Linux. Most xterminal windows use either the xterm or vt1000 set of commands, which are broadly similar, but that's not guaranteed. If somebody happens to be running a different terminal type, they'll see something weird. And I have no idea what happens on Windows. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 07Nov2018 11:22, Chip Wachob wrote: I'm sure that this is simple and my searches have just not used the correct words. What I would like to do is display, on a single line, in the terminal / command line a progress percentage, or, simply a sequence of - / - \, etc.. or even, accumulating period characters. What would the escape codes be, or is there a better way to handle this? Note that I need this to be platform agnostic. I'e got a module 'cs.upd' on PyPI which does this. "pip install cs.upd" to obtain it. Typical usage: import sys import time from cs.upd import Upd upd = Upd(sys.stdout) for percentage in range(1,100): upd.out("Progress: %d%%", percentage) if percentage % 10 == 0: upd.nl("completed %d%%", percentage) time.sleep(0.1) upd.out("Complete!") time.sleep(1.0) That example is obviously contrived, and the sleeps are so you can see it all happen. But you can slot this into simple terminal based programmes to present dynamic progress. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On Wed, Nov 7, 2018 at 1:17 PM Alan Gauld via Tutor wrote: > In Python 3 there are parameters to print() > > while someProcess(): >time.sleep(1) >print('.', end='', sep='') # no newline and no spaces You'll also want `flush=True` here to avoid having your dots buffered until end-of-line. -- Zach ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 07/11/2018 16:22, Chip Wachob wrote: > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? It depends on your Python version. In Python v2 you simply put a comma after your output character to turn off the auto newline while someProcess():# should probably be in a separate thread... time.sleep(1) # 1 second pause print '.', # comma suppresses newline If you want to suppress the spaces tyoo things get a tad more complex and you are probably best writing direct to sys.stdout In Python 3 there are parameters to print() while someProcess(): time.sleep(1) print('.', end='', sep='') # no newline and no spaces You shouldn't need any special escape codes. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Displaying Status on the Command Line
Hello, I'm sure that this is simple and my searches have just not used the correct words. What I would like to do is display, on a single line, in the terminal / command line a progress percentage, or, simply a sequence of - / - \, etc.. or even, accumulating period characters. What would the escape codes be, or is there a better way to handle this? Note that I need this to be platform agnostic. Thank you in advance for your insight. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor