diff --git a/src/diana/sgfParser/node.py b/src/diana/sgfParser/node.py --- a/src/diana/sgfParser/node.py +++ b/src/diana/sgfParser/node.py @@ -7,91 +7,97 @@ from .property import Property, GAME_INF class Node: def __init__(self): - self.properties=dict() - self.parent=None - self.children=[] + self.properties = dict() + self.parent = None + self.children = [] @staticmethod - def fits(s,i): - return i0: - node=queue.popleft() + queue = deque(self.children) + while len(queue) > 0: + node = queue.popleft() if node.isGINode(): - empty=False + empty = False yield node queue.extend(node.children) - if empty: yield self # always yield at least self, can work as GINode as well as any other + if empty: + yield self # always yield at least self, can work as GINode as well as any other def isGINode(self): - return any(prop.type==GAME_INFO for prop in self.properties.values()) + return any(prop.type == GAME_INFO for prop in self.properties.values()) - def setProp(self,name,value): - self.properties[name]=value - # zkontrolovat typ value + def setProp(self, name, value): + self.properties[name] = value + # check value type - def setChildren(self,children): - self.children=children - for child in children: child.parent=self + def setChildren(self, children): + self.children = children + for child in children: + child.parent = self - def addChild(self,node): - if node in self.children: return node - node.parent=self + def addChild(self, node): + if node in self.children: + return node + node.parent = self self.children.append(node) return node - def removeChild(self,node): + def removeChild(self, node): if node not in self.children: return None del self.children[self.children.index(node)] - node.parent=None + node.parent = None return node - def removeChildAt(self,i): - if -len(self.children)0: - node,left,right=stack.pop() - if left>0: output.append("("*left) + while len(stack) > 0: + (node, left, right) = stack.pop() + if left > 0: + output.append("("*left) output.append(str(node)+"\n") - childCount=len(node.children) - if childCount==0: # a leaf + childCount = len(node.children) + if childCount == 0: # a leaf output.append(")"*right+"\n") - elif childCount==1: # a line - stack.append((node.children[0],0,right)) - else: # a branching node + 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)) + children = zip(node.children, [1]*childCount, [1]*(childCount-1)+[1+right]) + stack.extend(reversed(list(children))) return "".join(output)