diff --git a/src/sgfParser/node.py b/src/sgfParser/node.py --- a/src/sgfParser/node.py +++ b/src/sgfParser/node.py @@ -68,3 +68,29 @@ class Node: def getProperty(self,name): if name in self.properties: return self.properties[name] else: return None + + ## Returns textual representation of the Node itself, but disregards its children. + def __str__(self): + return ";" + "".join(str(p) for p in self.properties.values()) + + def export(self): + # there is a beatiful recursive solution, which this stack is too narrow to contain + stack=[(self,1,1)] + output=[] + + while len(stack)>0: + node,left,right=stack.pop() + if left>0: output.append("("*left) + output.append(str(node)) + + childCount=len(node.children) + if childCount==0: # a leaf + output.append(")"*right) + elif childCount==1: # a line + stack.append((node.children[0],0,right)) + else: # a branching node + # first child pops first, last child closes parent's parentheses + children=zip(node.children,[1]*childCount,[1]*(childCount-1)+[1+right]) + stack.extend(reversed(children)) + + return "".join(output)