本文整理汇总了Python中tree.Tree.walk方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.walk方法的具体用法?Python Tree.walk怎么用?Python Tree.walk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.walk方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def main(schema=None, output_dir=None, config_path=None):
"""
Validate the schema file and output directory parameters.
Build a tree from the schema file.
Walk the tree, calling the registered callbacks on each node.
"""
validator = Validator(schema, output_dir=output_dir)
if not validator.validate():
click.echo(validator.error['msg'])
sys.exit(1)
directory_tree = Tree(
indent_size = validator.indent_size,
output_dir = output_dir,
base_path = os.path.abspath(os.curdir)
)
directory_tree.load_data(schema)
directory_tree.build_tree()
callbacks = [ pprint_node ]
if config_path:
process_hooks = make_config_processor(config_path)
callbacks.append(process_hooks)
directory_tree.walk(callbacks=callbacks)
示例2: show
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def show(self, result):
by_product = {}
by_id = {}
class TreeBug(dict):
def __init__(self, b):
for i in b:
self[i] = b[i]
def __str__(self):
res = ""
if 'notmybug' in self:
res = '\033[33m'
res += str(self['id']) + " "
res += str(self['priority']) + " "
res += str(self['estimated_time']) + " "
res += str(self['status'][:3]) + " "
res += str(self['severity'][:3]) + " "
res += str(self['summary'].encode('utf-8')) + " "
res += str(self['blocks']) + " "
creat = datetime.strptime(str(self['creation_time']).split('T')[0], "%Y%m%d").date()
creat += timedelta(days=severity_importance[self['severity']])
if self['severity'] != "enhancement":
res += str((creat - date.today()).days)
if 'notmybug' in self:
res += '\033[0m'
#res += str(self['blocklist']) + " "
return res
def equals(self, b):
return self['id'] == b['id']
def is_above(self, b):
return self.is_parent_of(b)
def is_parent_of(self, b):
return self['id'] in b['blocklist']
def update(self, b):
pass
class Walker(object):
def __init__(self):
self.parents = []
def _update_parents(self, bug):
while len(self.parents):
parent = self.parents[-1]
self.parents = self.parents[:-1]
if parent.is_parent_of(bug):
self.parents.append(parent)
break
class TreePrinter(Walker):
def __call__(self, bug):
self._update_parents(bug)
print '{} {}'.format(' ' * len(self.parents), str(bug))
self.parents.append(bug)
return True;
class TreeEstimator(Walker):
def __call__(self, bug):
self._update_parents(bug)
for parent in self.parents:
parent['estimated_time'] += bug['estimated_time']
self.parents.append(bug)
return True;
mybugs = []
for bug in result:
mybugs.append(TreeBug(bug))
result = None
for bug in mybugs:
by_product.setdefault(bug['product'], [])
by_product[bug['product']].append(bug)
by_id[bug['id']] = bug
for bug in mybugs:
res = []
self.addblock(by_id, bug, res)
bug['blocklist'] = res
for product in by_product:
print product
bugtree = Tree()
#by_product[product] = sorted(by_product[product], cmp=bugcmp)
for bug in sorted(by_product[product], cmp=bugcmp):
bugtree.insert(bug)
bugtree.walk(TreeEstimator())
bugtree.walk(TreePrinter())
print
示例3: test_walk
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def test_walk(self):
t = Tree("t", "T")
observed = list(t.walk())
expected = ['t']
self.assertEqual(observed, expected)
示例4: test_insert
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def test_insert(self):
t = Tree("t", "T")
t.insert('u', "U")
observed = list(t.walk())
expected = ['t', 'u']
self.assertEqual(observed, expected)
示例5: test_tree_init
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def test_tree_init(self):
t = Tree("D", 4)
self.assertEqual(list(t.walk()), ['D'])
示例6: test_tree_both_lr
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def test_tree_both_lr(self):
'Test tree with both L and R nodes. Also no data passed in.'
t = Tree('D')
for c in 'FGCAEB':
t.insert(c)
self.assertEqual(list(t.walk()), ['A', 'B', 'C', 'D', 'E', 'F', 'G'])
示例7: test_tree_no_left
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def test_tree_no_left(self):
t = Tree('D')
for c in 'FGE':
t.insert(c)
self.assertEqual(list(t.walk()), ['D', 'E', 'F', 'G'])
示例8: test_tree_no_right
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import walk [as 别名]
def test_tree_no_right(self):
t = Tree('D', 4)
for c, i in (('C', 3), ('A', 1), ('B', 2)):
t.insert(c, i)
self.assertEqual(list(t.walk()), ['A', 'B', 'C', 'D'])