]> granicus.if.org Git - python/commitdiff
Add example. Should I propagate this example to all the other DBM-ish modules, too?
authorAndrew M. Kuchling <amk@amk.ca>
Fri, 28 Jul 2006 12:48:07 +0000 (12:48 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Fri, 28 Jul 2006 12:48:07 +0000 (12:48 +0000)
Doc/lib/libanydbm.tex

index 17228dd284556a37e44d1529a6b1a523fe57fee8..badc6ecfc23e7fd404e1563b63150536b905f926 100644 (file)
@@ -46,6 +46,32 @@ be stored, retrieved, and deleted, and the \method{has_key()} and
 \method{keys()} methods are available.  Keys and values must always be
 strings.
 
+The following example records some hostnames and a corresponding title, 
+and then prints out the contents of the database:
+
+\begin{verbatim}
+import anydbm
+
+# Open database, creating it if necessary.
+db = anydbm.open('cache', 'c')
+
+# Record some values
+db['www.python.org'] = 'Python Website'
+db['www.cnn.com'] = 'Cable News Network'
+
+# Loop through contents.  Other dictionary methods
+# such as .keys(), .values() also work.
+for k, v in db.iteritems():
+    print k, '\t', v
+
+# Storing a non-string key or value will raise an exception (most
+# likely a TypeError).
+db['www.yahoo.com'] = 4
+
+# Close when done.
+db.close()
+\end{verbatim}
+
 
 \begin{seealso}
   \seemodule{dbhash}{BSD \code{db} database interface.}