Add SchemaNode.__contains__ to support "name in schema".

This should be useful for conditional imperative manipulation of
existing schemas.  Note that this changes the behaviour of "in
schema", which used to be "does it contain this node instance" instead
of "does it contain a node with this name".  Maybe this was done
deliberately so.
This commit is contained in:
Daniel Nouri
2011-03-10 17:09:15 +01:00
parent fa4deaf294
commit 0c235e04db
2 changed files with 14 additions and 0 deletions

View File

@@ -1399,6 +1399,13 @@ class SchemaNode(object):
""" Iterate over the children nodes of this schema node """
return iter(self.children)
def __contains__(self, name):
try:
self[name]
except KeyError:
return False
return True
def __repr__(self):
return '<%s.%s object at %d (named %s)>' % (
self.__module__,

View File

@@ -1503,6 +1503,13 @@ class TestSchemaNode(unittest.TestCase):
it = node.__iter__()
self.assertEqual(list(it), ['a', 'b', 'c'])
def test___contains__(self):
node = self._makeOne(None)
another = self._makeOne(None, name='another')
node.add(another)
self.assertEquals('another' in node, True)
self.assertEquals('b' in node, False)
def test_clone(self):
inner_typ = DummyType()
outer_typ = DummyType()