ANN: Interactive Python prompt tools
Nathaniel Gray
n8gray at caltech.edu.is.my.e-mail.address.com
Fri Sep 28 20:04:19 EDT 2001
__________________________________________________________________
Announcing: LazyPython v0.5
deep_reload v0.5
Tools for the interactive Python prompt.
__________________________________________________________________
WHAT ARE THEY?
LazyPython is an exception hook for Python versions 2.1 and later that brings
shell escapes, auto-parenthesizing and auto-quoting to the python interactive
prompt.
deep_reload is an import hook for Python versions 1.5.2 and above that causes
the reload() command to act recursively, reloading all of the modules that a
given module imports.
Together they enhance the convenience of the Python prompt as an
interactive environment like, for example, Matlab.
HOW DO I USE THEM?
Here's an example LazyPython session:
>>> import LazyPython
Welcome to Lazy Python. Type "help LazyPython" for help.
>>> sys.excepthook = LazyPython.LazyPython()
>>>
>>> # '!' is the shell escape character
>>> !pwd
/home/n8gray/python
>>> !ls
CVS gracePlot.py n8utils.pyc
Clex.py gracePlot.py.bak pythonstartup.py
Heller_inline_message.txt gracePlot.py.bck pythonstartup.py.bck
... snip listing ...
>>>
>>> # '/' is the auto-paren escape character
>>> import gracePlot
>>> /reload gracePlot # Look Ma, no parens!
--> reload(gracePlot)
>>> # When LazyPython rewrites your command line it notifies you with
>>> # the line "--> <new-cmd-line>\n\n"
>>>
>>> # Since reload is a callable, you don't really even have to escape it!
>>> reload gracePlot # No '/' or parens
--> reload(gracePlot)
>>> # ',' is the auto-quote escape character
>>> ,some_function arg1 arg2 arg3
--> some_function("arg1", "arg2", "arg3")
>>> # You can also specify a list of auto-quote functions that you don't
>>> # need to escape. Assuming we have a convenience function 'cp'
>>> # that we've specified as an auto-quote function:
>>> cp somefile someotherfile # Yes, you could also use a shell escape
--> cp("somefile", "someotherfile")
>>>
Here's how you use deep_reload:
>>> import __builtin__, deep_reload
>>> __builtin__.reload = deep_reload.reload
>>> import Numeric
>>> reload Numeric # LazyPython auto-parenning :-)
--> reload(Numeric)
Reloading Numeric
Reloading numeric_version
Reloading multiarray
Reloading umath
... snip a bunch of other modules ...
Reloading copy
Reloading org
<module 'Numeric' from '/usr/local/[snip...]/Numeric.pyc'>
>>>
WHY ARE THEY USEFUL?
The interactive capability of Python is one of its shining strengths. However,
it seems that the interactive prompt was not intended to be the primary
working environment for Python programmers and it lacks certain convenience
features, like shell escapes, that would enable this usage. As packages such
as Matlab demonstrate, though, an interactive prompt can be a very effective
working environment, and thanks to advances such as pydoc, Python is achingly
close to providing an interactive prompt that one can work at for hours at a
time. These files aim to bring the Python prompt closer to dedicated
interactive environments such as Matlab in terms of convenience and
productivity.
Working interactively is much different than writing a program. Every
keystroke is precious. This is why the syntax of shells are so different from
that of most programming languages. This, plus the utility of shell escapes,
is the motivation for LazyPython.
Also, when working interactively there are many times when one wishes to reload
his entire project at once rather than one module at a time. This is when
deep_reload comes in useful.
Using these utilities I have successfully and comfortably used Python as an
interactive scientific computing environment.
WHERE CAN I GET THEM?
Both files can be obtained at:
http://www.idyll.org/~n8gray/code/index.html
__________________________________________________________________
Cheers,
-n8
--
Nathaniel Gray
California Institute of Technology
Computation and Neural Systems
--
More information about the Python-list
mailing list