[Python-checkins] python/dist/src/Doc/lib libreadline.tex, 1.13, 1.14

montanaro at users.sourceforge.net montanaro at users.sourceforge.net
Sun May 23 15:06:44 EDT 2004


Update of /cvsroot/python/python/dist/src/Doc/lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24714

Modified Files:
	libreadline.tex 
Log Message:
Add example that uses readline.readline().


Index: libreadline.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libreadline.tex,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** libreadline.tex	23 May 2004 17:46:49 -0000	1.13
--- libreadline.tex	23 May 2004 19:06:41 -0000	1.14
***************
*** 160,161 ****
--- 160,194 ----
  \end{verbatim}
  
+ The following example extends the \class{code.InteractiveConsole} class to
+ support command line editing and history save/restore.
+ 
+ \begin{verbatim}
+ import code
+ import readline
+ import atexit
+ import os
+ 
+ class HistoryConsole(code.InteractiveConsole):
+     def __init__(self, locals=None, filename="<console>",
+                  histfile=os.path.expanduser("~/.console-history")):
+         code.InteractiveConsole.__init__(self)
+         self.init_history(histfile)
+ 
+     def init_history(self, histfile):
+         readline.parse_and_bind("tab: complete")
+         if hasattr(readline, "read_history_file"):
+             try:
+                 readline.read_history_file(histfile)
+             except IOError:
+                 pass
+             atexit.register(self.save_history, histfile)
+ 
+     def raw_input(self, prompt=""):
+         line = readline.readline(prompt)
+         if line:
+             readline.add_history(line)
+         return line
+ 
+     def save_history(self, histfile):
+         readline.write_history_file(histfile)
+ \end{verbatim}




More information about the Python-checkins mailing list