comparison emeraldtree/ElementTree.py @ 39:5784bad989d1

ElementTree - Don't use dict and list as argument default value
author Bastian Blank <bblank@thinkmo.de>
date Mon, 21 Jul 2008 12:43:20 +0200
parents 6e38bcef6030
children f9fe97f25af0
comparison
equal deleted inserted replaced
38:6e38bcef6030 39:5784bad989d1
181 181
182 @property 182 @property
183 def tail(self): 183 def tail(self):
184 raise RuntimeError('The tail argument is not supported') 184 raise RuntimeError('The tail argument is not supported')
185 185
186 def __init__(self, tag, attrib={}, children=[], **extra): 186 def __init__(self, tag, attrib=None, children=(), **extra):
187 attrib = attrib.copy() 187 attrib = attrib and attrib.copy() or {}
188 attrib.update(extra) 188 attrib.update(extra)
189 self.tag = tag 189 self.tag = tag
190 self.attrib = attrib 190 self.attrib = attrib
191 self._children = children[:] 191 self._children = list(children)
192 192
193 def __repr__(self): 193 def __repr__(self):
194 return "<Element %s at %x>" % (repr(self.tag), id(self)) 194 return "<Element %s at %x>" % (repr(self.tag), id(self))
195 195
196 ## 196 ##
431 # @param attrib An optional dictionary, containing element attributes. 431 # @param attrib An optional dictionary, containing element attributes.
432 # @param **extra Additional attributes, given as keyword arguments. 432 # @param **extra Additional attributes, given as keyword arguments.
433 # @return An element instance. 433 # @return An element instance.
434 # @defreturn Element 434 # @defreturn Element
435 435
436 def SubElement(parent, tag, attrib={}, **extra): 436 def SubElement(parent, tag, attrib=None, **extra):
437 attrib = attrib.copy() 437 attrib = attrib and attrib.copy() or {}
438 attrib.update(extra) 438 attrib.update(extra)
439 element = parent.makeelement(tag, attrib) 439 element = parent.makeelement(tag, attrib)
440 parent.append(element) 440 parent.append(element)
441 return element 441 return element
442 442
659 # keyword arguments 659 # keyword arguments
660 encoding="us-ascii", 660 encoding="us-ascii",
661 xml_declaration=None, 661 xml_declaration=None,
662 default_namespace=None, 662 default_namespace=None,
663 method=None, 663 method=None,
664 namespaces={}): 664 namespaces=None):
665 assert self._root is not None 665 assert self._root is not None
666 if not hasattr(file, "write"): 666 if not hasattr(file, "write"):
667 file = open(file, "wb") 667 file = open(file, "wb")
668 write = file.write 668 write = file.write
669 if not method: 669 if not method:
699 # maps qnames to *encoded* prefix:local names 699 # maps qnames to *encoded* prefix:local names
700 qnames = {None: None} 700 qnames = {None: None}
701 701
702 # maps uri:s to prefixes 702 # maps uri:s to prefixes
703 candidate_namespaces = _namespace_map.copy() 703 candidate_namespaces = _namespace_map.copy()
704 candidate_namespaces.update(namespaces) 704 if namespaces:
705 candidate_namespaces.update(namespaces)
705 if default_namespace: 706 if default_namespace:
706 candidate_namespaces[default_namespace] = "" 707 candidate_namespaces[default_namespace] = ""
707 used_namespaces = {} 708 used_namespaces = {}
708 709
709 def encode(text): 710 def encode(text):
719 uri = qname.uri 720 uri = qname.uri
720 prefix = used_namespaces.get(uri, None) 721 prefix = used_namespaces.get(uri, None)
721 if prefix is None: 722 if prefix is None:
722 prefix = candidate_namespaces.get(uri, None) 723 prefix = candidate_namespaces.get(uri, None)
723 if prefix is None: 724 if prefix is None:
724 prefix = "ns%d" % len(namespaces) 725 prefix = "ns%d" % len(used_namespaces)
725 if prefix != "xml": 726 if prefix != "xml":
726 used_namespaces[uri] = prefix 727 used_namespaces[uri] = prefix
727 if prefix: 728 if prefix:
728 qnames[qname] = encode("%s:%s" % (prefix, qname.name)) 729 qnames[qname] = encode("%s:%s" % (prefix, qname.name))
729 else: 730 else: