DictRow items can be updated. Patch by Alex Aster.

This commit is contained in:
Daniele Varrazzo 2010-12-01 13:17:12 +00:00
parent 6f2d40405e
commit 0ad7483a2b
3 changed files with 16 additions and 0 deletions

View File

@ -1,3 +1,7 @@
2010-12-01 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* lib/extras.py: DictRow items can be updated. Patch by Alex Aster.
2010-11-28 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* Cancel patch from Jan integrated.

View File

@ -144,6 +144,11 @@ class DictRow(list):
x = self._index[x]
return list.__getitem__(self, x)
def __setitem__(self, x, v):
if type(x) != int:
x = self._index[x]
list.__setitem__(self, x, v)
def items(self):
res = []
for n, v in self._index.items():

View File

@ -85,6 +85,7 @@ class ExtrasDictCursorTests(unittest.TestCase):
row = getter(curs)
self.failUnless(row['foo'] == 'bar')
self.failUnless(row[0] == 'bar')
return row
def _testWithNamedCursor(self, getter):
curs = self.conn.cursor('aname', cursor_factory=psycopg2.extras.DictCursor)
@ -105,6 +106,12 @@ class ExtrasDictCursorTests(unittest.TestCase):
row = getter(curs)
self.failUnless(row['foo'] == 'bar')
def testUpdateRow(self):
row = self._testWithPlainCursor(lambda curs: curs.fetchone())
row['foo'] = 'qux'
self.failUnless(row['foo'] == 'qux')
self.failUnless(row[0] == 'qux')
def if_has_namedtuple(f):
def if_has_namedtuple_(self):