本文整理汇总了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'))
示例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
示例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
示例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()
示例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
示例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()