本文整理汇总了Python中graph.Vertex.name方法的典型用法代码示例。如果您正苦于以下问题:Python Vertex.name方法的具体用法?Python Vertex.name怎么用?Python Vertex.name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph.Vertex
的用法示例。
在下文中一共展示了Vertex.name方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
# 需要导入模块: from graph import Vertex [as 别名]
# 或者: from graph.Vertex import name [as 别名]
#.........这里部分代码省略.........
if 'id' not in vxbranch.data:
raise ImportError, "GML format error: node without ID"
#
# Extension - we allow node IDs of type String as well as
# integer.
#
if not isinstance(vxbranch.data['id'][0].data, (int, GMLString)):
raise ImportError, ("GML format error: node ID '%s' of "
"unsupportedtype" % (vxbranch.data['id'][0].data))
id = str(vxbranch.data['id'][0].data)
if id in vertexIndex:
# Duplicate vertex
if yesall_duplicate_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Duplicate vertex detected: id %s" % (id))
if result == CustomButtonDialog.ID['Yes']:
continue
elif result == CustomButtonDialog.ID['Yes to similar errors']:
yesall_duplicate_vert = True
continue
elif result == CustomButtonDialog.ID['Yes to all errors']:
yestoall = True
continue
else:
raise ImportError, "Import aborted."
else:
raise ImportError, "Duplicate vertex detected: id %s" % (id)
v = Vertex(id=id)
if 'label' in vxbranch.data:
v.name = str(vxbranch.data['label'][0].data)
if 'graphics' in vxbranch.data:
pos = list(v.pos)
rad = v.radius
color = list(v.color)
grbranch = vxbranch.data['graphics'][0].data
if 'x' in grbranch.data:
pos[X_AXIS] = float(grbranch.data['x'][0].data)
if 'y' in grbranch.data:
pos[Y_AXIS] = float(grbranch.data['y'][0].data)
if 'z' in grbranch.data:
pos[Z_AXIS] = float(grbranch.data['z'][0].data)
if 'w' in grbranch.data:
radius = float(grbranch.data['w'][0].data)
if 'h' in grbranch.data:
radius = max(radius, float(grbranch.data['h'][0].data))
if 'd' in grbranch.data:
radius = max(radius, float(grbranch.data['d'][0].data))
if 'fill' in grbranch.data:
fail = False
fillstr = grbranch.data['fill'][0].data
if not isinstance(fillstr, GMLString):
fail = True
fail = "fill must be a String"
elif not len(str(fillstr)) > 0:
fail = "fill string too short"
elif str(fillstr)[0] != '#':
# Named color
try:
color = colorByName(str(fillstr))
except UnknownColor:
fail = "unrecognized named color"
示例2: load
# 需要导入模块: from graph import Vertex [as 别名]
# 或者: from graph.Vertex import name [as 别名]
def load(graph, fobj, queryCallback=None, warningCallback=None):
""" Load a TLP file and create a graph from it
Arguments:
graph -- the graph to be created from the loaded file
fobj -- a file object of the TLP file to be read
queryCallback -- function to display a default query dialog (default None)
warningCallback -- function to display a default warning dialog
(default None)
"""
try:
tlpgraph = TLPGraph(Stream(fobj))
except Unbalanced:
raise ImportError, "Unbalanced delimiters in file"
except TypeMismatch:
raise ImportError, "Unintelligible value in file"
except EOF:
raise ImportError, "Premature end of file"
# Boolean variables to control if the rest of the import errors are handled
# automatically
yestoall = False
yesall_duplicate_edge = False
yesall_degenerate_edge = False
yesall_nonexist_vert = False
# Make vertices and apply properties
for vertex, properties in tlpgraph.nodes.items():
v = Vertex(vertex)
if "viewLabel" in properties:
v.name = properties["viewLabel"]
if "viewLayout" in properties:
pos = string_to_tuple(properties["viewLayout"])
v.pos = tuple([float(x) for x in pos])
if "viewSize" in properties:
v.radius = float(string_to_tuple(properties["viewSize"])[0])
if "viewColor" in properties:
color = string_to_tuple(properties["viewColor"])[:3]
v.color = tuple([float(x) / 255 for x in color])
graph.addVertex(v)
# Make edges and apply properties
edges = tlpgraph.edges.values()
edges.extend(tlpgraph.lost_edges)
for properties in edges:
source = properties["source"]
target = properties["target"]
# Raise an error if edge is degenerate, or source or target vertices
# are non existent. Automatically handle errors if yes all is selected
if source == target:
# Degenerate Edge
if yesall_degenerate_edge or yestoall:
continue
if queryCallback:
result = queryCallback("Degenerate edge detected: %s -> %s" % (source, target))
if result == CustomButtonDialog.ID["Yes"]:
continue
elif result == CustomButtonDialog.ID["Yes to similar errors"]:
yesall_degenerate_edge = True
continue
elif result == CustomButtonDialog.ID["Yes to all errors"]:
yestoall = True
continue
else:
raise ImportError, "Import aborted."
else:
raise ImportError("Degenerate edge detected: %s -> %s" % (source, target))
if source not in tlpgraph.nodes.keys():
# Nonexistent vertex
if yesall_nonexist_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Edge specifies nonexistent " "source vertex %s" % (source))
if result == CustomButtonDialog.ID["Yes"]:
continue
elif result == CustomButtonDialog.ID["Yes to similar errors"]:
yesall_nonexist_vert = True
continue
elif result == CustomButtonDialog.ID["Yes to all errors"]:
yestoall = True
continue
else:
raise ImportError, "Import aborted."
else:
raise ImportError, ("Edge specifies nonexistent source vertex" " %s" % (source))
if target not in tlpgraph.nodes.keys():
# Nonexistent vertex
if yesall_nonexist_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Edge specifies nonexistent " "target vertex %s" % (target))
if result == CustomButtonDialog.ID["Yes"]:
continue
elif result == CustomButtonDialog.ID["Yes to similar errors"]:
yesall_nonexist_vert = True
continue
#.........这里部分代码省略.........
示例3: load
# 需要导入模块: from graph import Vertex [as 别名]
# 或者: from graph.Vertex import name [as 别名]
#.........这里部分代码省略.........
raise ImportError, "No edge default directedness found!"
if attrs['edgedefault'] != 'undirected' and warningCallback:
warningCallback("Directed graphs are not supported. "
"Directed edges will be converted.")
#
# Set up an index of vertex IDs and edge tuples - this allows
# us to check uniqueness and also eliminates costly
# findVertexByID calls.
#
vertexIndex = {}
edgeIndex = {}
# Read vertices (aka nodes)
for vertNode in graphNode.getElementsByTagName('node'):
vertAttrs = dict(
[(str(a), str(b)) for a, b in vertNode.attributes.items()]
)
# Create a dict that maps vertex attribute names to their values
for dataNode in vertNode.getElementsByTagName('data'):
dataAttrs = dict(
[(str(a), str(b)) for a, b in dataNode.attributes.items()]
)
if 'node' in keyInfo[dataAttrs['key']]:
key = keyInfo[dataAttrs['key']]['node']
else:
key = keyInfo[dataAttrs['key']]['forNotSpecified']
if dataNode.childNodes != []:
if 'attr.type' in key.keys():
value = key['attr.type'](dataNode.childNodes[0].wholeText.strip())
else:
value = str(dataNode.childNodes[0].wholeText.strip())
if 'attr.name' in key.keys():
vertAttrs[key['attr.name']] = value
# Assign defaults
if len(defaults) > 0 and defaults['node']:
for prop, value in defaults['node'].items():
if prop not in vertAttrs:
vertAttrs[prop] = value
# Raise ImportError if vertex is a duplicate
if vertAttrs['id'] in vertexIndex:
if yesall_duplicate_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Duplicate vertex detected: id %s" %
(vertAttrs['id']))
if result == CustomButtonDialog.ID['Yes']:
continue
elif result == CustomButtonDialog.ID['Yes to similar errors']:
yesall_duplicate_vert = True
continue
elif result == CustomButtonDialog.ID['Yes to all errors']:
yestoall = True
continue
else:
raise ImportError, "Import aborted."
else:
raise ImportError, ("Duplicate vertex detected: id %s" %
(vertAttrs['id']))
v = Vertex(id=vertAttrs['id'])
# Determine the color of the vertex, accepting either
示例4: load
# 需要导入模块: from graph import Vertex [as 别名]
# 或者: from graph.Vertex import name [as 别名]
def load(graph, fobj, queryCallback=None, warningCallback=None):
"""Load .gv or .dot file
Arguments:
graph -- the graph to be created from the loaded file
fobj -- a file object of the DOT file to be read
queryCallback -- function to display a default query dialog (default None)
warningCallback -- function to display a default warning dialog
(default None)
"""
# Set global variables to in DOTGraph for automatically replacing
# attributes of duplicate nodes and edges
global _yestoall_nodes
global _yestoall_edges
_yestoall_nodes = None
_yestoall_edges = None
try:
dotgraph = DOTGraph(Stream(fobj), queryCallback=queryCallback,
warningCallback=warningCallback)
except EOF:
raise ImportError, "Premature end of file"
except Unbalanced:
raise ImportError, "Unbalanced delimiters in file"
# Boolean variables to control if the rest of the import errors are handled
# automatically
yestoall = False
yesall_color = False
yesall_degenerate_edge = False
def getColor(color):
"""Retun an RGB float triple of a color string. Return False if invalid
Arguments:
color -- a string representing a color
"""
# Hex color
if color[0] == '#' and len(color) == 7:
fillstr = color
r = fillstr[1:3]
g = fillstr[3:5]
b = fillstr[5:7]
try:
color = [x / 255.0 for x in (int(r, 16), int(g, 16), int(b, 16))]
return color
except:
return False
# HSV float triple
elif ',' in color or ' ' in color:
color = color.replace(" ", "").split(',', 3)
try:
color = [float(x) for x in color]
color = colorsys.hsv_to_rgb(color[0], color[1], color[2])
return color
except:
return False
# X11 Color Name
else:
try:
color = colorByName(color)
return color
except UnknownColor:
return False
# Add all the vertices in the dotgraph to the context graph
for vertex, attributes in dotgraph.nodes.items():
v = Vertex(id=vertex)
if 'label' in attributes:
v.name = attributes['label']
if 'width' in attributes:
v.radius = float(attributes['width'])
if 'pos' in attributes:
# remove ! and spaces, then split numbers
pos = attributes['pos'][:-1].replace(" ", "").split(',', 3)
if len(pos) < 3:
pos.append(0.0)
v.pos = (float(pos[0]), float(pos[1]), float(pos[2]))
if 'color' in attributes:
color = getColor(attributes['color'])
if color != False:
v.color = tuple(color)
elif not yesall_color and not yestoall:
# Invalid color
if queryCallback:
result = queryCallback("Invalid vertex color %s for %s. "
"Assign default color?" %
(attributes['color'], vertex), True)
if result == CustomButtonDialog.ID['Yes']:
pass
elif result == CustomButtonDialog.ID['Yes to similar errors']:
yesall_color = True
elif result == CustomButtonDialog.ID['Yes to all errors']:
yestoall = True
else:
raise ImportError, "Import aborted."
#.........这里部分代码省略.........
示例5: load
# 需要导入模块: from graph import Vertex [as 别名]
# 或者: from graph.Vertex import name [as 别名]
def load(graph, fobj, queryCallback=None, warningCallback=None):
""" Load a tgf file and create a graph from it
Raises ImportError when:
- there are duplicate vertices
- there are duplicate edges
- there are edges where the source and target vertices are the same
- there are edges with non-existant source or target vertices
"""
# Maps a vertex ID string to a vertex object
vertexIndex = {}
# Maps of 2-tuple of vertex ID strings (src, tgt) to an edge object
edgeIndex = {}
# Boolean variables to control if the rest of the import errors are handled
# automatically
yestoall = False
yesall_duplicate_vert = False
yesall_duplicate_edge = False
yesall_degenerate_edge = False
yesall_nonexist_vert = False
# Read vertices until # has been found
while (True):
line = fobj.readline()
if line == "#\n":
break
if line == "":
raise ImportError, "Premature end of file"
line = line[:-1].split(' ', 1)
id = line[0]
# Raise ImportError if same vertex id has already been added
if id in vertexIndex.keys():
if yesall_duplicate_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Duplicate vertex detected: "
"id %s" % (id))
if result == CustomButtonDialog.ID['Yes']:
continue
elif result == CustomButtonDialog.ID['Yes to similar errors']:
yesall_duplicate_vert = True
continue
elif result == CustomButtonDialog.ID['Yes to all errors']:
yestoall = True
continue
else:
raise ImportError, "Import aborted."
else:
raise ImportError, "Duplicate vertex detected: id %s" % (id)
v = Vertex(id=id)
if len(line) > 1:
v.name = line[1]
graph.addVertex(v)
vertexIndex[line[0]] = v
# Read edges until the end of file is reached
while (True):
line = fobj.readline()
if not line:
break
line = line[:-1].split(' ', 2)
(src, tgt) = (line[0], line[1])
# Raise ImportError if vertices do not exist, if edge already exists,
# or if source and target vertices are the same
if src not in vertexIndex:
# Nonexistent vertex
if yesall_nonexist_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Edge specifies nonexistent "
"source vertex %s" % (src))
if result == CustomButtonDialog.ID['Yes']:
continue
elif result == CustomButtonDialog.ID['Yes to similar errors']:
yesall_nonexist_vert = True
continue
elif result == CustomButtonDialog.ID['Yes to all errors']:
yestoall = True
continue
else:
raise ImportError, "Import aborted."
else:
raise ImportError, ("Edge specifies nonexistent "
"source vertex %s" % (src))
if tgt not in vertexIndex:
# Nonexistent vertex
if yesall_nonexist_vert or yestoall:
continue
if queryCallback:
result = queryCallback("Edge specifies nonexistent "
"target vertex %s" % (tgt))
if result == CustomButtonDialog.ID['Yes']:
continue
elif result == CustomButtonDialog.ID['Yes to similar errors']:
yesall_nonexist_vert = True
#.........这里部分代码省略.........