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 @@ -1,7 +1,7 @@ from collections import deque import logging as log -from . import skipWhitespace, ParserWarning +from . import skip_whitespace, ParserWarning from .property import Property, GAME_INFO @@ -20,65 +20,65 @@ class Node: assert Node.fits(s, start) res = Node() - i = skipWhitespace(s, start+1) + i = skip_whitespace(s, start + 1) while Property.fits(s, i): (i, x) = Property.create(s, i) if x.name in res.properties: log.warning(ParserWarning('duplicate "{0}" property in a node. second value ignored'.format(x.name), s, i)) else: res.properties[x.name] = x - i = skipWhitespace(s, i) + i = skip_whitespace(s, i) return (i, res) - def listGINodes(self): - if self.isGINode(): + def list_gi_nodes(self): + if self.is_gi_node(): yield self - empty = not self.isGINode() + empty = not self.is_gi_node() node = self while node.parent: node = node.parent - if node.isGINode(): + if node.is_gi_node(): empty = False yield node queue = deque(self.children) while len(queue) > 0: node = queue.popleft() - if node.isGINode(): + if node.is_gi_node(): 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 - def isGINode(self): + def is_gi_node(self): return any(prop.type == GAME_INFO for prop in self.properties.values()) - def setProp(self, name, value): + def set_prop(self, name, value): self.properties[name] = value # check value type - def setChildren(self, children): + def set_children(self, children): self.children = children for child in children: child.parent = self - def addChild(self, node): + def add_child(self, node): if node in self.children: return node node.parent = self self.children.append(node) return node - def removeChild(self, node): + def remove_child(self, node): if node not in self.children: return None del self.children[self.children.index(node)] node.parent = None return node - def removeChildAt(self, i): + def remove_child_at(self, i): if -len(self.children) < i < len(self.children): res = self.children[i] del self.children[i] @@ -93,7 +93,7 @@ class Node: res.parent = self.parent return res - def getProp(self, name, default=None): + def get_prop(self, name, default=None): if name in self.properties: return self.properties[name].value else: @@ -114,14 +114,14 @@ class Node: output.append("("*left) output.append(str(node)+"\n") - childCount = len(node.children) - if childCount == 0: # a leaf + child_count = len(node.children) + if child_count == 0: # a leaf output.append(")"*right+"\n") - elif childCount == 1: # a line + elif child_count == 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]) + children = zip(node.children, [1]*child_count, [1]*(child_count-1)+[1+right]) stack.extend(reversed(list(children))) return "".join(output)