comparison emeraldtree/tree.py @ 75:d9ac9fe47760

Tree - Allow all objects in tree
author Bastian Blank <bblank@thinkmo.de>
date Tue, 08 Jun 2010 22:28:35 +0200
parents a271e50bea43
children 2175b8941596
comparison
equal deleted inserted replaced
74:a271e50bea43 75:d9ac9fe47760
148 else: 148 else:
149 attrib = {} 149 attrib = {}
150 attrib.update(extra) 150 attrib.update(extra)
151 self.tag = tag 151 self.tag = tag
152 self.attrib = attrib 152 self.attrib = attrib
153 self._children = [self._check_node(i) for i in children] 153 self._children = list(children)
154 154
155 def __repr__(self): 155 def __repr__(self):
156 return "<Element %s at %x>" % (repr(self.tag), id(self)) 156 return "<Element %s at %x>" % (repr(self.tag), id(self))
157 157
158 ## 158 ##
183 # @param element The new element value. 183 # @param element The new element value.
184 # @exception IndexError If the given element does not exist. 184 # @exception IndexError If the given element does not exist.
185 # @exception AssertionError If element is not a valid object. 185 # @exception AssertionError If element is not a valid object.
186 186
187 def __setitem__(self, index, element): 187 def __setitem__(self, index, element):
188 if isinstance(index, slice):
189 element = [self._check_node(i) for i in element]
190 else:
191 element = self._check_node(element)
192 self._children.__setitem__(index, element) 188 self._children.__setitem__(index, element)
193 189
194 ## 190 ##
195 # Deletes the given subelement. 191 # Deletes the given subelement.
196 # 192 #
198 # @exception IndexError If the given element does not exist. 194 # @exception IndexError If the given element does not exist.
199 195
200 def __delitem__(self, index): 196 def __delitem__(self, index):
201 self._children.__delitem__(index) 197 self._children.__delitem__(index)
202 198
203 @staticmethod
204 def _check_node(node):
205 if isinstance(node, (Node, unicode)):
206 return node
207 if isinstance(node, str):
208 return unicode(node)
209 raise TypeError
210
211 ## 199 ##
212 # Adds a subelement to the end of this element. 200 # Adds a subelement to the end of this element.
213 # 201 #
214 # @param element The element to add. 202 # @param element The element to add.
215 # @exception AssertionError If a sequence member is not a valid object. 203 # @exception AssertionError If a sequence member is not a valid object.
216 204
217 def append(self, element): 205 def append(self, element):
218 element = self._check_node(element)
219 self._children.append(element) 206 self._children.append(element)
220 207
221 ## 208 ##
222 # Appends subelements from a sequence. 209 # Appends subelements from a sequence.
223 # 210 #
224 # @param elements A sequence object with zero or more elements. 211 # @param elements A sequence object with zero or more elements.
225 # @exception AssertionError If a subelement is not a valid object. 212 # @exception AssertionError If a subelement is not a valid object.
226 # @since 1.3 213 # @since 1.3
227 214
228 def extend(self, elements): 215 def extend(self, elements):
229 elements = [self._check_node(i) for i in elements]
230 self._children.extend(elements) 216 self._children.extend(elements)
231 217
232 ## 218 ##
233 # Inserts a subelement at the given position in this element. 219 # Inserts a subelement at the given position in this element.
234 # 220 #
235 # @param index Where to insert the new subelement. 221 # @param index Where to insert the new subelement.
236 # @exception AssertionError If the element is not a valid object. 222 # @exception AssertionError If the element is not a valid object.
237 223
238 def insert(self, index, element): 224 def insert(self, index, element):
239 element = self._check_node(element)
240 self._children.insert(index, element) 225 self._children.insert(index, element)
241 226
242 ## 227 ##
243 # Removes a matching subelement. Unlike the <b>find</b> methods, 228 # Removes a matching subelement. Unlike the <b>find</b> methods,
244 # this method compares elements based on identity, not on tag 229 # this method compares elements based on identity, not on tag