Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="module-readline.html" /> | |
13 | <link rel="parent" href="module-readline.html" /> | |
14 | <link rel="next" href="module-rlcompleter.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>7.20.1 Example </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="7.20 readline " | |
24 | href="module-readline.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="7.20 readline " | |
27 | href="module-readline.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="7.21 rlcompleter " | |
30 | href="module-rlcompleter.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="module-readline.html">7.20 readline </A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-readline.html">7.20 readline </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-rlcompleter.html">7.21 rlcompleter </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0092010000000000000000"></A><A NAME="readline-example"></A> | |
55 | <BR> | |
56 | 7.20.1 Example | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | The following example demonstrates how to use the | |
61 | <tt class="module">readline</tt> module's history reading and writing functions to | |
62 | automatically load and save a history file named <span class="file">.pyhist</span> from | |
63 | the user's home directory. The code below would normally be executed | |
64 | automatically during interactive sessions from the user's | |
65 | <a class="envvar" id='l2h-2964' xml:id='l2h-2964'>PYTHONSTARTUP</a> file. | |
66 | ||
67 | <P> | |
68 | <div class="verbatim"><pre> | |
69 | import os | |
70 | histfile = os.path.join(os.environ["HOME"], ".pyhist") | |
71 | try: | |
72 | readline.read_history_file(histfile) | |
73 | except IOError: | |
74 | pass | |
75 | import atexit | |
76 | atexit.register(readline.write_history_file, histfile) | |
77 | del os, histfile | |
78 | </pre></div> | |
79 | ||
80 | <P> | |
81 | The following example extends the <tt class="class">code.InteractiveConsole</tt> class to | |
82 | support history save/restore. | |
83 | ||
84 | <P> | |
85 | <div class="verbatim"><pre> | |
86 | import code | |
87 | import readline | |
88 | import atexit | |
89 | import os | |
90 | ||
91 | class HistoryConsole(code.InteractiveConsole): | |
92 | def __init__(self, locals=None, filename="<console>", | |
93 | histfile=os.path.expanduser("~/.console-history")): | |
94 | code.InteractiveConsole.__init__(self) | |
95 | self.init_history(histfile) | |
96 | ||
97 | def init_history(self, histfile): | |
98 | readline.parse_and_bind("tab: complete") | |
99 | if hasattr(readline, "read_history_file"): | |
100 | try: | |
101 | readline.read_history_file(histfile) | |
102 | except IOError: | |
103 | pass | |
104 | atexit.register(self.save_history, histfile) | |
105 | ||
106 | def save_history(self, histfile): | |
107 | readline.write_history_file(histfile) | |
108 | </pre></div> | |
109 | ||
110 | <DIV CLASS="navigation"> | |
111 | <div class='online-navigation'> | |
112 | <p></p><hr /> | |
113 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
114 | <tr> | |
115 | <td class='online-navigation'><a rel="prev" title="7.20 readline " | |
116 | href="module-readline.html"><img src='../icons/previous.png' | |
117 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
118 | <td class='online-navigation'><a rel="parent" title="7.20 readline " | |
119 | href="module-readline.html"><img src='../icons/up.png' | |
120 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
121 | <td class='online-navigation'><a rel="next" title="7.21 rlcompleter " | |
122 | href="module-rlcompleter.html"><img src='../icons/next.png' | |
123 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
124 | <td align="center" width="100%">Python Library Reference</td> | |
125 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
126 | href="contents.html"><img src='../icons/contents.png' | |
127 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
128 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
129 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
130 | <td class='online-navigation'><a rel="index" title="Index" | |
131 | href="genindex.html"><img src='../icons/index.png' | |
132 | border='0' height='32' alt='Index' width='32' /></A></td> | |
133 | </tr></table> | |
134 | <div class='online-navigation'> | |
135 | <b class="navlabel">Previous:</b> | |
136 | <a class="sectref" rel="prev" href="module-readline.html">7.20 readline </A> | |
137 | <b class="navlabel">Up:</b> | |
138 | <a class="sectref" rel="parent" href="module-readline.html">7.20 readline </A> | |
139 | <b class="navlabel">Next:</b> | |
140 | <a class="sectref" rel="next" href="module-rlcompleter.html">7.21 rlcompleter </A> | |
141 | </div> | |
142 | </div> | |
143 | <hr /> | |
144 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
145 | </DIV> | |
146 | <!--End of Navigation Panel--> | |
147 | <ADDRESS> | |
148 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
149 | </ADDRESS> | |
150 | </BODY> | |
151 | </HTML> |