def test_deep_tree(self): parser = NodeParser() parser.parse('(c1 ((c2 ((c3 ((c4)))))))') tree = parser.node() nodes = [] for node in depth_first_iterator(tree): nodes.append(node.name) self.assertEqual(nodes, ['c1', 'c2', 'c3', 'c4'])
def test_single_node(self): parser = NodeParser() parser.parse('(c1)') tree = parser.node() nodes = [] for node in depth_first_iterator(tree): nodes.append(node.name) self.assertEqual(nodes, ['c1'])
def test_empty_tree(self): parser = NodeParser() parser.parse('()') tree = parser.node() nodes = [] for node in depth_first_iterator(tree): nodes.append(node.name) self.assertEqual(nodes, [])
def main(): '''Function that will parse the given file and convert the tree to the format specified''' arg_parser = ArgumentParser(description='tree structured data ' 'converter') arg_parser.add_argument('file', type=FileType('r'), help='file to parse') arg_parser.add_argument('--format', type=str, default='string', help='format to convert to, default = string') options = arg_parser.parse_args() tree_str = '\n'.join(options.file.readlines()) options.file.close() node_parser = NodeParser() node_parser.parse(tree_str) tree = node_parser.node() if options.format == 'xml': writer = XmlWriter() elif options.format == 'relational': writer = RelationalWriter() else: writer = IndentedStringWriter() print(writer.write(tree))
def test_deep_tree(self): parser = NodeParser() parser.parse('(c1 ((c2 ((c3 ((c4)))))))') tree = parser.node() self.assertEqual(depth(tree), 4)
def test_single_node(self): parser = NodeParser() parser.parse('(c1)') tree = parser.node() self.assertEqual(depth(tree), 1)
def test_tree(self): parser = NodeParser() parser.parse('(c1 ((c2) (c3 ((c4) (c5))) (c6)))') tree = parser.node() self.assertEqual(depth(tree), 3)
def test_deep_tree(self): parser = NodeParser() parser.parse('(c1 ((c2 ((c3 ((c4)))))))') tree = parser.node() self.assertEqual(nr_leaf_nodes(tree), 1)
def test_empty_tree(self): parser = NodeParser() parser.parse('()') tree = parser.node() self.assertEqual(depth(tree), 0)
def test_tree(self): parser = NodeParser() parser.parse('(c1 ((c2) (c3 ((c4) (c5))) (c6)))') tree = parser.node() self.assertEqual(nr_leaf_nodes(tree), 4)
def test_empty_tree(self): parser = NodeParser() parser.parse('()') tree = parser.node() self.assertEqual(nr_leaf_nodes(tree), 0)