本文整理汇总了Python中xml.etree.ElementTree.SubElement.attrib["id"]方法的典型用法代码示例。如果您正苦于以下问题:Python SubElement.attrib["id"]方法的具体用法?Python SubElement.attrib["id"]怎么用?Python SubElement.attrib["id"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.ElementTree.SubElement
的用法示例。
在下文中一共展示了SubElement.attrib["id"]方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generateXML
# 需要导入模块: from xml.etree.ElementTree import SubElement [as 别名]
# 或者: from xml.etree.ElementTree.SubElement import attrib["id"] [as 别名]
def generateXML(node, interxml):
newelement = None
for child in node.getchildren():
#first read the object tag and pull the properties from there
if child.tag == "object":
print "found new %s %s" %(GLADE_NAMES[child.attrib["class"]], child.attrib["id"])
newelement = SubElement(interxml, GLADE_NAMES[child.attrib["class"]])
newelement.attrib["id"] = child.attrib["id"]
for prop in child.getchildren():
if prop.tag == "property":
newelement.attrib[prop.attrib["name"]] = prop.text
print "adding new property %s" %prop.attrib["name"]
elif prop.tag == "child":
anotherelement = generateXML(prop, newelement)
if newelement is not None:
for child in node.getchildren():
#then loop again and grab the packing tag
if child.tag == "packing":
for prop in child.getchildren():
print "adding new property %s" %prop.attrib["name"]
newelement.attrib[prop.attrib["name"]] = prop.text
return newelement
示例2: testLoadFactory
# 需要导入模块: from xml.etree.ElementTree import SubElement [as 别名]
# 或者: from xml.etree.ElementTree.SubElement import attrib["id"] [as 别名]
def testLoadFactory(self):
element = Element("source")
element.attrib["id"] = "1"
element.attrib["type"] = "pitivi.factories.test.VideoTestSourceFactory"
element.attrib["duration"] = 5 * gst.SECOND
element.attrib["default_duration"] = 5 * gst.SECOND
output_streams = SubElement(element, "output-streams")
output_stream = SubElement(output_streams, "stream")
caps = gst.Caps("video/x-raw-yuv")
output_stream.attrib["id"] = "1"
output_stream.attrib["type"] = "pitivi.stream.VideoStream"
output_stream.attrib["caps"] = str(caps)
factory = self.formatter._loadFactory(element)
self.failUnless(isinstance(factory, VideoTestSourceFactory))
self.failUnlessEqual(len(factory.output_streams), 2)
self.failUnlessEqual(self.formatter._context.factories["1"], factory)
self.failUnlessEqual(factory.duration, 5 * gst.SECOND)
self.failUnlessEqual(factory.default_duration, 5 * gst.SECOND)
示例3: Browse
# 需要导入模块: from xml.etree.ElementTree import SubElement [as 别名]
# 或者: from xml.etree.ElementTree.SubElement import attrib["id"] [as 别名]
def Browse(self, request, objectID, browseFlag, filter, startingIndex, requestedCount, sortCriteria):
# determine the host:port of content
host = request.headers.getHeader('host').split(':',1)[0]
port = CoreHttpConfig.HTTP_PORT
# break up the objectID into segments. objectIDs have the following form:
# 0/<artist>/<album>/<song>
segments = objectID.split('/')
if len(segments) < 1 or segments[0] != '0':
raise UPNPError(701, "ObjectID %i is invalid" % objectID)
# generate the DIDL envelope
didl = Element("DIDL-Lite")
didl.attrib["xmlns"] = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
didl.attrib["xmlns:upnp"] = "urn:schemas-upnp-org:metadata-1-0/upnp/"
didl.attrib["xmlns:dc"] = "http://purl.org/dc/elements/1.1/"
# browse the metadata of one specific item
if browseFlag == 'BrowseMetadata':
if len(segments) == 1:
container = SubElement(didl, 'container')
container.attrib['id'] = objectID
container.attrib['parentID'] = '-1'
container.attrib['restricted'] = '1'
container.attrib['childCount'] = '2'
SubElement(container, 'dc:title').text = 'Media on Higgins'
SubElement(container, 'upnp:class').text = 'object.container.storageFolder'
elif len(segments) > 1:
if segments[1] == 'music':
if len(segments) == 2:
container = SubElement(didl, 'container')
container.attrib['id'] = objectID
container.attrib['parentID'] = '0'
container.attrib['restricted'] = '1'
container.attrib['childCount'] = str(len(Artist.objects.all()))
SubElement(container, 'dc:title').text = 'Music'
SubElement(container, 'upnp:class').text = 'object.container.storageFolder'
elif len(segments) == 3:
artist = Artist.objects.get(id=int(segments[2]))
container = SubElement(didl, 'container')
container.attrib['id'] = objectID
container.attrib['parentID'] = '/'.join(segments[:1])
container.attrib['restricted'] = '1'
container.attrib['childCount'] = str(len(Album.objects.filter(artist=artist)))
SubElement(container, 'dc:title').text = str(artist.name)
SubElement(container, 'upnp:class').text = 'object.container.person.musicArtist'
elif len(segments) == 4:
album = Album.objects.get(id=int(segments[3]), artist=int(segments[2]))
container = SubElement(didl, 'container')
container.attrib['id'] = objectID
container.attrib['parentID'] = '/'.join(segments[:1])
container.attrib['restricted'] = '1'
container.attrib['childCount'] = str(len(Song.objects.filter(album=album)))
SubElement(container, 'dc:title').text = str(album.name)
SubElement(container, 'upnp:class').text = 'object.container.album.musicAlbum'
if segments[1] == 'playlists':
if len(segments) == 2:
container = SubElement(didl, 'container')
container.attrib['id'] = objectID
container.attrib['parentID'] = '0'
container.attrib['restricted'] = '1'
container.attrib['childCount'] = str(len(Playlist.objects.all()))
SubElement(container, 'dc:title').text = 'Playlists'
SubElement(container, 'upnp:class').text = 'object.container.storageFolder'
elif len(segments) == 3:
playlist = Playlist.objects.get(id=int(segments[2]))
container = SubElement(didl, 'container')
container.attrib['id'] = objectID
container.attrib['parentID'] = '/'.join(segments[:1])
container.attrib['restricted'] = '1'
container.attrib['childCount'] = str(len(playlist))
SubElement(container, 'dc:title').text = str(playlist.name)
SubElement(container, 'upnp:class').text = 'object.container.playlistContainer'
total_matches = 1
number_returned = 1
elif browseFlag == 'BrowseDirectChildren':
def getMatches(startingIndex, requestedCount, qset):
# don't return more than 100 items
total_matches = len(qset)
if requestedCount > 100 or requestedCount == 0:
requestedCount = 100
if startingIndex >= total_matches:
raise UPNPError(402, "startingIndex %i is out of range" % startingIndex)
if startingIndex + requestedCount > total_matches:
requestedCount = total_matches - startingIndex
matches = qset[startingIndex:startingIndex + requestedCount]
number_returned = len(matches)
retval = (matches, total_matches, number_returned)
logger.log_debug("getMatches: %s" % str(retval))
return retval
# determine the number of matches
if len(segments) == 1:
container = SubElement(didl, "container")
container.attrib["id"] = '0/music'
container.attrib["parentID"] = '0'
container.attrib["restricted"] = "1"
container.attrib['childCount'] = str(len(Album.objects.all()))
SubElement(container, "upnp:class").text = "object.container.storageFolder"
SubElement(container, "dc:title").text = 'Music'
container = SubElement(didl, "container")
container.attrib["id"] = '0/playlists'
container.attrib["parentID"] = '0'
container.attrib["restricted"] = "1"
#.........这里部分代码省略.........
示例4: gexf_export
# 需要导入模块: from xml.etree.ElementTree import SubElement [as 别名]
# 或者: from xml.etree.ElementTree.SubElement import attrib["id"] [as 别名]
def gexf_export(most_cited, most_paired, works_list, cliques, suffix, output_directory=options.directory_name):
#Exports network data in .gexf format (readable by Gephi)
#John Mulligan -- not the prettiest, but it gets the job done and translates all the information exported in the d3_export module.
from xml.etree import ElementTree as et
from xml.etree.ElementTree import Element, SubElement, tostring
import os
try:
os.stat(output_directory)
except:
os.mkdir(output_directory)
suffix = suffix + '.gexf'
outfile_name = os.path.join('%s' % output_directory, suffix)
node_key ={node:counter for counter,node in enumerate(sorted(most_cited))}
##Create the tree
et.register_namespace('',"http://www.gexf.net/1.2draft")
et.register_namespace('viz','http://www.gexf.net/1.2draft/viz')
tree = et.ElementTree()
gexf = et.Element("gexf",{"xmlns":"http://www.gexf.net/1.2draft","version":"1.2"})
tree._setroot(gexf)
graph = SubElement(gexf,"graph",{'defaultedgetype':'undirected', 'mode':'dynamic', 'timeformat':'double'})
#more (graph) header information
graph_nodes_attributes = SubElement(graph,"attributes",{'class':'node','mode':'dynamic'})
graph_nodes_mod_att = SubElement(graph_nodes_attributes,"attribute",{'id':'modularity_class','title':'Modularity Class','type':'integer'})
graph_nodes_mod_att_content = SubElement(graph_nodes_mod_att,'default')
graph_nodes_mod_att_content.text = "0"
graph_nodes_score_att = SubElement(graph_nodes_attributes, "attribute", {'id':'score', 'title':'score', 'type':'integer'})
graph_nodes_score_att.text = ' '
graph_edges_attributes = SubElement(graph, "attributes", {'class':'edge', 'mode':'dynamic'})
graph_edges_mod_att = SubElement(graph_edges_attributes,"attribute",{'id':'weight', 'title':'Weight', 'type':'float'})
graph_edges_mod_att.text = ' '
nodes = SubElement(graph,"nodes")
edges = SubElement(graph,"edges")
#write nodes
for n in sorted(most_cited.keys()):
#create node in xml tree
node_dates = sorted([int(a) for a in most_cited[n]['count'].keys() if a != 'full_count'])
node_date_sizes = works_list[n]['count']
node_start_date = str(min(node_dates)) + ".0"
node = SubElement(nodes, "node")
node.attrib["start"] = node_start_date
node.attrib["end"] = '2016.0'
node.attrib["id"] = str(node_key[n])
node.attrib["label"] = n
#add attributes: clique, name
attributes_wrapper = SubElement(node, "attvalues")
clique_id = str(cliques[n])
clique = SubElement(attributes_wrapper, "attvalue", {"for":"modularity_class", "value":clique_id})
clique.text = ' '
#add attribute: visualization size
last = 0
##Each node's "count" attribute is set to a dictionary whose keys are the years it attained new references, and whose values are the number of new references it attained that year.
##The way this is configured right now, nodes only grow (no "decay" variable). size_ratchet keeps track of these size values
size_ratchet = 0
for nd in node_dates:
size_ratchet += node_date_sizes[str(nd)]
##set the start date as the year of the node's changing size...
this_size_start_date = str(nd) + ".0"
##and the end date as one year prior to the next change...
if node_dates.index(nd) + 1 == len(node_dates):
this_size_end_date = "2016.0"
else:
this_size_end_date = str(int(node_dates[node_dates.index(nd)+1])-1) + ".0"
this_size = str(size_ratchet)
size_atty = SubElement(attributes_wrapper, "attvalue", {"for":"score","value":this_size, "start":this_size_start_date, "end":this_size_end_date})
size_atty.text = ' '
#write edges
c = 0
for p in most_paired.keys():
id = str(c)
source = str(node_key[p[0]])
target = str(node_key[p[1]])
edge = SubElement(edges,"edge",{'source':source,'target':target,'id':id})
attributes_wrapper = SubElement(edge, "attvalues")
edge_dates = sorted([int(a) for a in most_paired[p].keys() if (a != 'full_count')])
edge_dates = [str(a) for a in edge_dates]
##see "size ratchet" above in gexf_export()
weight_ratchet = 0
for ed in edge_dates:
start_time = ed + ".0"
if weight_ratchet == 0:
clear_time_atty = SubElement(attributes_wrapper, "attvalue", {"for":"weight", "value":"0.0", "start":"1974.0", "end":start_time})
clear_time_atty.text = ' '
pair = most_paired[p]
#.........这里部分代码省略.........
示例5: gexf_export
# 需要导入模块: from xml.etree.ElementTree import SubElement [as 别名]
# 或者: from xml.etree.ElementTree.SubElement import attrib["id"] [as 别名]
def gexf_export(most_cited, most_paired, output_directory=options.directory_name):
#Exports network data in .gexf format (readable by Gephi)
#John Mulligan -- not the prettiest, but it gets the job done and translates all the information exported in the d3_export module.
from xml.etree import ElementTree as et
from xml.etree.ElementTree import Element, SubElement, tostring
import os
try:
os.stat(output_directory)
except:
os.mkdir(output_directory)
outfile_name = os.path.join('%s' % output_directory,'cites.gexf')
node_key ={node:counter for counter,node in enumerate(sorted(most_cited))}
##Create the tree
et.register_namespace('',"http://www.gexf.net/1.2draft")
et.register_namespace('viz','http://www.gexf.net/1.2draft/viz')
tree = et.ElementTree()
gexf = et.Element("gexf",{"xmlns":"http://www.gexf.net/1.2draft","version":"1.2"})
tree._setroot(gexf)
graph = SubElement(gexf,"graph",{'defaultedgetype':'undirected','mode':'static'})
#more (graph) header information
graph_attributes = SubElement(graph,"attributes",{'class':'node','mode':'static'})
graph_mod_att = SubElement(graph_attributes,"attribute",{'id':'modularity_class','title':'Modularity Class','type':'integer'})
graph_mod_att_content = SubElement(graph_mod_att,'default')
graph_mod_att_content.text = "0"
nodes = SubElement(graph,"nodes")
edges = SubElement(graph,"edges")
#write nodes
for n in sorted(most_cited):
#create node in xml tree
node = SubElement(nodes, "node")
node.attrib["id"] = str(node_key[n])
node.attrib["label"] = n
#add attributes: clique, name
attributes_wrapper = SubElement(node, "attvalues")
clique_id = str(cliques[n])
clique = SubElement(attributes_wrapper,"attvalue",{"for":"modularity_class","value":clique_id})
clique.text = ' '
#add attribute: visualization size
size = str(cited_works[n]['count'])
viz = SubElement(node,"{http://www.gexf.net/1.2draft/viz}size",{"value":size})
#write edges
c = 1
for p in most_paired:
id = str(c)
source = str(node_key[p[0]])
target = str(node_key[p[1]])
value = str(p[2]['weight'])
edge = SubElement(edges,"edge",{'id':id,'source':source,'target':target,'value':value})
c+=1
tree.write(outfile_name, xml_declaration = True, encoding = 'utf-8', method = 'xml')