当前位置: 首页>>代码示例>>Python>>正文


Python Tree.add方法代码示例

本文整理汇总了Python中tree.Tree.add方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.add方法的具体用法?Python Tree.add怎么用?Python Tree.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tree.Tree的用法示例。


在下文中一共展示了Tree.add方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_new_tree_add_2_nodes_and_print_it

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add [as 别名]
 def test_new_tree_add_2_nodes_and_print_it(self):
     t = Tree()
     n = Node(title='test', id='1', parent_id='root')
     t.add(n)
     n = Node(title='test2', id='2', parent_id='1')
     t.add(n)
     print(t)
开发者ID:mkgilbert,项目名称:GDriveMgr,代码行数:9,代码来源:test_tree.py

示例2: test_new_tree_add_2_nodes_and_search_it

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add [as 别名]
 def test_new_tree_add_2_nodes_and_search_it(self):
     t = Tree()
     n = Node(title='test', id='1', parent_id='root')
     t.add(n)
     n = Node(title='test2', id='2', parent_id='1')
     t.add(n)
     #print(t)
     result = t.search('2')
     self.assertEqual(result.get_id(), '2')
开发者ID:mkgilbert,项目名称:GDriveMgr,代码行数:11,代码来源:test_tree.py

示例3: grow_trees

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add [as 别名]
def grow_trees(stream):
    tree = Tree()
    node_added = signal('node_added')
    for parent, child, created_at in stream:
        candidate = Edge(parent, child, created_at)
        if tree.should_add(candidate):
            tree.add(candidate)
            if tree.is_root(candidate):
                node_added.send('grow_trees', tree_name=tree.name, edge=candidate) 
            node_added.send('grow_trees', tree_name=tree.name, edge=candidate)
    return tree
开发者ID:wiredsister,项目名称:online_stats,代码行数:13,代码来源:compute_online.py

示例4: __init__

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add [as 别名]
class GDriveMgrHelper:
    """
    Takes care of actually building a local structure that matches Google
    Drive's online and downloading all the files and folders into their
    correct places in the hierarchy.
    It uses gdrivefunctions.py to handle the gdrive API calls and auth
    """

    def __init__(self, root_dir):
        """:param: root_dir = a node to build tree off of"""
        # initialize directory Tree structure
        self.dir_tree = Tree()
        # this will be initialized in main where we get credentials and stuff
        self._service = None
        # where we will store all dl'd files
        self.root_dir = root_dir

    def set_service(self, service):
        self._service = service

    def get_service(self):
        return self._service

    def _get_files_in_folder(self, folder_id):
        """ 
        Gets the metadata for all the children inside a Google Drive folder
        
        :param: folder_id = the Google Drive id of a folder

        :return: list of dicts that have all the file metadata
        """
        children_ids = gdrive.get_children(self.get_service(), folder_id)
        total = 0
        files_list = []
        for child_id in children_ids:
            metadata = gdrive.get_file_metadata(self.get_service(), child_id)
            if metadata["labels"]["trashed"]:
                continue  # the file was marked as trash, don't download

            # add file's info as tuple to the returned list
            title = metadata["title"]
            mime = metadata["mimeType"]
            if len(metadata["parents"]) != 0:
                # parent id is in a dict inside a list
                parents = metadata["parents"][0]
                if parents["isRoot"]:
                    parent_id = "root"
                else:
                    parent_id = parents["id"]
            else:
                # some files have an empty parents list
                parent_id = None

            files_list.append(
                {"id": child_id, "mime": mime, "title": title, "parent_id": parent_id, "metadata": metadata}
            )

            # logging to stdout
            total += 1
            print("Retrieved file " + str(total) + ". " + title + " (" + mime + ")")

        return files_list

    def create_tree(self):
        """
        Generate a Tree object using file metadata from all of the files in
        Google Drive. then save it as a pickled object inside the data directory.
        """
        self._rec_create_tree("root")  # Google API uses "root" as id of root folder
        # pickle the object to a file so we can reuse it as a template to download files
        dir_tree_path = os.path.join(gdrive.DATA_DIR, "dir_tree.pickle")
        with open(dir_tree_path, "wb") as f:
            pickle.dump(self.dir_tree, f)

    def _rec_create_tree(self, folder_id):
        """
        Helper to create directory tree object

        :param: folder_id = Google Drive id of a folder
        """
        if folder_id is None:
            return

        if folder_id == "root":
            print("")
            print("----------------------------------------")
            print(" Starting to create tree from Root")
            print("----------------------------------------")
            print("")

        files_list = self._get_files_in_folder(folder_id)  # tuple

        for f in files_list:
            # add the file to the folder with id 'folder_id'
            new_node = Node(id=f["id"], title=f["title"], parent_id=f["parent_id"], metadata=f["metadata"])

            result = self.dir_tree.add(new_node)

            if result == 0:
                # parent_id was None, so it shouldn't be added
#.........这里部分代码省略.........
开发者ID:mkgilbert,项目名称:GDriveMgr,代码行数:103,代码来源:gdrivemgrhelper.py

示例5: TestTreeMethods

# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add [as 别名]
class TestTreeMethods(unittest.TestCase):
    
    def setUp(self):
        self.n1 = Node(title='node1', id='1', parent_id='root')
        self.n2 = Node(title='node2', id='2', parent_id='1')
        self.n3 = Node(title='node3', id='3', parent_id='1')
        self.n4 = Node(title='node4', id='4', parent_id='2')
        self.n5 = Node(title='node5', id='5', parent_id='4') 
        
        # set up tree with multiple nodes
        self.t1 = Tree()
        self.t1.add(self.n1) # node1 has many children
        self.t1.add(self.n2)
        self.t1.add(self.n3)
        self.t1.add(self.n4)
        self.t1.add(self.n5)
        #print("Tree before the test:")
        #print(self.t1)
        
        # set up tree with only one node besides root
        self.n6 = Node('node6', '6', parent_id='root')
        self.one_node_tree = Tree()
        self.one_node_tree.add(self.n6)

    def tearDown(self):
        self.n1 = None
        self.n2 = None
        self.n3 = None
        self.n4 = None
        self.n5 = None
        self.n6 = None
        self.t1 = None
        self.t2 = None

    def test_get_root(self):
        self.assertEqual(self.t1.get_root().get_id(), 'root')

    def test_init_node_not_have_id_root(self):
        """ test init using a node who's id is not 'root'"""
        n = Node(title='foo', id=0)
        t = Tree(n)
        self.assertEqual(t.get_root().get_id(), 'root')

    def test_init_node_has_id_of_root(self):
        n = Node(title='foo', id='root')
        t = Tree(n)
        self.assertEqual(t.get_root().get_id(), 'root')
    
    def test_string_empty_tree(self):
        t2 = Tree(None)
        self.assertEqual(t2.__str__(), '|---Google_Drive\n')

    def test_string_non_empty_tree(self):
        print("You can't really test this...automatically")
        print(self.t1)

    def test_search_for_root(self):
        result = self.t1.search('root')
        self.assertTrue(result.get_id() == 'root')

    def test_search_for_first_node_added(self):
        result = self.t1.search('1')
        self.assertTrue(result.get_id() == '1')

    def test_search_for_nonexisting_node_in_one_node_tree(self):
        result = self.one_node_tree.search(self.n2.get_id())
        self.assertTrue(result == None)
    
    def test_new_tree_add_2_nodes_and_print_it(self):
        t = Tree()
        n = Node(title='test', id='1', parent_id='root')
        t.add(n)
        n = Node(title='test2', id='2', parent_id='1')
        t.add(n)
        print(t)

    def test_new_tree_add_2_nodes_and_search_it(self):
        t = Tree()
        n = Node(title='test', id='1', parent_id='root')
        t.add(n)
        n = Node(title='test2', id='2', parent_id='1')
        t.add(n)
        #print(t)
        result = t.search('2')
        self.assertEqual(result.get_id(), '2')

# From here down, tests are failing
    def test_search_for_nested_leaf_node(self):
        result = self.t1.search(self.n5.get_id())
        self.assertTrue('5' == result.get_id())

    def test_search_for_node1(self):
        result = self.t1.search(self.n1.get_id())
        self.assertTrue(result.get_id(), '1')

    def test_search_for_node2(self):
        result = self.t1.search(self.n2.get_id())
        self.assertTrue(result.get_id(), '2')

    def test_search_for_node3(self):
#.........这里部分代码省略.........
开发者ID:mkgilbert,项目名称:GDriveMgr,代码行数:103,代码来源:test_tree.py


注:本文中的tree.Tree.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。