[Tutor] Clear screen questions

Steven D'Aprano steve at pearwood.info
Mon May 6 05:12:34 CEST 2013


On 06/05/13 12:37, Brian van den Broek wrote:
> On 5 May 2013 22:10, boB Stepp <robertvstepp at gmail.com> wrote:
>
>> On Sun, May 5, 2013 at 7:54 PM, Steven D'Aprano <steve at pearwood.info>
>> wrote:
>>
>
> <snip>
>
>
>>>
>>>> So my main question is there a truly clean, cross-platform solution to
>>>> the clear screen dilemma? If my online searching is accurate, then the
>>>> answer appears to be no, unless one wants to print many blank lines.
>>>
>>>
>>> Your googling is accurate. There is no clean, cross-platform solution,
>> not
>>> even for the "main three" (Linux/Unix, Windows, Mac), let alone minority
>> and
>>> legacy platforms, other implementations, etc.
>>>
>>
>>
>
>> So it appears that the only way to cover the various possibilities is
>> to query for the platform being used and then apply the correct
>> statement for that platform. And it still would not work for the point
>> noted above. Could be a lot of effort for little gain!
>>
>>
>
> Try:
>
> def pragmatic_as_if_clear():
>      print '\n' * 100
>
> which isn't too far off of what clear does in bash.


Not in the version of bash I am using in an xterm window. (To be precise, Konsole under KDE 3.)

If I start a bash session, and then do something "large" like print a file listing, the prompt ends up right at the bottom of the screen. If I then call clear, the visible area of the screen is cleared, the prompt ends up at the top of the screen, but if I scroll back using the scroll bar, I can see the previous output immediately before the clear command, without 100 blank lines separating them.




>>>> A second question is that one person had as the answer to use:
>>>>
>>>> os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
>>>>
>>>> I don't understand this syntax. The writer said that if one
>>>> understands what this is doing, then the method is more generally
>>>> useful. Would someone explain how this works? And hopefully it will
>>>> become apparent to me how this is more generally useful?
>>
>>
> <snip>
>
>
>> terms when I saw the first pair of brackets, so it did not occur to me
>> to see the second set of brackets as indexing.
>>
>> boB
>>
>>
>
> Steven explained it. I'd point out that wiser snake charmers than I
> discouraged me (some on this list) from using it from the position that it
> was too clever. I've done so from time to time anyway; there is a momentary
> jolt when reading the code months later.

I don't know who they were, I certainly hope they didn't include me.

The list[index] trick shown above is clever, but it shouldn't be too clever. It used to be one of the standard ways of getting a one-line ternary expression in Python. Of course, these days we have proper ternary operator `value if flag else other`, but it's still a perfectly reasonable thing to do, no weirder and much more concise than something like this:

commands = {'windows': 'clr', 'other': 'clear'}
if os.name == 'nt':
     system = 'windows'
else:
     system = 'other'
os.system(commands[system])





-- 
Steven


More information about the Tutor mailing list