Loading DHParser/syntaxtree.py +16 −0 Original line number Diff line number Diff line Loading @@ -426,6 +426,22 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil self._xml_attr = OrderedDict() return self._xml_attr def get_attr(self, attribute: str, default: str) -> str: """ Returns the value of 'attribute' if attribute exists. If not, the default value is returned. This function has the same semantics as `node.attr.get(attribute, default)`, but with the advantage then other than `node.attr.get` it does not automatically create an attribute dictionary on (first) access. :param attribute: The attribute, the value of which shall be looked up :param default: A default value that is returned, in case attribute does not exist. :return: str """ if self.has_attr(): return self.attr.get(attribute, default) return default def compare_attr(self, other: 'Node') -> bool: """ Returns True, if `self` and `other` have the same attributes with the Loading test/test_syntaxtree.py +3 −2 Original line number Diff line number Diff line Loading @@ -273,8 +273,8 @@ class TestRootNode: str(result) class TestNodeFind(): """Test the select_if-functions of class Node. class TestNodeFind: """Test the item-access-functions of class Node. """ def test_find(self): Loading @@ -297,6 +297,7 @@ class TestNodeFind(): tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))') assert tree[0].equals(parse_sxpr('(b X)')) assert tree[2].equals(parse_sxpr('(e (X F))')) assert tree[-1].equals(parse_sxpr('(e (X F))')) try: node = tree[3] assert False, "IndexError expected!" Loading Loading
DHParser/syntaxtree.py +16 −0 Original line number Diff line number Diff line Loading @@ -426,6 +426,22 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil self._xml_attr = OrderedDict() return self._xml_attr def get_attr(self, attribute: str, default: str) -> str: """ Returns the value of 'attribute' if attribute exists. If not, the default value is returned. This function has the same semantics as `node.attr.get(attribute, default)`, but with the advantage then other than `node.attr.get` it does not automatically create an attribute dictionary on (first) access. :param attribute: The attribute, the value of which shall be looked up :param default: A default value that is returned, in case attribute does not exist. :return: str """ if self.has_attr(): return self.attr.get(attribute, default) return default def compare_attr(self, other: 'Node') -> bool: """ Returns True, if `self` and `other` have the same attributes with the Loading
test/test_syntaxtree.py +3 −2 Original line number Diff line number Diff line Loading @@ -273,8 +273,8 @@ class TestRootNode: str(result) class TestNodeFind(): """Test the select_if-functions of class Node. class TestNodeFind: """Test the item-access-functions of class Node. """ def test_find(self): Loading @@ -297,6 +297,7 @@ class TestNodeFind(): tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))') assert tree[0].equals(parse_sxpr('(b X)')) assert tree[2].equals(parse_sxpr('(e (X F))')) assert tree[-1].equals(parse_sxpr('(e (X F))')) try: node = tree[3] assert False, "IndexError expected!" Loading