From 0ad7483a2bff6dd76843b31aa592877d96d6ea39 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 1 Dec 2010 13:17:12 +0000 Subject: [PATCH] DictRow items can be updated. Patch by Alex Aster. --- ChangeLog | 4 ++++ lib/extras.py | 5 +++++ tests/extras_dictcursor.py | 7 +++++++ 3 files changed, 16 insertions(+) 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):