本文整理汇总了Python中elementtree.ElementTree.Element.attrib['type']方法的典型用法代码示例。如果您正苦于以下问题:Python Element.attrib['type']方法的具体用法?Python Element.attrib['type']怎么用?Python Element.attrib['type']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elementtree.ElementTree.Element
的用法示例。
在下文中一共展示了Element.attrib['type']方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: modify
# 需要导入模块: from elementtree.ElementTree import Element [as 别名]
# 或者: from elementtree.ElementTree.Element import attrib['type'] [as 别名]
def modify(self):
# self.node is an atom entry, see RFC4248 Section 4.1.2
########################################
# content elements RFC4248 Section 4.1.3
contents = self.entry.contents
if not (isinstance(contents, list) or isinstance(contents, tuple)):
contents = [contents]
for content in contents:
el = Element(self.namespace+"content")
# see RFC4248 Section 4.1.3.1 to 4.1.3.3
if content.get('body', None):
type = content.get('type', 'text')
if 'xhtml' in type:
el.attrib['type'] = 'xhtml'
applyAtomText(el, {'text':content.get('body'),
'type': 'xhtml'})
elif 'html' in type:
el.attrib['type'] = 'html'
applyAtomText(el, {'text':content.get('body'),
'type': 'html'})
elif '/plain' in type or type=='text':
el.attrib['type'] = 'text'
applyAtomText(el, {'text':content.get('body'),
'type': 'text'})
elif type.endswith('+xml') or type.endswith('/xml'):
el.attrib['type'] = type
el.text = content.get('body')
else: # base64 encode body
el.attrib['type'] = type
raise NotImplementedError, 'TODO: support base64.'
elif content.get('src', None):
type = content.get('type', 'application/octet-stream')
el.attrib['type'] = type
el.attrib['src'] = content.get('src')
self.node.append(el)
#######################################
# metadata elements RFC4248 Section 4.2
self.modifyMetadata(self.entry, self.node)
# link elements RFC4248 Section 4.2.7
if self.entry.webURL:
self.node.append(createLink(self.entry.webURL, 'alternate'))
# handle enclosures RFC4248 Section 4.2.7.2. The "rel" Attribute
if self.entry.enclosures:
for enclosure in self.entry.enclosures:
el = Element(self.namespace+"link")
el.attrib['rel'] = 'enclosure'
el.attrib['type'] = enclosure.mimetype
el.attrib['length'] = str(len(enclosure))
el.attrib['href'] = enclosure.url
self.node.append(el)
return [self.namespace, xhtmlns]
示例2: encode
# 需要导入模块: from elementtree.ElementTree import Element [as 别名]
# 或者: from elementtree.ElementTree.Element import attrib['type'] [as 别名]
def encode(key, value):
element = Element(key)
if isinstance(value, NoneType):
element.attrib['type'] = 'none'
elif isinstance(value, BooleanType):
element.attrib['type'] = 'bool'
element.attrib['value'] = value and 'true' or 'false'
elif isinstance(value, ComplexType):
element.attrib['type'] = 'complex'
element.attrib['re'] = repr(value.real)
element.attrib['im'] = repr(value.imag)
elif isinstance(value, FloatType):
element.attrib['type'] = 'float'
element.attrib['value'] = repr(value)
elif isinstance(value, IntType):
element.attrib['type'] = 'int'
element.attrib['value'] = repr(value)
elif isinstance(value, LongType):
element.attrib['type'] = 'long'
element.attrib['value'] = repr(value)
elif isinstance(value, StringType) or isinstance(value, UnicodeType):
element.attrib['type'] = 'cdata'
element.text = u"<![CDATA[%s]]>" % value
elif isinstance(value, DictType):
element.attrib['type'] = 'dict'
for key in value.keys():
element.append(encode(key, value[key]))
elif isinstance(value, ListType):
element.attrib['type'] = 'list'
for subvalue in value:
element.append(encode('value', subvalue))
elif isinstance(value, TupleType):
element.attrib['type'] = 'tuple'
for subvalue in value:
element.append(encode('value', subvalue))
else:
raise TypeError("Encoding of %s not supported (Key: %s)" % (repr(value), key))
return element