[Tutor] Re: clear screen [Let's make a feature request in Sourceforge!]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed Nov 13 02:20:01 2002


> From: Scott Widney [mailto:SWidney@ci.las-vegas.nv.us]
>
> > I also got a few "you dumb *&@*%*" type messages in private as well.

Goodness.  That's not nice at all.


> Sorry KP. I apologize on their behalves(?) since the lack of good
> judgement that allowed them to compose their remarks will probably keep
> them from doing so. I'm sure they represent a minority.  That behavior
> is definitely not in keeping with the spirit of this list. Lately,
> things have been a little more preach than teach....

Hi Kevin,

The problem with languages, invariably, is that people grow very very fond
of them, to the point of being extraordinarly defensive.  It pushes those
hot buttons that's at the core of every human who speaks language.

This happens with human languages, and for some strange reason, the same
darn thing happens with computer languages.  Even though we should know
better.

Doh, now I'm being preachy now.  Sorry Scott.  *grin*



If someone says that a part of the language doesn't feel clean and
consistant, I think it's worth taking a look into it.  The clear screen
issue has come up several times already on Tutor, so, clearly, there must
be something that's very appealing about erasing the screen.  Chalkboards
can be erased, and I get the feeling that people who are new to
programming instinctively want that same refreshing feeling of "starting
over"  that clearing the screen provides.


Perhaps it might be worthwhile to ask the Python developers to add a
function in the 'os' module to provide some kind of platform-independent
console screen clearing code.  I feel that the platform-dependent nature
of such a hypothetical os.clear_screen()  function wouldn't be too much of
a loss, considering that there's already a bunch of functions in there
that only work on Windows and Unix anyway.


Here's a possible implementation of clear_screen():

###
"""Platform independent console clear-screen.

Using it should be fairly straightforward:

    if __name__ == '__main__':
        clear_screen()


See the thread starting here:

    http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/1428127

for motivations for this code.

Part of this code is adapted from a recipe in the Python Cookbook:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65257
"""
import os
import sys


class _cls:
    def __init__(self):
        if sys.platform in ('linux-i386', 'linux2'):
            self._command = 'clear'
        elif sys.platform in ('win32', 'dos') or \
             sys.platform.startswith('ms-dos'):
            self._command = 'cls'
        else:
            self._command = None

    def __call__(self):
        """Clears the screen."""
        if self._command:
            os.system(self._command)

clear_screen = _cls()

if __name__ == '__main__':
    clear_screen()             ## test code
###


I can't confirm that it works on Win32 yet; can someone check this?  Once
it checks out ok, we can submit this to Python's sourceforge as a feature
request.  Who knows?  It might get in...


I hope this helps!