diff --git a/ChangeLog b/ChangeLog index a9c15313..a78024f3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2010-12-01 Daniele Varrazzo + + * lib/extras.py: DictRow items can be updated. Patch by Alex Aster. + 2010-11-28 Daniele Varrazzo * Cancel patch from Jan integrated. diff --git a/lib/extras.py b/lib/extras.py index 4439324b..09142ed6 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -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(): diff --git a/tests/extras_dictcursor.py b/tests/extras_dictcursor.py index b4ad2f76..c2bacb0b 100644 --- a/tests/extras_dictcursor.py +++ b/tests/extras_dictcursor.py @@ -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):