diff --git a/CHANGES.txt b/CHANGES.txt index 7b82b47..f532369 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -11,6 +11,12 @@ Unreleased - ``unflatten`` is implemented. +- Added ``__setitem__`` to ``SchemaNode``, allowing replacement of nodes by + name. + +- Added ``get_value`` and ``set_value`` methods to ``Schema`` which allow + access and mutation of appstructs using dotted name paths. + 0.9.3 (2011-06-23) ------------------ diff --git a/colander/__init__.py b/colander/__init__.py index 247a44c..237caac 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1640,6 +1640,15 @@ class SchemaNode(object): return node raise KeyError(name) + def __setitem__(self, name, newnode): + """ Replace a subnode by name """ + for idx, node in enumerate(self.children[:]): + if node.name == name: + self.children[idx] = newnode + newnode.name = name + return node + raise KeyError(name) + def __iter__(self): """ Iterate over the children nodes of this schema node """ return iter(self.children) diff --git a/colander/tests.py b/colander/tests.py index 444acd6..daf2e6e 100644 --- a/colander/tests.py +++ b/colander/tests.py @@ -1935,6 +1935,19 @@ class TestSchemaNode(unittest.TestCase): node = self._makeOne(None) self.assertRaises(KeyError, node.__delitem__, 'another') + def test___setitem__success(self): + node = self._makeOne(None) + another = self._makeOne(None, name='another') + node.add(another) + andanother = self._makeOne(None, name='andanother') + node['another'] = andanother + self.assertEqual(node['another'], andanother) + self.assertEqual(andanother.name, 'another') + + def test___setitem__failure(self): + node = self._makeOne(None) + self.assertRaises(KeyError, node.__setitem__, 'another', None) + def test___iter__(self): node = self._makeOne(None) node.children = ['a', 'b', 'c']