@@ -4068,15 +4068,52 @@ \section{Key Bindings \label{keyBindings}}
40684068readline.parse_and_bind('tab: complete')
40694069\end {verbatim }
40704070
4071- This binds the TAB key to the completion function, so hitting the TAB
4072- key twice suggests completions; it looks at Python statement names,
4073- the current local variables, and the available module names. For
4074- dotted expressions such as \code {string.a}, it will evaluate the the
4075- expression up to the final \character {.} and then suggest completions
4076- from the attributes of the resulting object. Note that this may
4077- execute application-defined code if an object with a
4071+ This binds the \kbd {Tab} key to the completion function, so hitting
4072+ the \kbd {Tab} key twice suggests completions; it looks at Python
4073+ statement names, the current local variables, and the available module
4074+ names. For dotted expressions such as \code {string.a}, it will
4075+ evaluate the the expression up to the final \character {.} and then
4076+ suggest completions from the attributes of the resulting object. Note
4077+ that this may execute application-defined code if an object with a
40784078\method {__getattr__()} method is part of the expression.
40794079
4080+ A more capable startup file might look like this example. Note that
4081+ this deletes the names it creates once they are no longer needed; this
4082+ is done since the startup file is executed in the same namespace as
4083+ the interactive commands, and removing the names avoids creating side
4084+ effects in the interactive environments. You may find it convenient
4085+ to keep some of the imported modules, such as \module {os}, which turn
4086+ out to be needed in most sessions with the interpreter.
4087+
4088+ \begin {verbatim }
4089+ # Add auto-completion and a stored history file of commands to your Python
4090+ # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
4091+ # bound to the Esc key by default (you can change it - see readline docs).
4092+ #
4093+ # Store the file in ~/.pystartup, and set an environment variable to point
4094+ # to it, e.g. "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash.
4095+ #
4096+ # Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
4097+ # full path to your home directory.
4098+
4099+ import atexit
4100+ import os
4101+ import readline
4102+ import rlcompleter
4103+
4104+ historyPath = os.path.expanduser("~/.pyhistory")
4105+
4106+ def save_history(historyPath=historyPath):
4107+ import readline
4108+ readline.write_history_file(historyPath)
4109+
4110+ if os.path.exists(historyPath):
4111+ readline.read_history_file(historyPath)
4112+
4113+ atexit.register(save_history)
4114+ del os, atexit, readline, rlcompleter, save_history, historyPath
4115+ \end {verbatim }
4116+
40804117
40814118\section {Commentary \label {commentary } }
40824119
0 commit comments