mirror of
https://github.com/python/cpython.git
synced 2025-11-11 22:55:08 +00:00
Add a more substantial example startup file for the interactive shell;
sample startup script provided by Itamar Shtull-Trauring. This closes SF patch #410890. Add some logical markup where it was missing.
This commit is contained in:
parent
70b014d3d3
commit
01815526e0
1 changed files with 44 additions and 7 deletions
|
|
@ -4068,15 +4068,52 @@ import rlcompleter, readline
|
||||||
readline.parse_and_bind('tab: complete')
|
readline.parse_and_bind('tab: complete')
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
This binds the TAB key to the completion function, so hitting the TAB
|
This binds the \kbd{Tab} key to the completion function, so hitting
|
||||||
key twice suggests completions; it looks at Python statement names,
|
the \kbd{Tab} key twice suggests completions; it looks at Python
|
||||||
the current local variables, and the available module names. For
|
statement names, the current local variables, and the available module
|
||||||
dotted expressions such as \code{string.a}, it will evaluate the the
|
names. For dotted expressions such as \code{string.a}, it will
|
||||||
expression up to the final \character{.} and then suggest completions
|
evaluate the the expression up to the final \character{.} and then
|
||||||
from the attributes of the resulting object. Note that this may
|
suggest completions from the attributes of the resulting object. Note
|
||||||
execute application-defined code if an object with a
|
that this may execute application-defined code if an object with a
|
||||||
\method{__getattr__()} method is part of the expression.
|
\method{__getattr__()} method is part of the expression.
|
||||||
|
|
||||||
|
A more capable startup file might look like this example. Note that
|
||||||
|
this deletes the names it creates once they are no longer needed; this
|
||||||
|
is done since the startup file is executed in the same namespace as
|
||||||
|
the interactive commands, and removing the names avoids creating side
|
||||||
|
effects in the interactive environments. You may find it convenient
|
||||||
|
to keep some of the imported modules, such as \module{os}, which turn
|
||||||
|
out to be needed in most sessions with the interpreter.
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
# Add auto-completion and a stored history file of commands to your Python
|
||||||
|
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
|
||||||
|
# bound to the Esc key by default (you can change it - see readline docs).
|
||||||
|
#
|
||||||
|
# Store the file in ~/.pystartup, and set an environment variable to point
|
||||||
|
# to it, e.g. "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash.
|
||||||
|
#
|
||||||
|
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
|
||||||
|
# full path to your home directory.
|
||||||
|
|
||||||
|
import atexit
|
||||||
|
import os
|
||||||
|
import readline
|
||||||
|
import rlcompleter
|
||||||
|
|
||||||
|
historyPath = os.path.expanduser("~/.pyhistory")
|
||||||
|
|
||||||
|
def save_history(historyPath=historyPath):
|
||||||
|
import readline
|
||||||
|
readline.write_history_file(historyPath)
|
||||||
|
|
||||||
|
if os.path.exists(historyPath):
|
||||||
|
readline.read_history_file(historyPath)
|
||||||
|
|
||||||
|
atexit.register(save_history)
|
||||||
|
del os, atexit, readline, rlcompleter, save_history, historyPath
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
|
||||||
\section{Commentary \label{commentary}}
|
\section{Commentary \label{commentary}}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue