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


Python Tree.to_json方法代码示例

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


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

示例1: TreePipeline

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import to_json [as 别名]
class TreePipeline(object):

    def open_spider(self, spider):
        self.tree = Tree()
        self.tree.create_node("root", "root")

    def process_item(self, item, spider):
        lst = item['text']
        lst = [x.strip() for x in [y.replace('...', '') for y in lst]]
        item['pagetitle'] = item['pagetitle'].replace('...', '')
        lst[-1] = item['pagetitle']
        for idx, elem in enumerate(lst):
            if idx == 0:
                previous = "root"
            else:
                previous = "|".join(lst[:idx])
            elem = "|".join(lst[:idx + 1])
            # elem = elem.replace('...', '')
            elem = elem.encode('utf-8').decode('utf-8')
            if not self.tree.contains(elem):
                print "Adding node %s" % elem
                self.tree.create_node(elem, elem, parent=previous)
                # self.tree.show()
        return item

    def close_spider(self, spider):
        self.tree.show()
        with open(makepath('data/cats/tree.json'), 'w') as outfile:
            outfile.write(self.tree.to_json())
        self.tree.save2file(makepath('data/cats/tree.tree'))
开发者ID:vlasy,项目名称:skool,代码行数:32,代码来源:pipelines.py

示例2: test_02_get_hierarchy_for_module_returns_single_node_when_nothing_depend_on_module

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import to_json [as 别名]
	def test_02_get_hierarchy_for_module_returns_single_node_when_nothing_depend_on_module(self, mock_client):
		"""
		Test that get_hierarchy_for_module returns a single node tree structure if no dependent modules are found
		:param mock_client: A mocked out version of erppeek.Client
		:return:
		"""
		# Mock Up
		mock_dp = DependencyGraph
		orig_mod_search = mock_dp.module_search
		orig_dep_search = mock_dp.dependency_search
		orig_client_search = mock_client.search
		mock_dp.module_search = MagicMock(return_value=[666])
		mock_dp.dependency_search = MagicMock(return_value=[])

		mock_dg = mock_dp('valid_module')
		test_hierarchy = Tree()
		test_hierarchy.create_node('valid_module', 'valid_module')
		self.assertEqual(mock_dg.hierarchy.to_json(), test_hierarchy.to_json(), 'get_hierarchy_for_module did not return [] when finding no dependent modules')

		# Mock Down
		mock_client.stop()
		mock_dp.module_search.stop()
		mock_client.search.stop()
		mock_client.search = orig_client_search
		mock_dp.dependency_search.stop()
		mock_dp.module_search = orig_mod_search
		mock_dp.dependency_search = orig_dep_search
开发者ID:Gimpneek,项目名称:odoo_dependency_graph,代码行数:29,代码来源:test_module_discovery.py

示例3: test_04_get_hierarchy_renames_duplicate_modules

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import to_json [as 别名]
	def test_04_get_hierarchy_renames_duplicate_modules(self, mock_client):
		"""
		Test that get_hierarchy_for_module renames the second instance of a dependent modules ID so can list that module multiple times
		- valid_module
		  - dependent_module_one
		  - dependent_module_two
		    - dependent_mdodule_one1

		:param mock_client: A mocked out version of erppeek.Client
		:return:
		"""
		# Mock Up
		mock_dp = DependencyGraph
		orig_mod_search = mock_dp.module_search
		orig_dep_search = mock_dp.dependency_search
		orig_client_search = mock_client.search
		mock_dp.module_search = MagicMock(return_value=[666])

		def dependency_search_side_effect(value):
			if value == 'valid_module':
				return [666]
			elif value == 'dependent_module_one':
				return [668]
			elif value == 'dependent_module_two':
				return [664]

		mock_dp.dependency_search = MagicMock()
		mock_dp.dependency_search.side_effect = dependency_search_side_effect

		def dependency_read_side_effect(value):
			if value == [666]:
				return ['dependent_module_one', 'dependent_module_two']
			elif value == [664]:
				return ['dependent_module_one']
			else:
				return []

		mock_dp.dependency_read = MagicMock()
		mock_dp.dependency_read.side_effect = dependency_read_side_effect

		mock_dg = mock_dp('valid_module')
		test_hierarchy = Tree()
		test_hierarchy.create_node('valid_module', 'valid_module')
		test_hierarchy.create_node('dependent_module_one', 'dependent_module_one', parent='valid_module')
		test_hierarchy.create_node('dependent_module_two', 'dependent_module_two', parent='valid_module')
		test_hierarchy.create_node('dependent_module_one', 'dependent_module_one1', parent='dependent_module_two')
		self.assertEqual(mock_dg.hierarchy.to_json(), test_hierarchy.to_json(), 'get_hierarchy_for_module did not return nested dict when finding dependent modules')

		# Mock Down
		mock_client.stop()
		mock_dp.module_search.stop()
		mock_client.search.stop()
		mock_client.search = orig_client_search
		mock_dp.dependency_search.stop()
		mock_dp.module_search = orig_mod_search
		mock_dp.dependency_search = orig_dep_search
开发者ID:Gimpneek,项目名称:odoo_dependency_graph,代码行数:58,代码来源:test_module_discovery.py

示例4: testUnjsonify

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import to_json [as 别名]
def testUnjsonify():
    tree = Tree()
    tree.create_node('home', 'home')
    tree.create_node('phone', 'phone', parent='home')
    tree.create_node('laptop', 'laptop', parent='home')
    tree.create_node('screen', 'screen', parent='laptop')
    tree.create_node(19, 19, parent='home')
    tree.create_node((1,2), (1,2), parent='screen')
    j = tree.to_json()
    unjsonify(j).show()
开发者ID:Totofufu,项目名称:treelib,代码行数:12,代码来源:test_plugins.py

示例5: test_03_get_hierarchy_for_module_returns_a_two_node_tree_when_another_module_depends_on_module

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import to_json [as 别名]
	def test_03_get_hierarchy_for_module_returns_a_two_node_tree_when_another_module_depends_on_module(self, mock_client):
		"""
		Test that get_hierarchy_for_module returns a tree structure with two nodes if a dependent module is found
		:param mock_client: A mocked out version of erppeek.Client
		:return:
		"""
		# Mock Up
		mock_dp = DependencyGraph
		orig_mod_search = mock_dp.module_search
		orig_dep_search = mock_dp.dependency_search
		orig_client_search = mock_client.search
		mock_dp.module_search = MagicMock(return_value=[666])

		def dependency_search_side_effect(value):
			if value == 'valid_module':
				return [666]
			elif value == 'dependent_module':
				return [668]

		mock_dp.dependency_search = MagicMock()
		mock_dp.dependency_search.side_effect = dependency_search_side_effect

		def dependency_read_side_effect(value):
			if value == [666]:
				return ['dependent_module']
			else:
				return []

		mock_dp.dependency_read = MagicMock()
		mock_dp.dependency_read.side_effect = dependency_read_side_effect

		mock_dg = mock_dp('valid_module')
		test_hierarchy = Tree()
		test_hierarchy.create_node('valid_module', 'valid_module')
		test_hierarchy.create_node('dependent_module', 'dependent_module', parent='valid_module')
		self.assertEqual(mock_dg.hierarchy.to_json(), test_hierarchy.to_json(), 'get_hierarchy_for_module did not return nested dict when finding dependent modules')

		# Mock Down
		mock_client.stop()
		mock_dp.module_search.stop()
		mock_client.search.stop()
		mock_client.search = orig_client_search
		mock_dp.dependency_search.stop()
		mock_dp.module_search = orig_mod_search
		mock_dp.dependency_search = orig_dep_search
开发者ID:Gimpneek,项目名称:odoo_dependency_graph,代码行数:47,代码来源:test_module_discovery.py

示例6: tree_test

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import to_json [as 别名]
def tree_test():
  test_titles = [
    "deletion_mapping", 
    "orthotheca", 
    "genetically_modified_sperm", 
    "category:intelligence"
  ]
  titles = dict((e, idx) for idx, e in enumerate(test_titles))

  # Tree testing
  t = Tree()
  t.create_node("deletion_mapping",15)
  t.create_node("orthotheca",14, parent=15)
  t.create_node("genetically_modified_sperm",13, parent=14)
  t.create_node("category:intelligence",12, parent=14)
  t.show()

  json = t.to_json()
  print "\nAs JSON:"
  print json

  print "\nAnd parsed back into a tree."
  t2 = from_json(json, titles)
  t2.show()
开发者ID:sacry-,项目名称:AdaptiveAgents,代码行数:26,代码来源:trees.py


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