[Tutor] Re: sitecustomize and pythonstartup

Christopher Smith csmith@blakeschool.org
Sat, 02 Feb 2002 11:54:02 -0600


>####
>def isInteractive():
>    """Returns 1 if we're running in an interpreter window, and false
>    otherwise.  This is a variation of what pydoc.help() tries."""
>    
>    """
>    return inspect.stack()[3][1].find("PyInteractive")<>0
>
Well...this doesn't quite work because in a script, [3][1] might return
None which doesn't support the find function.  SO...rather than guess
where that PyInteractive might be I just turned the whole stack into a
string and looked for it.  The only problem with that is that even in a
script, the stack will contain the word "PyInteractive" because that is
the text that is being searched for.  SO...I just pulled out element [1]
from the stack and search through it for "PyInteractive" and that works:

def isInteractive():
.
.
.
    return str([x[1] for x in inspect.stack()]).find("PyInteractive")>-1

So math can now be accessed at the interpreter's prompt and within a
function:

>>> def f():
...  print math.pi
... 
>>> f()
3.14159265359
>>> math.pi
3.1415926535897931
>>>

So the IDE now behaves like the Interpreter in this regard.

In a script created within the IDE, both of the the following attempts at
printing pi fail (as they should):

print math.pi
def f():
	print math.pi
f()



/c