def test_increase_height_DataTree(self): """ Test increase_height function of DataTree. """ t1 = DataTree() self.assertEqual(t1.height(), 1) d1 = DataNode(1) d2 = DataNode(2) t1.root.add(d1) d1.add(d2) self.assertEqual(t1.height(), 3) t1.increase_height(1) self.assertEqual(t1.height(), 4) t1.increase_height(4) self.assertEqual(t1.height(), 8) self.assertRaises(TypeError, lambda: t1.increase_height('3')) self.assertRaises(ValueError, lambda: t1.increase_height(-4))
def help_xml_toTree(xml_node): """ Helps xml_ToTree function, walks through xml recursive Parameters ---------- xml_node: ElementType1 Returns ------- node: DataNode """ if xml_node.hasChildNodes(): tree_node = DataNode(xml_node.localName) for node in xml_node.childNodes: tree_node.add(help_xml_toTree(node)) return tree_node # TODO Add Attributes node = None if xml_node.nodeType == xml_node.TEXT_NODE: # TODO: guess xml_node.nodeValue == xml_node.data node = DataNode(xml_node.nodeValue.replace('\n ', '')) elif xml_node.nodeType == xml_node.ELEMENT_NODE: # TODO: guess xml_node.tagName == xml_node.localName node = DataNode(xml_node.localName) else: # TODO: Implement the other nodeTypes print('Warning: NodeType not supported yet') node = DataNode(xml_node.localName) return node
def csv_toTree(file, delimiter, ignore_index=True, options=None): """ Function transforms a csv file into a DataTree. Parameters ---------- file: file open input file in at least read mode delimiter: str ignore_index: bool, optional options: list, optional Returns ------- tree: DataTree """ # TODO add option column or row store csv_reader = csv.reader(file, delimiter=delimiter) tree = DataTree(tree_type='csv') i = 0 for row in csv_reader: row_node = DataNode('Row%s' % i) for col in row: col_node = DataNode(col) row_node.add(col_node) tree.root.add(row_node) i += 1 return tree
def txt_toTree(file, delimiter=None, options=None): """ Function transforms a txt file into a DataTree. Parameters ---------- file: file open input file in at least read mode delimiter: TODO options: list, optional Returns ------- tree: DataTree """ tree = DataTree(tree_type='txt') # TODO add more options # TODO add column or row storage col0 = DataNode('') for row in file.readlines(): col0.add(DataNode(row)) tree.root.add(col0) return tree
def test_height_DataNode(self): """ Test height function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.height(), 1) t1.add(DataNode(11)) self.assertEqual(t1.height(), 2)
def test_is_leaf_DataNode(self): """ Test is_leaf function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.is_leaf(), True) t2 = DataNode(2) t1.add(t2) self.assertEqual(t1.is_leaf(), False) self.assertEqual(t2.is_leaf(), True)
def test_minimize_height_DataNode(self): """ Test minimize_height function of DataNode. """ t1, t2, t3 = DataNode('1'), DataNode('2'), DataNode('3') t1.add(t2) t2.add(t3) self.assertEqual(t1.height(), 3) t1.minimize_height() self.assertEqual(t1.height(), 2) t1.minimize_height() self.assertEqual(t1.height(), 1)
def test_add_DataNode2(self): """ Test add function of DataNode. Test fail of add function """ t1 = DataNode('1') self.assertRaises(TypeError, lambda: t1.add("DataNode"))
def test_add_DataNode(self): """ Test add function of DataNode. """ t1 = DataNode('1') t2 = DataNode('2', children=[DataNode(21)]) t1.add(DataNode(11)) t1.add(DataNode(12)) t2.add(DataNode(22)) t2.add(DataNode(23)) self.assertEqual(len(t1.children), 2) self.assertEqual(len(t2.children), 3) self.assertEqual(t1.height(), 2) self.assertEqual(t2.height(), 2)
def test_simpleDataTree(self): """ Test Creation of simple DataTree. """ t1 = DataTree('type') self.assertEqual(t1.tree_type, 'type') d1 = DataNode(1) d2 = DataNode(2) d3 = DataNode(3) t1.root.add(d1) d1.add(d2) d2.add(d3) self.assertEqual(t1.height(), 4) self.assertIsNotNone(t1.root.children)
def test_txt_fromTree(self): """ Test the DataTree to txt function. """ # Prepare DataTree col0 = DataNode('') row0 = DataNode('Row0') row1 = DataNode('Row1') row2 = DataNode('Row2') row3 = DataNode('Row3') row4 = DataNode('Row4') tree = DataTree(tree_type='txt') col0.add(row0) col0.add(row1) col0.add(row2) col0.add(row3) col0.add(row4) tree.root.add(col0) # Run function outfile = StringIO() txt_fromTree(tree, outfile) outfile.seek(0) content = outfile.read() self.assertEqual(content, tree.generate_string_representation())
def test_minimize_height_DataTree(self): """ Test minimize_height function of DataTree. """ t1 = DataTree() self.assertEqual(t1.height(), 1) d1 = DataNode(1) d2 = DataNode(2) d3 = DataNode(3) d4 = DataNode(4) t1.root.add(d1) d1.add(d2) d2.add(d3) d3.add(d4) self.assertEqual(t1.height(), 5) t1.minimize_height(1) self.assertEqual(t1.height(), 4) t1.minimize_height(3) self.assertEqual(t1.height(), 1) self.assertRaises(ValueError, lambda: t1.minimize_height(7)) self.assertRaises(TypeError, lambda: t1.minimize_height('3'))
def test_height_DataTree(self): """ Test height function of DataTree. """ t1 = DataTree() self.assertEqual(t1.height(), 1) d1 = DataNode(1) d2 = DataNode(2) d3 = DataNode(3) d4 = DataNode(4) t1.root.add(d1) self.assertEqual(t1.height(), 2) d1.add(d2) self.assertEqual(t1.height(), 3) d2.add(d3) self.assertEqual(t1.height(), 4) t1.root.add(d4) self.assertEqual(t1.height(), 4)
def xlsx_toTree(file_name, options=None): """ Function transforms a xlsx file into a DataTree. Therefore it needs the path of the original file. Parameters ---------- file_name: str Returns ------- tree: DataTree """ # TODO: add options guess_type, data_only, keep_vba xlsx_wb = openpyxl.load_workbook(filename=file_name) tree = DataTree(tree_type='xlsx') # For each sheet create a node for sheet_name in xlsx_wb.sheetnames: sheet_node = DataNode('Sheet:' + sheet_name) sheet = xlsx_wb[sheet_name] # go through each cell in matrix max_rows x max_cols # and create tree like csv file i = 0 for row in sheet.rows: row_node = DataNode('Row%s' % i) i += 1 for cell in row: col_node = DataNode(str(cell.value)) row_node.add(col_node) sheet_node.add(row_node) tree.root.add(sheet_node) return tree
def test_remove_children_DataNode(self): """ Test remove_children function of DataNode. """ # Test it with t1 having children t1 = DataNode('t1') t2 = DataNode('t2') t1.add(t2) self.assertEqual(t1.is_leaf(), False) self.assertEqual(t2.is_leaf(), True) self.assertIsNotNone(t1.children) result = t1.remove_children() self.assertIsNotNone(result) self.assertEqual(type(result), type([])) self.assertEqual(len(result), 1) self.assertEqual(t1.children, None) # Testing it with t1 having no children self.assertEqual(None, t1.remove_children())