changeset 23:4c1912ff58a3

Support initialization with children. * emeraldtree/ElementTree.py - Add children argument to Element.__init__. - Style fixes. * emeraldtree/tests/test_ElementTree.py - Use children init. - Check for unicode.
author Bastian Blank <bblank@thinkmo.de>
date Fri, 06 Jun 2008 21:57:12 +0200
parents 0aa00b020bc1
children 2015d11467bc
files emeraldtree/ElementTree.py emeraldtree/tests/test_ElementTree.py
diffstat 2 files changed, 10 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/emeraldtree/ElementTree.py	Fri Jun 06 20:56:42 2008 +0200
+++ b/emeraldtree/ElementTree.py	Fri Jun 06 21:57:12 2008 +0200
@@ -183,12 +183,12 @@
     def tail(self):
         raise RuntimeError('The tail argument is not supported')
 
-    def __init__(self, tag, attrib={}, **extra):
+    def __init__(self, tag, attrib={}, children=[], **extra):
         attrib = attrib.copy()
         attrib.update(extra)
         self.tag = tag
         self.attrib = attrib
-        self._children = []
+        self._children = children[:]
 
     def __repr__(self):
         return "<Element %s at %x>" % (repr(self.tag), id(self))
@@ -475,7 +475,7 @@
 # @return An opaque object, representing the QName.
 
 class QName(object):
-    def __init__(self, text, uri = None):
+    def __init__(self, text, uri=None):
         if text[0] == '{':
             if uri is not None:
                 raise ValueError
--- a/emeraldtree/tests/test_ElementTree.py	Fri Jun 06 20:56:42 2008 +0200
+++ b/emeraldtree/tests/test_ElementTree.py	Fri Jun 06 21:57:12 2008 +0200
@@ -15,14 +15,12 @@
     assert elem
 
 def test_Element__len__():
-    elem = Element('a')
-    elem._children = range(10)
+    elem = Element('a', children = range(10))
 
     assert len(elem) == 10
 
 def test_Element___getitem__():
-    elem = Element('a')
-    elem._children = [Element('b'), Element('c')]
+    elem = Element('a', children = [Element('b'), Element('c')])
 
     assert elem[0].tag == 'b'
     assert elem[1].tag == 'c'
@@ -35,8 +33,7 @@
     assert elem[:2][1].tag == 'c'
 
 def test_Element___setitem__():
-    elem = Element('a')
-    elem._children = [Element('b1'), Element('b2')]
+    elem = Element('a', children = [Element('b1'), Element('b2')])
 
     elem[0] = Element('c')
     assert elem[0].tag == 'c'
@@ -52,8 +49,7 @@
     assert elem[2].tag == 'd'
 
 def test_Element___delitem__():
-    elem = Element('a')
-    elem._children = [Element('b1'), Element('b2')]
+    elem = Element('a', children = [Element('b1'), Element('b2')])
 
     del elem[0]
     assert len(elem) == 1
@@ -149,7 +145,10 @@
 def test_XMLParser_text1():
     elem = XML('<a>b</a>')
     assert elem.tag == 'a'
+    assert isinstance(elem, Element)
     assert len(elem) == 1
+    assert elem[0] == 'b'
+    assert isinstance(elem[0], unicode)
 
 def test_XMLParser_text2():
     elem = XML('<a>b<c>d</c>d</a>')