def get_tree_ast(str):
    """Возвращает AST
    """
    lst = get_ast(str)
    tree_ast = SimpleTree(TreeNode(None, None))
    new_lst = lst.copy()

    while len(new_lst) != 0:
        token = new_lst.pop(0)
        #print(token)
        if token == '(':
            tree_ast.add_child(TreeNode(tree_ast.current, None))
            tree_ast.current = tree_ast.current.child[0]
        elif token == ')':
            tree_ast.current = tree_ast.current.parent
        elif token != '/' and token != '*' and token != '-' and token != '+':
            tree_ast.current.value = token
            tree_ast.current = tree_ast.current.parent
        else:
            tree_ast.current.value = token
            #print('value =', tree_ast.current.value)
            #print('level =', tree_ast.current.level)
            tree_ast.add_child(TreeNode(tree_ast.current, None))
            tree_ast.current = tree_ast.current.child[1]

    return tree_ast
 def test_LevelCount(self):
     self.assertEqual(self.tree.LevelCount(self.tree.root), 0)
     
     self.assertEqual(self.tree.current.NodeValue, 5)
     self.assertEqual(self.tree.LevelCount(self.tree.current), 1)
     
     self.tree = SimpleTree(SimpleTreeNode(1000, None))
     self.assertEqual(self.tree.LevelCount(self.tree.root), 0)        
 def setUp(self):
     
     self.tree = SimpleTree(SimpleTreeNode(10, None))
     self.tree.AddChild(self.tree.current, SimpleTreeNode(5, None))
     self.tree.AddChild(self.tree.current, SimpleTreeNode(7, None))
     self.tree.AddChild(self.tree.current, SimpleTreeNode(8, None))
     self.tree.current = self.tree.current.Children[0]
     self.tree.AddChild(self.tree.current, SimpleTreeNode(12, None))
     self.tree.AddChild(self.tree.current, SimpleTreeNode(37, None))
Esempio n. 4
0
def construct_example_tree(selectable_nodes=True, children=2):
    # define a list of tree structures to be passed on to SimpleTree
    forrest = [construct_example_simpletree_structure(selectable_nodes,
                                                      children)]

    # stick out test tree into a SimpleTree and return
    return SimpleTree(forrest)
 def test_LeafCount(self):
     self.assertEqual(self.tree.LeafCount(), 4)  
     
     self.tree = SimpleTree(SimpleTreeNode(1000, None))
     self.assertEqual(self.tree.LeafCount(), 1)        
class TestSimpleTree(unittest.TestCase):
    
    def setUp(self):
        
        self.tree = SimpleTree(SimpleTreeNode(10, None))
        self.tree.AddChild(self.tree.current, SimpleTreeNode(5, None))
        self.tree.AddChild(self.tree.current, SimpleTreeNode(7, None))
        self.tree.AddChild(self.tree.current, SimpleTreeNode(8, None))
        self.tree.current = self.tree.current.Children[0]
        self.tree.AddChild(self.tree.current, SimpleTreeNode(12, None))
        self.tree.AddChild(self.tree.current, SimpleTreeNode(37, None))
        
    def test_add_child(self):
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 5, 12, 37, 7, 8]) 
        self.tree.current = self.tree.current.Parent.Children[1]
        self.tree.AddChild(self.tree.current, SimpleTreeNode(100, None))
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 5, 12, 37, 7, 100, 8])  
            
    def test_DeleteNode(self):
        self.assertEqual(self.tree.current.NodeValue, 5)
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 5, 12, 37, 7, 8])
        
        self.tree.DeleteNode(self.tree.current)
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 7, 8, 12, 37])  
       
    
    def test_GetAllNodes(self):
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 5, 12, 37, 7, 8]) 
        self.tree.AddChild(self.tree.current, SimpleTreeNode(100, None))
        
        self.tree = SimpleTree(SimpleTreeNode(1000, None))
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [1000])
        
        
    def test_FindNodesByValue(self):
        self.assertEqual([node.NodeValue for node in self.tree.FindNodesByValue(7)], [7]) 
        
        self.tree.AddChild(self.tree.current, SimpleTreeNode(7, None))
        self.assertEqual([node.NodeValue for node in self.tree.FindNodesByValue(7)], [7, 7]) 
        
    def test_MoveNode(self):
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 5, 12, 37, 7, 8]) 
        self.assertEqual(self.tree.current.NodeValue, 5) 
        
        self.tree.MoveNode(self.tree.current, self.tree.root.Children[-1])
        self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 7, 8, 5, 12, 37])
        
    def test_Count(self):
        self.assertEqual(self.tree.Count(), 6)
        
        self.tree = SimpleTree(SimpleTreeNode(1000, None))
        self.assertEqual(self.tree.Count(), 1)
                
    def test_LeafCount(self):
        self.assertEqual(self.tree.LeafCount(), 4)  
        
        self.tree = SimpleTree(SimpleTreeNode(1000, None))
        self.assertEqual(self.tree.LeafCount(), 1)        
        
    def test_LevelCount(self):
        self.assertEqual(self.tree.LevelCount(self.tree.root), 0)
        
        self.assertEqual(self.tree.current.NodeValue, 5)
        self.assertEqual(self.tree.LevelCount(self.tree.current), 1)
        
        self.tree = SimpleTree(SimpleTreeNode(1000, None))
        self.assertEqual(self.tree.LevelCount(self.tree.root), 0)        
 def test_GetAllNodes(self):
     self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [10, 5, 12, 37, 7, 8]) 
     self.tree.AddChild(self.tree.current, SimpleTreeNode(100, None))
     
     self.tree = SimpleTree(SimpleTreeNode(1000, None))
     self.assertEqual([node.NodeValue for node in self.tree.GetAllNodes()], [1000])
Esempio n. 8
0
 def __init__(self, content):
     if not isinstance(content, Tree):
         # do we need this?
         content = SimpleTree(content)
     self._tree = content
     self.root = self._tree.root
Esempio n. 9
0
                                            indent=10)

    # A SimpleTree, that contains the two above
    middletree = SimpleTree(
        [
            (FocusableText('Middle ROOT'),
             [
                 (FocusableText('Mid Child One'), None),
                 (FocusableText('Mid Child Two'), None),
                 (innertree, None),
                 (FocusableText('Mid Child Three'),
                  [
                      (FocusableText('Mid Grandchild One'), None),
                      (FocusableText('Mid Grandchild Two'), None),
                  ]
                  ),
                 (anotherinnertree,
                  # middletree defines a childnode here. This is usually
                  # covered by the tree 'anotherinnertree', unless the
                  # interepreting NestedTree's constructor gets parameter
                  # interpret_covered=True..
                  [
                      (FocusableText('XXX I\'m invisible!'), None),

                  ]),
             ]
             )
        ]
    )  # end SimpleTree constructor for middletree
    # use customized arrow decoration for middle tree
    middletree = ArrowTree(middletree,
                           arrow_hbar_char=u'\u2550',