Commit deadbf50 authored by Raymond Hettinger's avatar Raymond Hettinger

SF #662923

Add support for the iterator and mapping protocols.
For Py2.3, this was done for shelve, dumbdbm and other mapping objects, but
not for bsddb and dbhash which were inadvertently missed.
parent 74c8e55f
...@@ -100,8 +100,9 @@ systems which ship with the old Berkeley DB 1.85 database library. The ...@@ -100,8 +100,9 @@ systems which ship with the old Berkeley DB 1.85 database library. The
\subsection{Hash, BTree and Record Objects \label{bsddb-objects}} \subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
Once instantiated, hash, btree and record objects support the following Once instantiated, hash, btree and record objects support
methods: the same methods as dictionaries. In addition, they support
the following methods:
\begin{methoddesc}{close}{} \begin{methoddesc}{close}{}
Close the underlying file. The object can no longer be accessed. Since Close the underlying file. The object can no longer be accessed. Since
...@@ -177,6 +178,20 @@ Example: ...@@ -177,6 +178,20 @@ Example:
('2', '4') ('2', '4')
>>> db.previous() >>> db.previous()
('1', '1') ('1', '1')
>>> for k, v in db.iteritems():
... print k, v
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
>>> 8 in db
True
>>> db.sync() >>> db.sync()
0 0
\end{verbatim} \end{verbatim}
...@@ -51,23 +51,23 @@ This module provides an exception and a function: ...@@ -51,23 +51,23 @@ This module provides an exception and a function:
\subsection{Database Objects \label{dbhash-objects}} \subsection{Database Objects \label{dbhash-objects}}
The database objects returned by \function{open()} provide the methods The database objects returned by \function{open()} provide the methods
common to all the DBM-style databases. The following methods are common to all the DBM-style databases and mapping objects. The following
available in addition to the standard methods. methods are available in addition to the standard methods.
\begin{methoddesc}[dbhash]{first}{} \begin{methoddesc}[dbhash]{first}{}
It's possible to loop over every key in the database using this method It's possible to loop over every key/value pair in the database using
and the \method{next()} method. The traversal is ordered by this method and the \method{next()} method. The traversal is ordered by
the databases internal hash values, and won't be sorted by the key the databases internal hash values, and won't be sorted by the key
values. This method returns the starting key. values. This method returns the starting key.
\end{methoddesc} \end{methoddesc}
\begin{methoddesc}[dbhash]{last}{} \begin{methoddesc}[dbhash]{last}{}
Return the last key in a database traversal. This may be used to Return the last key/value pair in a database traversal. This may be used to
begin a reverse-order traversal; see \method{previous()}. begin a reverse-order traversal; see \method{previous()}.
\end{methoddesc} \end{methoddesc}
\begin{methoddesc}[dbhash]{next}{} \begin{methoddesc}[dbhash]{next}{}
Returns the key next key in a database traversal. The Returns the key next key/value pair in a database traversal. The
following code prints every key in the database \code{db}, without following code prints every key in the database \code{db}, without
having to create a list in memory that contains them all: having to create a list in memory that contains them all:
...@@ -79,7 +79,7 @@ for i in xrange(1, len(d)): ...@@ -79,7 +79,7 @@ for i in xrange(1, len(d)):
\end{methoddesc} \end{methoddesc}
\begin{methoddesc}[dbhash]{previous}{} \begin{methoddesc}[dbhash]{previous}{}
Returns the previous key in a forward-traversal of the database. Returns the previous key/value pair in a forward-traversal of the database.
In conjunction with \method{last()}, this may be used to implement In conjunction with \method{last()}, this may be used to implement
a reverse-order traversal. a reverse-order traversal.
\end{methoddesc} \end{methoddesc}
......
...@@ -52,8 +52,9 @@ error = db.DBError # So bsddb.error will mean something... ...@@ -52,8 +52,9 @@ error = db.DBError # So bsddb.error will mean something...
#---------------------------------------------------------------------- #----------------------------------------------------------------------
import UserDict
class _DBWithCursor: class _DBWithCursor(UserDict.DictMixin):
""" """
A simple wrapper around DB that makes it look like the bsddbobject in A simple wrapper around DB that makes it look like the bsddbobject in
the old module. It uses a cursor as needed to provide DB traversal. the old module. It uses a cursor as needed to provide DB traversal.
...@@ -144,6 +145,14 @@ class _DBWithCursor: ...@@ -144,6 +145,14 @@ class _DBWithCursor:
self._checkOpen() self._checkOpen()
return self.db.sync() return self.db.sync()
def __iter__(self):
try:
yield self.first()[0]
next = self.next
while 1:
yield next()[0]
except _bsddb.DBNotFoundError:
return
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# Compatibility object factory functions # Compatibility object factory functions
......
...@@ -20,12 +20,24 @@ def test(openmethod, what, ondisk=1): ...@@ -20,12 +20,24 @@ def test(openmethod, what, ondisk=1):
verify(f.keys() == []) verify(f.keys() == [])
if verbose: if verbose:
print 'creation...' print 'creation...'
f['0'] = '' keys = ['0', 'a', 'b', 'c', 'd', 'e', 'f']
f['a'] = 'Guido' values = ['', 'Guido', 'van', 'Rossum', 'invented', 'Python']
f['b'] = 'van' items = zip(keys, values)
f['c'] = 'Rossum' for k, v in items:
f['d'] = 'invented' f[k] = v
f['f'] = 'Python'
# test mapping iteration methods
from sets import Set
def verifyset(s1, s2):
verify(Set(s1) == Set(s2))
verify(keys, f.keys())
verify(values, f.values())
verify(items, f.items())
verify(keys, f)
verify(keys, f.iterkeys())
verify(values, f.itervalues())
verify(items, f.iteritems())
if verbose: if verbose:
print '%s %s %s' % (f['a'], f['b'], f['c']) print '%s %s %s' % (f['a'], f['b'], f['c'])
......
...@@ -35,6 +35,10 @@ Extension modules ...@@ -35,6 +35,10 @@ Extension modules
Library Library
------- -------
- The bsddb module and dbhash module now support the iterator and
mapping protocols which make them more substitutable for dictionaries
and shelves.
- The csv module's DictReader and DictWriter classes now accept keyword - The csv module's DictReader and DictWriter classes now accept keyword
arguments. This was an omission in the initial implementation. arguments. This was an omission in the initial implementation.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment