graphviz中有向图解析函数
def match_slice(pattern, string, k=0)->list:
'''
返回首次或末次匹配到的字符串在原字符串的位置范围,若无返回空列表
k=0表示默认从左到右查,当k = -1表示从右到左查,即查最后一个匹配的位置范围
这里pattern,为re内的pattern
彡
220728
'''
matchs_temp = re.finditer(pattern, string)
if k == 0:
pass
else:
k = -1
try:
match_span = [x for x in matchs_temp][k].span()
rs = [match_span[0], match_span[1]]
except:
rs = []
return rs
s_test = "1fghsfdhsfg}hsfghsf}};}; "
print(len(s_test))
print(match_slice("\};?",s_test,-1))
def digraph_resolver(dot_sourse)->list:
'''
解析器resolver
parameter:
dot_sourse:为dot.sourse
对列表dot.sourse中的每一字符所表示的内容进行判断归类,并返回结果列表
类别有:
# 图内外单行注释 [[], [], ...]
# 图内外多行注释 [[], [], ...]
# 图 [[图头],[图体],[图尾]]
# 图属性 [[], [], ...]
# 子图 [[], [], ...] >>> 点 ,边
# 点 [[], [], ...] >>> 点的对应标签
# 边 [[], [], ...]
彡
220723-220728
'''
pattern4comment1 = "//.*"
pattern4comment2 = "/\*.*?\*/"
pattern4bigraph = "digraph +[a-zA-Z\_][0-9a-zA-Z\_]* +\{.*\}"
pattern4bigraph_head = "digraph +[a-zA-Z\_][0-9a-zA-Z\_]* +\{"
pattern4bigraph_attr = "(graph|node|edge) +\[.*?\];?"
pattern4subgraph = "subgraph +cluster[0-9a-zA-Z\_]+ +\{.*?[^\\\\]\};?"
pattern4node = '(([0-9]+)|([a-zA-Z\_][0-9a-zA-Z\_]*[^(digraph|graph|node|edge)])|( ?"[^"]+?[^\\\\]")|( ?"")) +\[.*?[^\\\\]\];?'
pattern4edge = '(([0-9]+)|([a-zA-Z\_][0-9a-zA-Z\_]*)|( ?"[^"]+?[^\\\\]")|( ?""))( *: *[a-zA-Z\_][0-9a-zA-Z\_]*)? *-> *(([0-9]+)|([a-zA-Z\_][0-9a-zA-Z\_]*)|( ?"[^"]+?[^\\\\]")|( ?""))( *: *[a-zA-Z\_][0-9a-zA-Z\_]*)? *(\[.*?[^\\\\]\])?;?'
pattern4label = 'label *= *(".*?[^\\\\]"|\< *\< *table .*table *\> *\>)'
li_comment1s = []
li_comment2s = []
li_bigraph_temp = []
li_bigraph = []
li_bigraph_attrs = []
li_subgraphs = []
li_nodes = []
li_edges = []
li_other = []
li_source = []
with open("dot_source.txt",'w') as f:
for line in dot_sourse:
f.write(line)
li_source.append(line)
s_source = ''.join(li_source)
print("s_source:",s_source)
lines = s_source.split("\n"