[Tutor] CLS?
Chris Smith
smichr at bigfoot.com
Mon Apr 25 15:18:23 CEST 2005
I have this in my misc library. It was my last attempt at unifying the
cls function snippets that I got when I asked about this question some
time ago (check the ASPN tutor archives for 'cls'). After adding
"darwin" to the test for the system (instead of just 'mac') I got it to
work from a script written and executed in pythonIDE for 2.3.3. Does
it work for you in your context? (Note: it ends with the "white paper"
method if all else fails.)
###
#--------------------------
def isInteractive():
"""Returns 1 if we're running in an interpreter window, and false
otherwise. This is a variation of what
pydoc.help() tries. If it doesn't work it might be because the
None, 1, "?" is occuring on a different line. One might consider
the following approach instead:
for li in inspect.stack():
if li[1:4] == (None, 1, "?"):
return 1
"""
#return inspect.stack()[2][1:4] == (None, 1, "?")
# the line above will enable the math if called on the 1st line of
a script
#return inspect.stack()[3][1].find("PyInteractive")<>0
# but if this encounters a None at that position it will not be able
# to use the find function. SO...let's just look through the
whole stack
import inspect
return str([x[1] for x in inspect.stack()]).find("PyInteractive")>-1
#--------------------------
def cls():
"""A function to clear the output window."""
import sys,os
if sys.platform in ['mac', 'darwin']:
if isInteractive():
import PyConsole
PyConsole.console.clearbuffer()
print
else:
print #in case the window is not open yet
sys.stdout.clearbuffer()
else:
try:
os.system('cls')
except:
try:
os.system('clear')
except:
try:
print
sys.stdout.write(os.popen("clear").read())
except:
print '\n'*80
###
More information about the Tutor
mailing list