[PythonCE] input(...) and more

Telion telionce@yahoo.com
Fri, 11 Oct 2002 12:10:53 -0700 (PDT)

--- goodey27@juno.com wrote:
> 1) I previously posted about the Hourglass coming up when using the
> input(...) & raw_input(...) functions. 
> I just want too mention. That when I run a script by double clicking it ,
> I don't have this problem.

I guees it has to do something with thread related issue.
Maybe sending a message to somewhere will eeliminate.
But I don't know.

Let's see if someone who knows better than me can 
come up with solution.

> 2) Another question, I'm looking for a way to get the path of the script,
> inside the script. Is there any way to do this.
> Greetings
> Isr

Do you mean samething as when you do this?

>>> import re
>>> re
<module 're' from '\MemCard2\Python22\lib\re.pyc'>

['DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'U',
'UNICODE', 'VERBOSE', 'X', '__all__', '__builtins__', '__doc__', '__file__',
'__name__', 'compile', 'engine', 'error', 'escape', 'findall', 'match',
'purge', 'search', 'split', 'sub', 'subn', 'template']
>>> re.__name__
>>> re.__file__

So, __file__ would give you the path for the script.
To test, create a file with something like this,

print locals().keys()
print globals().keys()
print __name__
print __file__

and put it under within sys.path.

import that module and you can see that works.

If you are interested in "Know yourself" type of thing,
frame object gives you many informations.

>>> dir(sys._getframe(0))
['__class__', '__delattr__', '__getattribute__', '__hash__', '__init__',
'__new__', '__reduce__', '__repr__', '__setattr__', '__str__', 'f_back',
'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value',
'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace']
>>> sys._getframe(2).f_code
<code object RunCode at 000E2818, file "\MemCard2\zzPython22\pcceshell.py",
line 528>

>>> sys._getframe(2).f_locals
{'cmdToExecute': None, 'i': 0, 'shell': <pcceshell.SimpleShell instance at
0x000DDAF8>, 'bKeepOpen': 0, 'bInteract': 1}

>>> dir(sys._getframe(2).f_code)
['__class__', '__cmp__', '__delattr__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__',
'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename',
'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name',
'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']

>>> sys._getframe(2).f_code.co_names
('sys', 'argv', 'appargv', 'bKeepOpen', 'bInteract', 'None', 'cmdToExecute',
'filter', 'i', 'len', 'string', 'join', 'traceback', 'print_exc', 'fname',
'os', 'path', 'splitext', 'ext', 'mode', 'imp', 'PY_COMPILED', 'imp_params',
'PY_SOURCE', 'open', 'file', 'IOError', 'code', 'why', 'load_module', 'close',
'Interact', 'shell', 'PostThreadMessage', 'shellThreadId', 'WM_QUIT')

>>> sys._getframe(2).f_code.co_varnames
('shell', 'imp_params', 'code', 'file', 'why', 'bInteract', 'i',
'cmdToExecute', 'ext', 'bKeepOpen', 'mode', 'fname')
>>> sys._getframe(2).f_code.co_filename
>>> sys._getframe(1).f_code.co_filename
>>> sys._getframe(0).f_code.co_filename

By inpecting frame object, you can know the file name, too.
File name of caller script, as well.

Each frame belongs to module, function, class, methods, etc...
And it makes its own namespace universe, I guess.

- telionce@yahoo.com -

Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More