~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/readline.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>16.8. readline — GNU readline interface &mdash; Python 2.7.11 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.11',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 2.7.11 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
 
    <link rel="up" title="16. Optional Operating System Services" href="someos.html" />
34
 
    <link rel="next" title="16.9. rlcompleter — Completion function for GNU readline" href="rlcompleter.html" />
35
 
    <link rel="prev" title="16.7. mmap — Memory-mapped file support" href="mmap.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="rlcompleter.html" title="16.9. rlcompleter — Completion function for GNU readline"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="mmap.html" title="16.7. mmap — Memory-mapped file support"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.11</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="someos.html" accesskey="U">16. Optional Operating System Services</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
77
 
  <div class="section" id="module-readline">
78
 
<span id="readline-gnu-readline-interface"></span><h1>16.8. <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a> &#8212; GNU readline interface<a class="headerlink" href="#module-readline" title="Permalink to this headline">¶</a></h1>
79
 
<p>The <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a> module defines a number of functions to facilitate
80
 
completion and reading/writing of history files from the Python interpreter.
81
 
This module can be used directly or via the <a class="reference internal" href="rlcompleter.html#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code class="xref py py-mod docutils literal"><span class="pre">rlcompleter</span></code></a> module.  Settings
82
 
made using  this module affect the behaviour of both the interpreter&#8217;s
83
 
interactive prompt  and the prompts offered by the <a class="reference internal" href="functions.html#raw_input" title="raw_input"><code class="xref py py-func docutils literal"><span class="pre">raw_input()</span></code></a> and
84
 
<a class="reference internal" href="functions.html#input" title="input"><code class="xref py py-func docutils literal"><span class="pre">input()</span></code></a> built-in functions.</p>
85
 
<div class="admonition note">
86
 
<p class="first admonition-title">Note</p>
87
 
<p>On MacOS X the <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a> module can be implemented using
88
 
the <code class="docutils literal"><span class="pre">libedit</span></code> library instead of GNU readline.</p>
89
 
<p class="last">The configuration file for <code class="docutils literal"><span class="pre">libedit</span></code> is different from that
90
 
of GNU readline. If you programmatically load configuration strings
91
 
you can check for the text &#8220;libedit&#8221; in <code class="xref py py-const docutils literal"><span class="pre">readline.__doc__</span></code>
92
 
to differentiate between GNU readline and libedit.</p>
93
 
</div>
94
 
<p>The <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a> module defines the following functions:</p>
95
 
<dl class="function">
96
 
<dt id="readline.parse_and_bind">
97
 
<code class="descclassname">readline.</code><code class="descname">parse_and_bind</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.parse_and_bind" title="Permalink to this definition">¶</a></dt>
98
 
<dd><p>Parse and execute single line of a readline init file.</p>
99
 
</dd></dl>
100
 
 
101
 
<dl class="function">
102
 
<dt id="readline.get_line_buffer">
103
 
<code class="descclassname">readline.</code><code class="descname">get_line_buffer</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_line_buffer" title="Permalink to this definition">¶</a></dt>
104
 
<dd><p>Return the current contents of the line buffer.</p>
105
 
</dd></dl>
106
 
 
107
 
<dl class="function">
108
 
<dt id="readline.insert_text">
109
 
<code class="descclassname">readline.</code><code class="descname">insert_text</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.insert_text" title="Permalink to this definition">¶</a></dt>
110
 
<dd><p>Insert text into the command line.</p>
111
 
</dd></dl>
112
 
 
113
 
<dl class="function">
114
 
<dt id="readline.read_init_file">
115
 
<code class="descclassname">readline.</code><code class="descname">read_init_file</code><span class="sig-paren">(</span><span class="optional">[</span><em>filename</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.read_init_file" title="Permalink to this definition">¶</a></dt>
116
 
<dd><p>Parse a readline initialization file. The default filename is the last filename
117
 
used.</p>
118
 
</dd></dl>
119
 
 
120
 
<dl class="function">
121
 
<dt id="readline.read_history_file">
122
 
<code class="descclassname">readline.</code><code class="descname">read_history_file</code><span class="sig-paren">(</span><span class="optional">[</span><em>filename</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.read_history_file" title="Permalink to this definition">¶</a></dt>
123
 
<dd><p>Load a readline history file. The default filename is <code class="file docutils literal"><span class="pre">~/.history</span></code>.</p>
124
 
</dd></dl>
125
 
 
126
 
<dl class="function">
127
 
<dt id="readline.write_history_file">
128
 
<code class="descclassname">readline.</code><code class="descname">write_history_file</code><span class="sig-paren">(</span><span class="optional">[</span><em>filename</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.write_history_file" title="Permalink to this definition">¶</a></dt>
129
 
<dd><p>Save a readline history file. The default filename is <code class="file docutils literal"><span class="pre">~/.history</span></code>.</p>
130
 
</dd></dl>
131
 
 
132
 
<dl class="function">
133
 
<dt id="readline.clear_history">
134
 
<code class="descclassname">readline.</code><code class="descname">clear_history</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.clear_history" title="Permalink to this definition">¶</a></dt>
135
 
<dd><p>Clear the current history.  (Note: this function is not available if the
136
 
installed version of GNU readline doesn&#8217;t support it.)</p>
137
 
<div class="versionadded">
138
 
<p><span class="versionmodified">New in version 2.4.</span></p>
139
 
</div>
140
 
</dd></dl>
141
 
 
142
 
<dl class="function">
143
 
<dt id="readline.get_history_length">
144
 
<code class="descclassname">readline.</code><code class="descname">get_history_length</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_history_length" title="Permalink to this definition">¶</a></dt>
145
 
<dd><p>Return the desired length of the history file.  Negative values imply unlimited
146
 
history file size.</p>
147
 
</dd></dl>
148
 
 
149
 
<dl class="function">
150
 
<dt id="readline.set_history_length">
151
 
<code class="descclassname">readline.</code><code class="descname">set_history_length</code><span class="sig-paren">(</span><em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.set_history_length" title="Permalink to this definition">¶</a></dt>
152
 
<dd><p>Set the number of lines to save in the history file. <a class="reference internal" href="#readline.write_history_file" title="readline.write_history_file"><code class="xref py py-func docutils literal"><span class="pre">write_history_file()</span></code></a>
153
 
uses this value to truncate the history file when saving.  Negative values imply
154
 
unlimited history file size.</p>
155
 
</dd></dl>
156
 
 
157
 
<dl class="function">
158
 
<dt id="readline.get_current_history_length">
159
 
<code class="descclassname">readline.</code><code class="descname">get_current_history_length</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_current_history_length" title="Permalink to this definition">¶</a></dt>
160
 
<dd><p>Return the number of lines currently in the history.  (This is different from
161
 
<a class="reference internal" href="#readline.get_history_length" title="readline.get_history_length"><code class="xref py py-func docutils literal"><span class="pre">get_history_length()</span></code></a>, which returns the maximum number of lines that will
162
 
be written to a history file.)</p>
163
 
<div class="versionadded">
164
 
<p><span class="versionmodified">New in version 2.3.</span></p>
165
 
</div>
166
 
</dd></dl>
167
 
 
168
 
<dl class="function">
169
 
<dt id="readline.get_history_item">
170
 
<code class="descclassname">readline.</code><code class="descname">get_history_item</code><span class="sig-paren">(</span><em>index</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_history_item" title="Permalink to this definition">¶</a></dt>
171
 
<dd><p>Return the current contents of history item at <em>index</em>.</p>
172
 
<div class="versionadded">
173
 
<p><span class="versionmodified">New in version 2.3.</span></p>
174
 
</div>
175
 
</dd></dl>
176
 
 
177
 
<dl class="function">
178
 
<dt id="readline.remove_history_item">
179
 
<code class="descclassname">readline.</code><code class="descname">remove_history_item</code><span class="sig-paren">(</span><em>pos</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.remove_history_item" title="Permalink to this definition">¶</a></dt>
180
 
<dd><p>Remove history item specified by its position from the history.</p>
181
 
<div class="versionadded">
182
 
<p><span class="versionmodified">New in version 2.4.</span></p>
183
 
</div>
184
 
</dd></dl>
185
 
 
186
 
<dl class="function">
187
 
<dt id="readline.replace_history_item">
188
 
<code class="descclassname">readline.</code><code class="descname">replace_history_item</code><span class="sig-paren">(</span><em>pos</em>, <em>line</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.replace_history_item" title="Permalink to this definition">¶</a></dt>
189
 
<dd><p>Replace history item specified by its position with the given line.</p>
190
 
<div class="versionadded">
191
 
<p><span class="versionmodified">New in version 2.4.</span></p>
192
 
</div>
193
 
</dd></dl>
194
 
 
195
 
<dl class="function">
196
 
<dt id="readline.redisplay">
197
 
<code class="descclassname">readline.</code><code class="descname">redisplay</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.redisplay" title="Permalink to this definition">¶</a></dt>
198
 
<dd><p>Change what&#8217;s displayed on the screen to reflect the current contents of the
199
 
line buffer.</p>
200
 
<div class="versionadded">
201
 
<p><span class="versionmodified">New in version 2.3.</span></p>
202
 
</div>
203
 
</dd></dl>
204
 
 
205
 
<dl class="function">
206
 
<dt id="readline.set_startup_hook">
207
 
<code class="descclassname">readline.</code><code class="descname">set_startup_hook</code><span class="sig-paren">(</span><span class="optional">[</span><em>function</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.set_startup_hook" title="Permalink to this definition">¶</a></dt>
208
 
<dd><p>Set or remove the startup_hook function.  If <em>function</em> is specified, it will be
209
 
used as the new startup_hook function; if omitted or <code class="docutils literal"><span class="pre">None</span></code>, any hook function
210
 
already installed is removed.  The startup_hook function is called with no
211
 
arguments just before readline prints the first prompt.</p>
212
 
</dd></dl>
213
 
 
214
 
<dl class="function">
215
 
<dt id="readline.set_pre_input_hook">
216
 
<code class="descclassname">readline.</code><code class="descname">set_pre_input_hook</code><span class="sig-paren">(</span><span class="optional">[</span><em>function</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.set_pre_input_hook" title="Permalink to this definition">¶</a></dt>
217
 
<dd><p>Set or remove the pre_input_hook function.  If <em>function</em> is specified, it will
218
 
be used as the new pre_input_hook function; if omitted or <code class="docutils literal"><span class="pre">None</span></code>, any hook
219
 
function already installed is removed.  The pre_input_hook function is called
220
 
with no arguments after the first prompt has been printed and just before
221
 
readline starts reading input characters.</p>
222
 
</dd></dl>
223
 
 
224
 
<dl class="function">
225
 
<dt id="readline.set_completer">
226
 
<code class="descclassname">readline.</code><code class="descname">set_completer</code><span class="sig-paren">(</span><span class="optional">[</span><em>function</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.set_completer" title="Permalink to this definition">¶</a></dt>
227
 
<dd><p>Set or remove the completer function.  If <em>function</em> is specified, it will be
228
 
used as the new completer function; if omitted or <code class="docutils literal"><span class="pre">None</span></code>, any completer
229
 
function already installed is removed.  The completer function is called as
230
 
<code class="docutils literal"><span class="pre">function(text,</span> <span class="pre">state)</span></code>, for <em>state</em> in <code class="docutils literal"><span class="pre">0</span></code>, <code class="docutils literal"><span class="pre">1</span></code>, <code class="docutils literal"><span class="pre">2</span></code>, ..., until it
231
 
returns a non-string value.  It should return the next possible completion
232
 
starting with <em>text</em>.</p>
233
 
</dd></dl>
234
 
 
235
 
<dl class="function">
236
 
<dt id="readline.get_completer">
237
 
<code class="descclassname">readline.</code><code class="descname">get_completer</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_completer" title="Permalink to this definition">¶</a></dt>
238
 
<dd><p>Get the completer function, or <code class="docutils literal"><span class="pre">None</span></code> if no completer function has been set.</p>
239
 
<div class="versionadded">
240
 
<p><span class="versionmodified">New in version 2.3.</span></p>
241
 
</div>
242
 
</dd></dl>
243
 
 
244
 
<dl class="function">
245
 
<dt id="readline.get_completion_type">
246
 
<code class="descclassname">readline.</code><code class="descname">get_completion_type</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_completion_type" title="Permalink to this definition">¶</a></dt>
247
 
<dd><p>Get the type of completion being attempted.</p>
248
 
<div class="versionadded">
249
 
<p><span class="versionmodified">New in version 2.6.</span></p>
250
 
</div>
251
 
</dd></dl>
252
 
 
253
 
<dl class="function">
254
 
<dt id="readline.get_begidx">
255
 
<code class="descclassname">readline.</code><code class="descname">get_begidx</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_begidx" title="Permalink to this definition">¶</a></dt>
256
 
<dd><p>Get the beginning index of the readline tab-completion scope.</p>
257
 
</dd></dl>
258
 
 
259
 
<dl class="function">
260
 
<dt id="readline.get_endidx">
261
 
<code class="descclassname">readline.</code><code class="descname">get_endidx</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_endidx" title="Permalink to this definition">¶</a></dt>
262
 
<dd><p>Get the ending index of the readline tab-completion scope.</p>
263
 
</dd></dl>
264
 
 
265
 
<dl class="function">
266
 
<dt id="readline.set_completer_delims">
267
 
<code class="descclassname">readline.</code><code class="descname">set_completer_delims</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.set_completer_delims" title="Permalink to this definition">¶</a></dt>
268
 
<dd><p>Set the readline word delimiters for tab-completion.</p>
269
 
</dd></dl>
270
 
 
271
 
<dl class="function">
272
 
<dt id="readline.get_completer_delims">
273
 
<code class="descclassname">readline.</code><code class="descname">get_completer_delims</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.get_completer_delims" title="Permalink to this definition">¶</a></dt>
274
 
<dd><p>Get the readline word delimiters for tab-completion.</p>
275
 
</dd></dl>
276
 
 
277
 
<dl class="function">
278
 
<dt id="readline.set_completion_display_matches_hook">
279
 
<code class="descclassname">readline.</code><code class="descname">set_completion_display_matches_hook</code><span class="sig-paren">(</span><span class="optional">[</span><em>function</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#readline.set_completion_display_matches_hook" title="Permalink to this definition">¶</a></dt>
280
 
<dd><p>Set or remove the completion display function.  If <em>function</em> is
281
 
specified, it will be used as the new completion display function;
282
 
if omitted or <code class="docutils literal"><span class="pre">None</span></code>, any completion display function already
283
 
installed is removed.  The completion display function is called as
284
 
<code class="docutils literal"><span class="pre">function(substitution,</span> <span class="pre">[matches],</span> <span class="pre">longest_match_length)</span></code> once
285
 
each time matches need to be displayed.</p>
286
 
<div class="versionadded">
287
 
<p><span class="versionmodified">New in version 2.6.</span></p>
288
 
</div>
289
 
</dd></dl>
290
 
 
291
 
<dl class="function">
292
 
<dt id="readline.add_history">
293
 
<code class="descclassname">readline.</code><code class="descname">add_history</code><span class="sig-paren">(</span><em>line</em><span class="sig-paren">)</span><a class="headerlink" href="#readline.add_history" title="Permalink to this definition">¶</a></dt>
294
 
<dd><p>Append a line to the history buffer, as if it was the last line typed.</p>
295
 
</dd></dl>
296
 
 
297
 
<div class="admonition seealso">
298
 
<p class="first admonition-title">See also</p>
299
 
<dl class="last docutils">
300
 
<dt>Module <a class="reference internal" href="rlcompleter.html#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><code class="xref py py-mod docutils literal"><span class="pre">rlcompleter</span></code></a></dt>
301
 
<dd>Completion of Python identifiers at the interactive prompt.</dd>
302
 
</dl>
303
 
</div>
304
 
<div class="section" id="example">
305
 
<span id="readline-example"></span><h2>16.8.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
306
 
<p>The following example demonstrates how to use the <a class="reference internal" href="#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">readline</span></code></a> module&#8217;s
307
 
history reading and writing functions to automatically load and save a history
308
 
file named <code class="file docutils literal"><span class="pre">.pyhist</span></code> from the user&#8217;s home directory.  The code below would
309
 
normally be executed automatically during interactive sessions from the user&#8217;s
310
 
<span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONSTARTUP</span></code></a> file.</p>
311
 
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
312
 
<span class="kn">import</span> <span class="nn">readline</span>
313
 
<span class="n">histfile</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="s2">&quot;~&quot;</span><span class="p">),</span> <span class="s2">&quot;.pyhist&quot;</span><span class="p">)</span>
314
 
<span class="k">try</span><span class="p">:</span>
315
 
    <span class="n">readline</span><span class="o">.</span><span class="n">read_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
316
 
    <span class="c1"># default history len is -1 (infinite), which may grow unruly</span>
317
 
    <span class="n">readline</span><span class="o">.</span><span class="n">set_history_length</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
318
 
<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
319
 
    <span class="k">pass</span>
320
 
<span class="kn">import</span> <span class="nn">atexit</span>
321
 
<span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">readline</span><span class="o">.</span><span class="n">write_history_file</span><span class="p">,</span> <span class="n">histfile</span><span class="p">)</span>
322
 
<span class="k">del</span> <span class="n">os</span><span class="p">,</span> <span class="n">histfile</span>
323
 
</pre></div>
324
 
</div>
325
 
<p>The following example extends the <a class="reference internal" href="code.html#code.InteractiveConsole" title="code.InteractiveConsole"><code class="xref py py-class docutils literal"><span class="pre">code.InteractiveConsole</span></code></a> class to
326
 
support history save/restore.</p>
327
 
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">code</span>
328
 
<span class="kn">import</span> <span class="nn">readline</span>
329
 
<span class="kn">import</span> <span class="nn">atexit</span>
330
 
<span class="kn">import</span> <span class="nn">os</span>
331
 
 
332
 
<span class="k">class</span> <span class="nc">HistoryConsole</span><span class="p">(</span><span class="n">code</span><span class="o">.</span><span class="n">InteractiveConsole</span><span class="p">):</span>
333
 
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">locals</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s2">&quot;&lt;console&gt;&quot;</span><span class="p">,</span>
334
 
                 <span class="n">histfile</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="s2">&quot;~/.console-history&quot;</span><span class="p">)):</span>
335
 
        <span class="n">code</span><span class="o">.</span><span class="n">InteractiveConsole</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">locals</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
336
 
        <span class="bp">self</span><span class="o">.</span><span class="n">init_history</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
337
 
 
338
 
    <span class="k">def</span> <span class="nf">init_history</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">histfile</span><span class="p">):</span>
339
 
        <span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s2">&quot;tab: complete&quot;</span><span class="p">)</span>
340
 
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">readline</span><span class="p">,</span> <span class="s2">&quot;read_history_file&quot;</span><span class="p">):</span>
341
 
            <span class="k">try</span><span class="p">:</span>
342
 
                <span class="n">readline</span><span class="o">.</span><span class="n">read_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
343
 
            <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
344
 
                <span class="k">pass</span>
345
 
            <span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_history</span><span class="p">,</span> <span class="n">histfile</span><span class="p">)</span>
346
 
 
347
 
    <span class="k">def</span> <span class="nf">save_history</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">histfile</span><span class="p">):</span>
348
 
        <span class="n">readline</span><span class="o">.</span><span class="n">set_history_length</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
349
 
        <span class="n">readline</span><span class="o">.</span><span class="n">write_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
350
 
</pre></div>
351
 
</div>
352
 
</div>
353
 
</div>
354
 
 
355
 
 
356
 
          </div>
357
 
        </div>
358
 
      </div>
359
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
360
 
        <div class="sphinxsidebarwrapper">
361
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
362
 
  <ul>
363
 
<li><a class="reference internal" href="#">16.8. <code class="docutils literal"><span class="pre">readline</span></code> &#8212; GNU readline interface</a><ul>
364
 
<li><a class="reference internal" href="#example">16.8.1. Example</a></li>
365
 
</ul>
366
 
</li>
367
 
</ul>
368
 
 
369
 
  <h4>Previous topic</h4>
370
 
  <p class="topless"><a href="mmap.html"
371
 
                        title="previous chapter">16.7. <code class="docutils literal"><span class="pre">mmap</span></code> &#8212; Memory-mapped file support</a></p>
372
 
  <h4>Next topic</h4>
373
 
  <p class="topless"><a href="rlcompleter.html"
374
 
                        title="next chapter">16.9. <code class="docutils literal"><span class="pre">rlcompleter</span></code> &#8212; Completion function for GNU readline</a></p>
375
 
<h3>This Page</h3>
376
 
<ul class="this-page-menu">
377
 
  <li><a href="../bugs.html">Report a Bug</a></li>
378
 
  <li><a href="../_sources/library/readline.txt"
379
 
         rel="nofollow">Show Source</a></li>
380
 
</ul>
381
 
 
382
 
<div id="searchbox" style="display: none" role="search">
383
 
  <h3>Quick search</h3>
384
 
    <form class="search" action="../search.html" method="get">
385
 
      <input type="text" name="q" />
386
 
      <input type="submit" value="Go" />
387
 
      <input type="hidden" name="check_keywords" value="yes" />
388
 
      <input type="hidden" name="area" value="default" />
389
 
    </form>
390
 
    <p class="searchtip" style="font-size: 90%">
391
 
    Enter search terms or a module, class or function name.
392
 
    </p>
393
 
</div>
394
 
<script type="text/javascript">$('#searchbox').show(0);</script>
395
 
        </div>
396
 
      </div>
397
 
      <div class="clearer"></div>
398
 
    </div>  
399
 
    <div class="related" role="navigation" aria-label="related navigation">
400
 
      <h3>Navigation</h3>
401
 
      <ul>
402
 
        <li class="right" style="margin-right: 10px">
403
 
          <a href="../genindex.html" title="General Index"
404
 
             >index</a></li>
405
 
        <li class="right" >
406
 
          <a href="../py-modindex.html" title="Python Module Index"
407
 
             >modules</a> |</li>
408
 
        <li class="right" >
409
 
          <a href="rlcompleter.html" title="16.9. rlcompleter — Completion function for GNU readline"
410
 
             >next</a> |</li>
411
 
        <li class="right" >
412
 
          <a href="mmap.html" title="16.7. mmap — Memory-mapped file support"
413
 
             >previous</a> |</li>
414
 
        <li><img src="../_static/py.png" alt=""
415
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
416
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
417
 
        <li>
418
 
          <span class="version_switcher_placeholder">2.7.11</span>
419
 
          <a href="../index.html">Documentation</a> &raquo;
420
 
        </li>
421
 
 
422
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
423
 
          <li class="nav-item nav-item-2"><a href="someos.html" >16. Optional Operating System Services</a> &raquo;</li> 
424
 
      </ul>
425
 
    </div>  
426
 
    <div class="footer">
427
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
428
 
    <br />
429
 
    The Python Software Foundation is a non-profit corporation.
430
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
431
 
    <br />
432
 
    Last updated on Jan 23, 2016.
433
 
    <a href="../bugs.html">Found a bug</a>?
434
 
    <br />
435
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
436
 
    </div>
437
 
 
438
 
  </body>
439
 
</html>
 
 
b'\\ No newline at end of file'