Hongyi Zhao wrote:
> On Thu, 29 Aug 2019 16:42:44 +0100, Rhodri James wrote:
>> I don't understand what's to not to understand.
>> if condition:
>> is a perfectly normal construction. If you mean something else, please
>> be explicit.
> Let me say it more explicitly:
> The original code snippet's logic is as follows:
> if STREAM.isatty():
> p = (self.progress_template + '\r') % params
> [do something to reset the new value of p]
> p = (self.progress_template + '\n') % params
> 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?
Perhaps a simple example can help?
$ cat checktty.py
stream = sys.stdout
message = "tty"
message = "no tty"
When you run the script it prints to the terminal:
$ python3 checktty.py
But when you redirect to a pipe or into a file:
$ python3 checktty.py | cat
$ python3 checktty.py > tmp.txt
$ cat tmp.txt