def writefile():
"""
>>> elem = ET.Element("tag")
>>> elem.text = "text"
>>> serialize(elem)
'<tag>text</tag>'
>>> ET.SubElement(elem, "subtag").text = "subtext"
>>> serialize(elem)
'<tag>text<subtag>subtext</subtag></tag>'
Test tag suppression
>>> elem.tag = None
>>> serialize(elem)
'text<subtag>subtext</subtag>'
>>> elem.insert(0, ET.Comment("comment"))
>>> serialize(elem) # assumes 1.3
'text<!--comment--><subtag>subtext</subtag>'
>>> elem[0] = ET.PI("key", "value")
>>> serialize(elem)
'text<?key value?><subtag>subtext</subtag>'
"""
python类Comment()的实例源码
def bug_200709_element_comment():
"""
Not sure if this can be fixed, really (since the serializer needs
ET.Comment, not cET.comment).
>>> a = ET.Element('a')
>>> a.append(ET.Comment('foo'))
>>> a[0].tag == ET.Comment
True
>>> a = ET.Element('a')
>>> a.append(ET.PI('foo'))
>>> a[0].tag == ET.PI
True
"""
def writefile():
"""
>>> elem = ET.Element("tag")
>>> elem.text = "text"
>>> serialize(elem)
'<tag>text</tag>'
>>> ET.SubElement(elem, "subtag").text = "subtext"
>>> serialize(elem)
'<tag>text<subtag>subtext</subtag></tag>'
Test tag suppression
>>> elem.tag = None
>>> serialize(elem)
'text<subtag>subtext</subtag>'
>>> elem.insert(0, ET.Comment("comment"))
>>> serialize(elem) # assumes 1.3
'text<!--comment--><subtag>subtext</subtag>'
>>> elem[0] = ET.PI("key", "value")
>>> serialize(elem)
'text<?key value?><subtag>subtext</subtag>'
"""
def bug_200709_element_comment():
"""
Not sure if this can be fixed, really (since the serializer needs
ET.Comment, not cET.comment).
>>> a = ET.Element('a')
>>> a.append(ET.Comment('foo'))
>>> a[0].tag == ET.Comment
True
>>> a = ET.Element('a')
>>> a.append(ET.PI('foo'))
>>> a[0].tag == ET.PI
True
"""
def writefile():
"""
>>> elem = ET.Element("tag")
>>> elem.text = "text"
>>> serialize(elem)
'<tag>text</tag>'
>>> ET.SubElement(elem, "subtag").text = "subtext"
>>> serialize(elem)
'<tag>text<subtag>subtext</subtag></tag>'
Test tag suppression
>>> elem.tag = None
>>> serialize(elem)
'text<subtag>subtext</subtag>'
>>> elem.insert(0, ET.Comment("comment"))
>>> serialize(elem) # assumes 1.3
'text<!--comment--><subtag>subtext</subtag>'
>>> elem[0] = ET.PI("key", "value")
>>> serialize(elem)
'text<?key value?><subtag>subtext</subtag>'
"""
def bug_200709_element_comment():
"""
Not sure if this can be fixed, really (since the serializer needs
ET.Comment, not cET.comment).
>>> a = ET.Element('a')
>>> a.append(ET.Comment('foo'))
>>> a[0].tag == ET.Comment
True
>>> a = ET.Element('a')
>>> a.append(ET.PI('foo'))
>>> a[0].tag == ET.PI
True
"""
def __init__(self, nml_boiler=False):
self.comp_type = Element('ComponentType')
self.dynamics = Element('Dynamics')
if nml_boiler:
self.comp_type.set('extends', 'baseIonChannel')
self.comp_type.append(
Comment('The defs below are hardcoded for testing purposes!'))
SubElement(self.comp_type, 'Constant', attrib={
'dimension': 'voltage', 'name': 'MV', 'value': '1mV'})
SubElement(self.comp_type, 'Constant', attrib={
'dimension': 'time', 'name': 'MS', 'value': '1ms'})
SubElement(self.comp_type, 'Requirement', attrib={
'name': 'v', 'dimension': 'voltage'})
self.comp_type.append(Comment('End of hardcoded defs!'))
def writefile():
"""
>>> elem = ET.Element("tag")
>>> elem.text = "text"
>>> serialize(elem)
'<tag>text</tag>'
>>> ET.SubElement(elem, "subtag").text = "subtext"
>>> serialize(elem)
'<tag>text<subtag>subtext</subtag></tag>'
Test tag suppression
>>> elem.tag = None
>>> serialize(elem)
'text<subtag>subtext</subtag>'
>>> elem.insert(0, ET.Comment("comment"))
>>> serialize(elem) # assumes 1.3
'text<!--comment--><subtag>subtext</subtag>'
>>> elem[0] = ET.PI("key", "value")
>>> serialize(elem)
'text<?key value?><subtag>subtext</subtag>'
"""
def bug_200709_element_comment():
"""
Not sure if this can be fixed, really (since the serializer needs
ET.Comment, not cET.comment).
>>> a = ET.Element('a')
>>> a.append(ET.Comment('foo'))
>>> a[0].tag == ET.Comment
True
>>> a = ET.Element('a')
>>> a.append(ET.PI('foo'))
>>> a[0].tag == ET.PI
True
"""
def writefile():
"""
>>> elem = ET.Element("tag")
>>> elem.text = "text"
>>> serialize(elem)
'<tag>text</tag>'
>>> ET.SubElement(elem, "subtag").text = "subtext"
>>> serialize(elem)
'<tag>text<subtag>subtext</subtag></tag>'
Test tag suppression
>>> elem.tag = None
>>> serialize(elem)
'text<subtag>subtext</subtag>'
>>> elem.insert(0, ET.Comment("comment"))
>>> serialize(elem) # assumes 1.3
'text<!--comment--><subtag>subtext</subtag>'
>>> elem[0] = ET.PI("key", "value")
>>> serialize(elem)
'text<?key value?><subtag>subtext</subtag>'
"""
def bug_200709_element_comment():
"""
Not sure if this can be fixed, really (since the serializer needs
ET.Comment, not cET.comment).
>>> a = ET.Element('a')
>>> a.append(ET.Comment('foo'))
>>> a[0].tag == ET.Comment
True
>>> a = ET.Element('a')
>>> a.append(ET.PI('foo'))
>>> a[0].tag == ET.PI
True
"""
def add_entity_to_doc(doc_element, entity, n):
entityname = ET.SubElement(doc_element, "entity")
id = ET.SubElement(entityname, "id")
id.text = "t" + str(n)
entityname.append(ET.Comment(entity.text))
#id.text = str(len(doc_element.findall("entity")))
span = ET.SubElement(entityname, "span")
span.text = str(entity.dstart)+","+str(entity.dend)
type = ET.SubElement(entityname, "type")
type.texts = "EVENT"
propertiesname = ET.SubElement(entityname, "properties")
classs = ET.SubElement(propertiesname, "Class")
value = ET.SubElement(propertiesname, "Value")
####
propertiesname = ET.SubElement(entityname, "properties") #XML
classs = ET.SubElement(propertiesname, "Class") # XML
classs.text = "asdasd"#entity.tag
##
return doc_element
def add_relation_to_doc(doc_element, relation):
relationname = ET.SubElement(doc_element, "relation") #XML entity
id = ET.SubElement(relationname, "id") #XML id
id.text = convert_id(relation.pid)
# id.append(ET.Comment(relation.entities[0].text + "+" + relation.entities[1].text))
#id.text = str(len(doc_element.findall("entity"))) esta so comentado
type = ET.SubElement(relationname, "type") #XML
type.text = "TLINK"
# entity1 = ET.SubElement(relationname, "parentsType")
# entity1.text = "TemporalRelations"
propertiesname = ET.SubElement(relationname, "properties") #XML
source = ET.SubElement(propertiesname, "Source") # XML
source.text = convert_id(relation.entities[0].eid)
propertiesname.append(ET.Comment("Source:{}".format(relation.entities[0].text)))
t = ET.SubElement(propertiesname, "Type") # XML
t.text = "CONTAINS"
target = ET.SubElement(propertiesname, "Target") # XML
target.text = convert_id(relation.entities[1].eid)
propertiesname.append(ET.Comment("Target:{}".format(relation.entities[1].text)))
return doc_element
def map_node_to_class(self, node):
if isinstance(node, BaseET.ElementTree):
return nodes.Document
elif node.tag == BaseET.ProcessingInstruction:
return nodes.ProcessingInstruction
elif node.tag == BaseET.Comment:
return nodes.Comment
elif isinstance(node, ETAttribute):
return nodes.Attribute
elif isinstance(node, ElementTreeText):
if node.is_cdata:
return nodes.CDATA
else:
return nodes.Text
elif self._is_node_an_element(node):
return nodes.Element
raise exceptions.Xml4hImplementationBug(
'Unrecognized type for implementation node: %s' % node)
def summarize(elem):
if elem.tag == ET.Comment:
return "<Comment>"
return elem.tag
def bug_200709_iter_comment():
"""
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> comment_b = ET.Comment("TEST-b")
>>> b.append(comment_b)
>>> summarize_list(a.iter(ET.Comment))
['<Comment>']
"""
# --------------------------------------------------------------------
# reported on bugs.python.org
def summarize(elem):
if elem.tag == ET.Comment:
return "<Comment>"
return elem.tag
def bug_200709_iter_comment():
"""
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> comment_b = ET.Comment("TEST-b")
>>> b.append(comment_b)
>>> summarize_list(a.iter(ET.Comment))
['<Comment>']
"""
def summarize(elem):
if elem.tag == ET.Comment:
return "<Comment>"
return elem.tag
def bug_200709_iter_comment():
"""
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> comment_b = ET.Comment("TEST-b")
>>> b.append(comment_b)
>>> summarize_list(a.iter(ET.Comment))
['<Comment>']
"""
def set_text(element, content):
content = escape(content, entities={'\r\n': '<br />'}) # retain html tags in text
content = content.replace("\n", "<br />") # replace new line for *nix system
content = content.replace("<br />", "<br />\n") # add the line break in source to make it readable
# trick to add CDATA for element tree lib
element.append(Comment(' --><![CDATA[' + content.replace(']]>', ']]]]><![CDATA[>') + ']]><!-- '))
def comment(self, data):
self.start(XmlET.Comment, {})
self.data(data)
self.end(XmlET.Comment)
def summarize(elem):
if elem.tag == ET.Comment:
return "<Comment>"
return elem.tag
def bug_200709_iter_comment():
"""
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> comment_b = ET.Comment("TEST-b")
>>> b.append(comment_b)
>>> summarize_list(a.iter(ET.Comment))
['<Comment>']
"""
def summarize(elem):
if elem.tag == ET.Comment:
return "<Comment>"
return elem.tag
def bug_200709_iter_comment():
"""
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> comment_b = ET.Comment("TEST-b")
>>> b.append(comment_b)
>>> summarize_list(a.iter(ET.Comment))
['<Comment>']
"""
# --------------------------------------------------------------------
# reported on bugs.python.org
def comment(self, data):
self.start(ET.Comment, {})
self.data(data)
self.end(ET.Comment)
def test_wrap_node_and_is_type_methods(self):
# Wrap root element
wrapped_node = self.adapter_class.wrap_node(self.root_elem, self.doc)
self.assertEqual(self.root_elem, wrapped_node.impl_node)
self.assertEqual('DocRoot', wrapped_node.name)
self.assertEqual(self.doc, wrapped_node.impl_document)
self.assertTrue(wrapped_node.is_type(xml4h.nodes.ELEMENT_NODE))
self.assertTrue(wrapped_node.is_element)
# Wrap a non-root element
wrapped_node = self.adapter_class.wrap_node(self.elem3_second, self.doc)
self.assertEqual(self.elem3_second, wrapped_node.impl_node)
self.assertEqual('ns2:Element3', wrapped_node.name)
self.assertEqual('Element4', wrapped_node.parent.name)
self.assertTrue(wrapped_node.is_type(xml4h.nodes.ELEMENT_NODE))
self.assertTrue(wrapped_node.is_element)
# Test node types
wrapped_node = self.adapter_class.wrap_node(self.text_node, self.doc)
self.assertIsInstance(wrapped_node, xml4h.nodes.Text)
self.assertTrue(wrapped_node.is_type(xml4h.nodes.TEXT_NODE))
self.assertTrue(wrapped_node.is_text)
wrapped_node = self.adapter_class.wrap_node(self.cdata_node, self.doc)
self.assertIsInstance(wrapped_node, xml4h.nodes.CDATA)
self.assertTrue(wrapped_node.is_type(xml4h.nodes.CDATA_NODE))
self.assertTrue(wrapped_node.is_cdata)
wrapped_node = self.adapter_class.wrap_node(self.comment_node, self.doc)
self.assertIsInstance(wrapped_node, xml4h.nodes.Comment)
self.assertTrue(wrapped_node.is_type(xml4h.nodes.COMMENT_NODE))
self.assertTrue(wrapped_node.is_comment)
wrapped_node = self.adapter_class.wrap_node(
self.instruction_node, self.doc)
self.assertIsInstance(
wrapped_node, xml4h.nodes.ProcessingInstruction)
self.assertTrue(
wrapped_node.is_type(xml4h.nodes.PROCESSING_INSTRUCTION_NODE))
self.assertTrue(wrapped_node.is_processing_instruction)
def new_impl_comment(self, text):
return self.ET.Comment(text)
def get_node_value(self, node):
if node.tag == BaseET.ProcessingInstruction:
name, target = node.text.split(' ')
return target
elif node.tag == BaseET.Comment:
return node.text
elif hasattr(node, 'value'):
return node.value
else:
return node.text