MCPcopy Create free account
hub / github.com/ActiveState/code / __new__

Method __new__

recipes/Python/576445_ElementTree_Wrapper/recipe-576445.py:66–97  ·  view source on GitHub ↗
(cls,tag,thing = None,*args,**kwargs)

Source from the content-addressed store, hash-verified

64 print xml(e)
65 """
66 def __new__(cls,tag,thing = None,*args,**kwargs):
67 if hasattr(tag,'__xml__'):
68 return tag.__xml__()
69 self = object.__new__(xml)
70 if cElementTree.iselement(tag):
71 self.__content = tag
72 elif isinstance(tag,cElementTree.ElementTree):
73 self.__content = tag.getroot()
74 elif is_file(tag):
75 self.__content = cElementTree.parse(tag).getroot()
76 elif isinstance(tag,str) and len(tag) > 0 and tag[0] == '<':
77 self.__content = cElementTree.fromstring(tag)
78 else:
79 if type(tag) != str:
80 raise TypeError("Cannot convert %s object to xml" % str(type(tag)))
81 self.__content = cElementTree.fromstring('<%s/>' % tag)
82 if is_text(thing) or type(thing) == int:
83 self.__content.text = text(thing)
84 elif thing != None:
85 self.append(xml(thing))
86 for subthing in args:
87 self.append(xml(subthing))
88 for key,value in kwargs.items():
89 if key == '__class' or key == 'klass':
90 self['class'] = value
91 else:
92 self[key] = value
93 if '{' in self.__content.tag:
94 self.__prefix = PREFIX_PAT.search(self.__content.tag).groups()[0]
95 else:
96 self.__prefix = ''
97 return self
98 def clear(self):
99 self.__content.clear()
100 def append(self,xml_obj):

Callers

nothing calls this directly

Calls 12

appendMethod · 0.95
strFunction · 0.85
is_textFunction · 0.85
getrootMethod · 0.80
is_fileFunction · 0.70
textFunction · 0.70
xmlClass · 0.70
__xml__Method · 0.45
parseMethod · 0.45
fromstringMethod · 0.45
itemsMethod · 0.45
searchMethod · 0.45

Tested by

no test coverage detected