我有一个文件(请参阅下面的代码中的状态变量),我想将其转换为流程图(附件)。我的python脚本会转换" status"进入字典。如何将该字典转换为流程图或图形? 我的代码:
status = """
Object car {
Name honda;
From Richland;
To Seattle;
Distance 160;
Configuration road_travel;
}
Object bus {
Name greyhound;
From pasco;
To richland;
Distance 15;
Configuration road_travel;
}
Object aeroplane {
Name united;
From miami_airport;
To pasco;
Distance 1000;
Configuration air_travel;
}
Object train {
Name gas_train;
From beach;
To miami_airport;
Distance 30;
Configuration train_travel;
}
"""
sale_number = ''
sales = collections.defaultdict(list)
for line in status.split('\n'):
line = line.strip()
if line.startswith("set"):
continue
elif (line.startswith("Object") or line.startswith("object")):
sale_number = line.split(' ')[1].strip()
elif not line or line.isspace() :
continue
else:
# you can also use a regular expression here
sales[sale_number].append(line.split())
for sale in sales:
print sale+str(dict(sales[sale][:-1]))
这会产生:
car{'To': 'Seattle;', 'Configuration': 'road_travel;', 'From': 'Richland;', 'Name': 'honda;', 'Distance': '160;'}
train{'To': 'miami_airport;', 'Configuration': 'train_travel;', 'From': 'beach;', 'Name': 'gas_train;', 'Distance': '30;'}
aeroplane{'To': 'pasco;', 'Configuration': 'air_travel;', 'From': 'miami_airport;', 'Name': 'united;', 'Distance': '1000;'}
bus{'To': 'richland;', 'Configuration': 'road_travel;', 'From': 'pasco;', 'Name': 'greyhound;', 'Distance': '15;'}
我希望将上面的python输出转换成如下所示的图片。我不想使用Giffy或MS-Visio手动执行此操作,因为我的实际案例有大约1000个对象(此示例在"状态"中有4个对象;)
答案 0 :(得分:1)
只关注将奇怪格式化的status
字符串转换为dict
是不是很难,你不能用像JSON这样更合理,更流行的格式吗?
import re
def Status2dict(status):
result = {}
current = {}
lines = status.splitlines()
for line in lines:
line = line.strip()
if not line:
continue
mo = re.match(r'Object (\w+) {', line)
if mo:
curk = mo.group(1)
current = {curk: {}}
elif re.match('}', line):
result.update(current)
current = {}
else:
mo = re.match(r'(\w+)\s+([\w\s]+);', line)
if not mo:
raise ValueError('cannot match {!r}'.format(line))
current[curk][mo.group(1)] = mo.group(2)
if current:
result.update(current)
return result
import pprint
pprint.pprint(Status2dict(status))
此代码尝试在推断语法的小变化上略微强健,您可能需要向上拨打或,具体取决于。但是,它应该比没有好。
答案 1 :(得分:0)
如果您已将代码转换为点文件,则可以使用graphviz的tk导出在tkinter画布上可视化您的图形。您可以查看其他库的this other question。
dot -Ttk
输出像这样的tcl代码
# a
$c create oval 5.33 53.33 77.33 5.33 -fill white -width 1 -outline black -tags {1node1}
$c create text 41.33 30.3 -text {a} -fill black -font {"Times" 14} -tags {0node1}
# c
$c create oval 53.33 149.33 125.33 101.33 -fill white -width 1 -outline black -tags {1node2}
$c create text 89.33 126.3 -text {c} -fill black -font {"Times" 14} -tags {0node2}
#(...)
您可以通过调用tcl解释器在tkinter画布上显示它,如下例所示:
from Tkinter import *
import subprocess
graph = "digraph g { a-> c ; b -> c ; c -> d }"
ps = subprocess.Popen(('dot', '-Ttk'),stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = ps.communicate(input=graph)
canvas = Canvas()
canvas.pack(expand=YES, fill=BOTH)
#the output of graphiz assume thazt the canvas in variable $c
out = "set c .{}\n".format(canvas._name) + out
#tk.eval expect command one by one
map(canvas.tk.eval, out.split("\n"))
canvas.mainloop()