python 写的自动生成植化流程图的脚本
发表于 : 2009-09-09 0:48
RT
可能有搞植化/化学的同学有用(受弯弯同学脚本的启发并参考了部分代码http://python.ubuntu.org.cn/viewtopic.p ... 5bf2325043)。看图: 画图的话只需要用很简单的语言描述一下即可,请看此图的描述语言(deffile):
用法:
简要说明:
运行该脚本需要 python-yapgvb
#前面是 node 的名字,$后面是描述,=> 将两个 node 连接起来,在其中画箭头,可以连续连接 比如 a# => b# => c# 也是可以的,node 后面 => 前面的 $xxx 是线上的注释,如果某一个点要往下分支的话只需用 node#^ 即可,node#^ 后面可以直接加注释,很简单的,看上面那个例子结合图片就可以搞明白了。源码如下:
可能有搞植化/化学的同学有用(受弯弯同学脚本的启发并参考了部分代码http://python.ubuntu.org.cn/viewtopic.p ... 5bf2325043)。看图: 画图的话只需要用很简单的语言描述一下即可,请看此图的描述语言(deffile):
代码: 全选
a#草药样品
b#粗分浸膏
a#$甲醇提取=>b#
c1#正丁醇相
c2#石油醚相
c3#...
c4#水相
b#^$有机溶剂萃取
b#=>c1#
b#=>c2#
b#=>c3#
b#=>c4#
c2#^$继续柱层析分离
c2#=>d1#成分五
c2#=>d2#成分六
代码: 全选
python liuchengtu.py -o xxx.png deffile
运行该脚本需要 python-yapgvb
#前面是 node 的名字,$后面是描述,=> 将两个 node 连接起来,在其中画箭头,可以连续连接 比如 a# => b# => c# 也是可以的,node 后面 => 前面的 $xxx 是线上的注释,如果某一个点要往下分支的话只需用 node#^ 即可,node#^ 后面可以直接加注释,很简单的,看上面那个例子结合图片就可以搞明白了。源码如下:
代码: 全选
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: xioooli<at>yahoo.com.cn,joolix.com
# Licence GPLv2
# Version 2009.09.09
import sys
import yapgvb
import optparse
FORMATS = {"png" : yapgvb.formats.png,
"jpg" : yapgvb.formats.jpg,
"gif" : yapgvb.formats.gif}
ENGINES = {"dot" : yapgvb.engines.dot,
"neato" : yapgvb.engines.neato,
"circo" : yapgvb.engines.circo,
"twopi" : yapgvb.engines.twopi}
if __name__ == '__main__':
# args = sys.argv
# if len(args) < 2:
# print "Usage: python state_machine.py <def file>"
# sys.exit(0)
parser = optparse.OptionParser()
parser.add_option("-f", "--format", dest="format",
help="store the flow chart in FORMAT (png, svg, jpg, gif)",
metavar="FORMAT", default="png")
parser.add_option("-o", "--output", dest="output",
help="save the flow chart to FILE",
metavar="FILE")
parser.add_option("-e", "--engine", dest="engine",
help="the layout ENGINE to use for the flow chart (dot, neato, circo, twopi)",
metavar="ENGINE", default="dot")
parser.add_option("-c", "--color", dest="fillcolor",
help="the fillcolor of the node",
default="white")
parser.add_option("--fs", "--font-size", dest="fontsize",
help="the font size of the text",
default="12")
parser.add_option("--nfc", "--node-font-color", dest="nodefontcolor",
help="the fillcolor of the font in node",
default="black")
parser.add_option("--nc", "--node-color", dest="nodecolor",
help="the color of the node frame",
default="black")
parser.add_option("--ec", "--edge-color", dest="edgecolor",
help="the color of the edge",
default="black")
parser.add_option("--efc", "--edge-font-color", dest="edgefontcolor",
help="the font color of the edge text",
default="black")
parser.add_option("-s", "--style", dest="style",
help="the style of the node",
default="filled")
parser.add_option("--ah", "--arrowhead", dest="arrowhead",
help="the style of the arrowhead",
default="normal")
parser.add_option("--as", "--arrowsize", dest="arrowsize",
help="the size of the arrow",
default="1")
options, args = parser.parse_args()
if len(args) < 1:
parser.print_usage()
sys.exit(0)
graph = yapgvb.Graph("States")
graph.rankdir="TB"
node_dict = {}
with open(args[0]) as def_file:
lines = [l.strip() for l in def_file.readlines()]
for line in lines:
nodes = line.split("=>")
prev_node = None
for node in nodes[::-1]:
label = node.strip().split("#")
if not node_dict.has_key(label[0]) and label[0] != "":
try:
lb=label[1].split("$")[0]
except:
lb=""
node_in_graph = graph.add_node(label=lb,
shape='box', fillcolor=options.fillcolor,
fontcolor=options.nodefontcolor, fontsize=options.fontsize,
style=options.style, color=options.nodecolor, width=0.5)
node_dict[label[0]] = node_in_graph
elif label[0] != "":
node_in_graph = node_dict[label[0]]
try:
blanknode = node.strip().split("#^")
if len(blanknode) >= 2:
blanknode = blanknode[0]
else:
blanknode = ""
except:
blanknode = ""
if node_dict.has_key(blanknode) and blanknode != "":
node_dict[blanknode + "_0"] = node_dict[blanknode]
node_in_graph = graph.add_node(label="",
shape="circle", fillcolor=options.edgecolor,
color=options.edgecolor, style="filled", height=.05, width=.05)
node_dict[blanknode] = node_in_graph
edge = node_dict[blanknode + "_0"] - node_dict[blanknode]
edge.color=options.edgecolor
try:
edge.label=" "+node.strip().split("$")[1]
edge.fontcolor=options.edgefontcolor
edge.fontsize=options.fontsize
except:
pass
if prev_node:
edge = node_in_graph - prev_node
edge.color=options.edgecolor
edge.arrowhead = options.arrowhead
edge.arrowsize = options.arrowsize
try:
edge.label=" "+node.strip().split("$")[1]
edge.fontcolor=options.edgefontcolor
edge.fontsize=options.fontsize
except:
pass
prev_node = node_in_graph
graph.layout(ENGINES[options.engine])
format = FORMATS[options.format]
if options.output:
out_file = options.output
else:
out_file = args[0] + "." + format
graph.render(out_file, format)