if STREAM.isatty():

Cameron Simpson cs at cskk.id.au
Thu Aug 29 19:22:45 EDT 2019


On 29Aug2019 22:38, Hongyi Zhao <hongyi.zhao at gmail.com> wrote:
>On Thu, 29 Aug 2019 16:42:44 +0100, Rhodri James wrote:
>The original code snippet's logic is as follows:
>
>        if STREAM.isatty():
>            p = (self.progress_template + '\r') % params
>        else:
>           [do something to reset the new value of p]
>           p = (self.progress_template + '\n') % params
>        STREAM.write(p)
>        STREAM.flush()
>
>
>In order to refresh the tty, the if and else must be run alternatively.
>i.e., the isatty() must return 0 and 1 periodically.
>
>I still understand how the above code will let the isatty() return 0 and
>1 in turn?

No, merely that the test is done every time this code runs. It will 
normally always return the same value.

What you describe above is two distinct implementations of progress 
reporting, chosen accordin to the nature of the output stream.

If the output stream is a terminal, it always writes a progress string 
and a carriage return, which will return the cursor to the start of the 
line. Notably, this never scrolls the display. The next call will 
overwrite what happened before.

Otherwise, it writes the progress string and a newline on the premise 
that the output is a normal text file, such as a log file. So it just 
wants to write normal lines of text.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list