make DictRow more compatible with dict

Marko Kreen <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Attached patch adds following dict methods to DictRow
that were missing:

  .iterkeys()
  .itervalues()
  .copy()
  .__contains__()

The .copy() is especially interesting - it returns real dict,
that can be now also modified as dict.

Adding them is safe, except maybe __contains__() as it changes
the meaning of "x in row".  Previously it checked if x appears in
any of columns, now it checks if column exists.  As I cannot imagine
why would anyone want to do the former, I don't see any reason
to keep the old behaviour.

-- 
marko

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
better.dictrow.diff (text/x-patch, 607 B)
=== modified file 'lib/extras.py'
--- lib/extras.py	2009-03-02 10:07:17 +0000
+++ lib/extras.py	2009-04-15 11:14:59 +0000
@@ -141,6 +141,17 @@
         for n, v in self._index.items():
             yield n, list.__getitem__(self, v)
 
+    def iterkeys(self):
+        return self._index.iterkeys()
+
+    def itervalues(self):
+        return list.__iter__(self)
+
+    def copy(self):
+        return dict(self.items())
+
+    def __contains__(self, x):
+        return self._index.__contains__(x)
 
 class RealDictConnection(_connection):
     """A connection that uses RealDictCursor automatically."""
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.