changeset 76:2175b8941596

Tree - Add element only iterator
author Bastian Blank <bblank@thinkmo.de>
date Thu, 10 Jun 2010 14:39:36 +0200
parents d9ac9fe47760
children f7db63ba09f4
files emeraldtree/tests/test_tree.py emeraldtree/tree.py
diffstat 2 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/emeraldtree/tests/test_tree.py	Tue Jun 08 22:28:35 2010 +0200
+++ b/emeraldtree/tests/test_tree.py	Thu Jun 10 14:39:36 2010 +0200
@@ -303,6 +303,16 @@
     l = list(elem.itertext())
     assert len(l) == 1
 
+def test_Element_iter_elements():
+    elem = Element(u'a')
+    assert len(list(elem.iter_elements())) == 0
+
+    elem.append(Element(u'c'))
+    assert len(list(elem.iter_elements())) == 1
+
+    elem.append(u'b')
+    assert len(list(elem.iter_elements())) == 1
+
 def test_Comment():
     elem = Comment(u'a')
     assert serialize(elem) == u'<!--a-->'
--- a/emeraldtree/tree.py	Tue Jun 08 22:28:35 2010 +0200
+++ b/emeraldtree/tree.py	Thu Jun 10 14:39:36 2010 +0200
@@ -366,6 +366,14 @@
             elif isinstance(e, basestring):
                 yield e
 
+    def iter_elements(self):
+        """
+        Creates an interator over all direct element children.
+        """
+        for child in self._children:
+            if child.__class__ is Element:
+                yield child
+
 
 ##
 # Subelement factory.  This function creates an element instance, and