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="next" href="module-termios.html" /> | |
13 | <link rel="prev" href="module-dbm.html" /> | |
14 | <link rel="parent" href="unix.html" /> | |
15 | <link rel="next" href="module-termios.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>8.7 gdbm -- GNU's reinterpretation of dbm</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="8.6 dbm " | |
25 | href="module-dbm.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="8. Unix Specific Services" | |
28 | href="unix.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="8.8 termios " | |
31 | href="module-termios.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-dbm.html">8.6 dbm </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="unix.html">8. Unix Specific Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-termios.html">8.8 termios </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0010700000000000000000"> | |
56 | 8.7 <tt class="module">gdbm</tt> -- | |
57 | GNU's reinterpretation of dbm</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-gdbm"></A> | |
62 | <p class="availability">Availability: <span | |
63 | class="platform">Unix</span>.</p> | |
64 | ||
65 | <P> | |
66 | This module is quite similar to the <tt class="module"><a href="module-dbm.html">dbm</a></tt><a id='l2h-3006' xml:id='l2h-3006'></a> | |
67 | module, but uses <code>gdbm</code> instead to provide some additional | |
68 | functionality. Please note that the file formats created by | |
69 | <code>gdbm</code> and <code>dbm</code> are incompatible. | |
70 | ||
71 | <P> | |
72 | The <tt class="module">gdbm</tt> module provides an interface to the GNU DBM | |
73 | library. <code>gdbm</code> objects behave like mappings | |
74 | (dictionaries), except that keys and values are always strings. | |
75 | Printing a <code>gdbm</code> object doesn't print the keys and values, and | |
76 | the <tt class="method">items()</tt> and <tt class="method">values()</tt> methods are not supported. | |
77 | ||
78 | <P> | |
79 | The module defines the following constant and functions: | |
80 | ||
81 | <P> | |
82 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-3000' xml:id='l2h-3000' class="exception">error</tt></b></dt> | |
83 | <dd> | |
84 | Raised on <code>gdbm</code>-specific errors, such as I/O errors. | |
85 | <tt class="exception">KeyError</tt> is raised for general mapping errors like | |
86 | specifying an incorrect key. | |
87 | </dd></dl> | |
88 | ||
89 | <P> | |
90 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
91 | <td><nobr><b><tt id='l2h-3001' xml:id='l2h-3001' class="function">open</tt></b>(</nobr></td> | |
92 | <td><var>filename, </var><big>[</big><var>flag, </var><big>[</big><var>mode</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
93 | <dd> | |
94 | Open a <code>gdbm</code> database and return a <code>gdbm</code> object. The | |
95 | <var>filename</var> argument is the name of the database file. | |
96 | ||
97 | <P> | |
98 | The optional <var>flag</var> argument can be | |
99 | <code>'r'</code> (to open an existing database for reading only -- default), | |
100 | <code>'w'</code> (to open an existing database for reading and writing), | |
101 | <code>'c'</code> (which creates the database if it doesn't exist), or | |
102 | <code>'n'</code> (which always creates a new empty database). | |
103 | ||
104 | <P> | |
105 | The following additional characters may be appended to the flag to | |
106 | control how the database is opened: | |
107 | ||
108 | <P> | |
109 | ||
110 | <UL> | |
111 | <LI><code>'f'</code> -- Open the database in fast mode. Writes to the database | |
112 | will not be synchronized. | |
113 | </LI> | |
114 | <LI><code>'s'</code> -- Synchronized mode. This will cause changes to the database | |
115 | will be immediately written to the file. | |
116 | </LI> | |
117 | <LI><code>'u'</code> -- Do not lock database. | |
118 | </LI> | |
119 | </UL> | |
120 | ||
121 | <P> | |
122 | Not all flags are valid for all versions of <code>gdbm</code>. The | |
123 | module constant <code>open_flags</code> is a string of supported flag | |
124 | characters. The exception <tt class="exception">error</tt> is raised if an invalid | |
125 | flag is specified. | |
126 | ||
127 | <P> | |
128 | The optional <var>mode</var> argument is the <span class="Unix">Unix</span> mode of the file, used | |
129 | only when the database has to be created. It defaults to octal | |
130 | <code>0666</code>. | |
131 | </dl> | |
132 | ||
133 | <P> | |
134 | In addition to the dictionary-like methods, <code>gdbm</code> objects have the | |
135 | following methods: | |
136 | ||
137 | <P> | |
138 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
139 | <td><nobr><b><tt id='l2h-3002' xml:id='l2h-3002' class="function">firstkey</tt></b>(</nobr></td> | |
140 | <td><var></var>)</td></tr></table></dt> | |
141 | <dd> | |
142 | It's possible to loop over every key in the database using this method | |
143 | and the <tt class="method">nextkey()</tt> method. The traversal is ordered by | |
144 | <code>gdbm</code>'s internal hash values, and won't be sorted by the key | |
145 | values. This method returns the starting key. | |
146 | </dl> | |
147 | ||
148 | <P> | |
149 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
150 | <td><nobr><b><tt id='l2h-3003' xml:id='l2h-3003' class="function">nextkey</tt></b>(</nobr></td> | |
151 | <td><var>key</var>)</td></tr></table></dt> | |
152 | <dd> | |
153 | Returns the key that follows <var>key</var> in the traversal. The | |
154 | following code prints every key in the database <code>db</code>, without | |
155 | having to create a list in memory that contains them all: | |
156 | ||
157 | <P> | |
158 | <div class="verbatim"><pre> | |
159 | k = db.firstkey() | |
160 | while k != None: | |
161 | print k | |
162 | k = db.nextkey(k) | |
163 | </pre></div> | |
164 | </dl> | |
165 | ||
166 | <P> | |
167 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
168 | <td><nobr><b><tt id='l2h-3004' xml:id='l2h-3004' class="function">reorganize</tt></b>(</nobr></td> | |
169 | <td><var></var>)</td></tr></table></dt> | |
170 | <dd> | |
171 | If you have carried out a lot of deletions and would like to shrink | |
172 | the space used by the <code>gdbm</code> file, this routine will reorganize | |
173 | the database. <code>gdbm</code> will not shorten the length of a database | |
174 | file except by using this reorganization; otherwise, deleted file | |
175 | space will be kept and reused as new (key, value) pairs are added. | |
176 | </dl> | |
177 | ||
178 | <P> | |
179 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
180 | <td><nobr><b><tt id='l2h-3005' xml:id='l2h-3005' class="function">sync</tt></b>(</nobr></td> | |
181 | <td><var></var>)</td></tr></table></dt> | |
182 | <dd> | |
183 | When the database has been opened in fast mode, this method forces any | |
184 | unwritten data to be written to the disk. | |
185 | </dl> | |
186 | ||
187 | <P> | |
188 | <div class="seealso"> | |
189 | <p class="heading">See Also:</p> | |
190 | ||
191 | <dl compact="compact" class="seemodule"> | |
192 | <dt>Module <b><tt class="module"><a href="module-anydbm.html">anydbm</a></tt>:</b> | |
193 | <dd>Generic interface to <code>dbm</code>-style databases. | |
194 | </dl> | |
195 | <dl compact="compact" class="seemodule"> | |
196 | <dt>Module <b><tt class="module"><a href="module-whichdb.html">whichdb</a></tt>:</b> | |
197 | <dd>Utility module used to determine the type of an | |
198 | existing database. | |
199 | </dl> | |
200 | </div> | |
201 | ||
202 | <DIV CLASS="navigation"> | |
203 | <div class='online-navigation'> | |
204 | <p></p><hr /> | |
205 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
206 | <tr> | |
207 | <td class='online-navigation'><a rel="prev" title="8.6 dbm " | |
208 | href="module-dbm.html"><img src='../icons/previous.png' | |
209 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
210 | <td class='online-navigation'><a rel="parent" title="8. Unix Specific Services" | |
211 | href="unix.html"><img src='../icons/up.png' | |
212 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
213 | <td class='online-navigation'><a rel="next" title="8.8 termios " | |
214 | href="module-termios.html"><img src='../icons/next.png' | |
215 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
216 | <td align="center" width="100%">Python Library Reference</td> | |
217 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
218 | href="contents.html"><img src='../icons/contents.png' | |
219 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
220 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
221 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
222 | <td class='online-navigation'><a rel="index" title="Index" | |
223 | href="genindex.html"><img src='../icons/index.png' | |
224 | border='0' height='32' alt='Index' width='32' /></A></td> | |
225 | </tr></table> | |
226 | <div class='online-navigation'> | |
227 | <b class="navlabel">Previous:</b> | |
228 | <a class="sectref" rel="prev" href="module-dbm.html">8.6 dbm </A> | |
229 | <b class="navlabel">Up:</b> | |
230 | <a class="sectref" rel="parent" href="unix.html">8. Unix Specific Services</A> | |
231 | <b class="navlabel">Next:</b> | |
232 | <a class="sectref" rel="next" href="module-termios.html">8.8 termios </A> | |
233 | </div> | |
234 | </div> | |
235 | <hr /> | |
236 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
237 | </DIV> | |
238 | <!--End of Navigation Panel--> | |
239 | <ADDRESS> | |
240 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
241 | </ADDRESS> | |
242 | </BODY> | |
243 | </HTML> |