附件里是将 kjd.sgf (围棋定式)转换为 sql 语句的 python 源码,以及 sgf 源文件及生成的 sql 源文件。改自 eidogo.com。
今天玩 python 的体会:1、因为用到 simplejson,我用的是 python 2.66 的版本。太低了(2.1.7)好象不行。2、python 里不能用 tab 符号?3、python 确实很强大。
#import MySQLdb as mysql
import simplejson
class SgfNode:
def __init__(self, id, parent):
self.id = id
self.properties = {}
self.parent = parent
self.children = []
# DB prep fields
self.depth = 0
self.lt = 0
self.rt = 0
pass
class SgfParser:
def __init__(self, sgf):
self.sgf = sgf
self.pos = 0
self.root = SgfNode(0, None)
self.nodecounter = 1
self.parse_tree(self.root)
def parse_tree(self, r):
while (self.pos < len(self.sgf)):
c = self.sgf[self.pos]
self.pos += 1
if c == ';':
r = self.parse_node(r)
elif c == '(':
self.parse_tree(r)
elif c == ')':
return
def parse_node(self, parent):
node = SgfNode(self.nodecounter, parent)
self.nodecounter += 1
node = self.parse_properties(node)
cur = node
depth = 0
while 1:
cur = cur.parent
if not cur:
break
depth += 1
node.depth = depth
parent.children.append(node)
return node
def parse_properties(self, node):
prop = ''
vals = []
i = 0
while (self.pos < len(self.sgf)):
c = self.curc()
if c == ';' or c == '(' or c == ')':
break
if self.curc() == '[':
while self.curc() == '[':
self.pos += 1
vals.insert(i, '')
while self.curc() != ']' and self.pos < len(self.sgf):
if self.curc() == '\\':
self.pos += 1
while self.curc() == '\r' or self.curc() == '\n':
self.pos +=1
vals[i] += self.curc()
self.pos += 1
i += 1
while self.curc() == ']' or self.curc() == '\r' or self.curc() == '\n':
self.pos += 1
if node.properties.has_key(prop):
if not (type(node.properties[prop]) is list):
node.properties[prop] = [node.properties[prop]]
node.properties[prop].extend(vals)
else:
if (len(vals) > 1):
node.properties[prop] = vals
else:
node.properties[prop] = vals[0]
prop = ''
vals = []
i = 0
continue
if c != ' ' and c != '\n' and c != '\r' and c != '\t':
prop += c
self.pos += 1
return node
def curc(self):
return self.sgf[self.pos]
def compute_ltrt(node, lt):
rt = lt + 1
for child in node.children:
rt = compute_ltrt(child, rt)
node.lt = lt
node.rt = rt
return rt + 1
def db_populate(n, c):
if n.parent:
parent_id = n.parent.id
else:
parent_id = 0
c.write( "insert into kjd (id, parent, properties, lt, rt, depth) values(" )
c.write(str(n.id) + "," + str(parent_id) + ",'" + str(simplejson.dumps(n.properties))
# c.write(str(n.id) + "," + str(parent_id) + ",'" + str(n.properties)
+ "'," + str(n.lt) + "," + str(n.rt) + "," + str(n.depth) + ")\r\n")
print n.id
for child in n.children:
db_populate(child, c)
def file_print(data, c):
c.write(data)
c.write('\r\n')
#if __name__ == "__main__":
# file = open("../sgf/kjd.sgf")
file = open("kjd.sgf")
sgf = file.read()
file.close()
root = SgfParser(sgf).root
compute_ltrt(root, 1)
# db = mysql.connect(host="localhost", user="", passwd="", db="eidogo")
# c = db.cursor()
# db_populate(root, c)
# c.execute("update kjd set parent=null where id=0")
c = open("kjd.sql", "w")
db_populate(root, c)
# c.write( "insert into kjd (id, parent, properties, lt, rt, depth) values(" )
c.write("update kjd set parent=null where id=0\r\n")
# file_print( "insert into kjd (id, parent, properties, lt, rt, depth) values(", c )
# file_print( "update kjd set parent=null where id=0", c )
c.close()