本文整理汇总了Python中tree.Tree.print_tree方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.print_tree方法的具体用法?Python Tree.print_tree怎么用?Python Tree.print_tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.print_tree方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import print_tree [as 别名]
def main():
usage = 'python foldify.py source_file [destination_file] [--label] [--help]'
description = '''
======================================================
Foldify - CLI tools for managing directory Trees.
Version: {version}
Author: {author}
Operations:
## Print a directory tree ##
$ foldify directory1
## Label empty Folder in tree ##
$ foldify directory1 --label [update, remove]
## Copy a directory Tree ##
$ foldify directory1 directory2
## Create Json from a directory ##
$ foldify directory1 directory1.json
## Create Directory from json ##
$ foldify directory1.json directory1
======================================================
'''.format(version=__version__, author=__author__)
parser = argparse.ArgumentParser(prog='Foldify', description=description,
usage=usage,
formatter_class=help_formatter,
)
parser.add_argument('source_file', type=str,
help='Source filepath.')
parser.add_argument('-l', '--label', choices=['update', 'remove'],
help='Adds Label _EMPTY to empty folders.')
parser.add_argument('--custom-label', help='Customizes empty label.')
parser.add_argument('dest_file', type=str, nargs='?',
help='Destination filepath.')
parser.add_argument('-v', '--verbose', action='store_true')
args_dict = vars(parser.parse_args())
globals().update(args_dict)
# print(args_dict)
if verbose:
enable_debug()
# ensure source_file exists, if not exit.
exists = os.path.exists
if not exists(source_file):
print('=' * 40)
parser.print_help()
print('=' * 40)
print('ERROR: source_file does not exist.')
sys.exit()
# If no dest_file, print source_tree
if not dest_file:
if label:
t = generate_transactions(path=source_file, label=custom_label,
remove_all=bool(label == 'remove'))
apply_transactions(t)
else:
tree = Tree(source_file, json=is_json(source_file))
print('=' * 40)
tree.print_tree()
print('=' * 40)
sys.exit('Done.')
# if dest_file exists, prompt for overwite before continuing.
if exists(dest_file):
if label:
sys.exit('Must use only source_file with --label option.')
if input('WARNING: dest_file already exists. Overwrite? (y/n)') != 'y':
print('Exiting.')
print('=' * 40)
sys.exit()
# Copy folder, json from dir, or dir from json, depending on formats.
tree = Tree(source_file, json=is_json(source_file))
write_dest = tree.write_json if is_json(dest_file) else tree.write_tree
write_dest(dest_file)
示例2: DicomSR
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import print_tree [as 别名]
class DicomSR(object):
def __init__(self, report_type="", id_ontology=-1):
self.report_type = report_type
self.id_ontology = id_ontology
self.report = Tree()
def imprime(self):
""" Pretty print of a report """
print u"\n ------ {0} ---------- \n".format(self.report_type)
self.report.print_tree(0)
def add_node(self, node, parent):
self.report.add_node(node, parent)
def get_ontology(self):
""" Return current report ontology """
return self.id_ontology
def get_flat_data(self):
flat = {}
self.report.get_flat_tree(flat)
return flat
def get_root(self):
return self.report.value
def get_data_from_report(self, template_type, languages=None,
position=None, cardinality=None):
""" Return data from the report in a dictionary
Keyword arguments:
languages -- list of languages supported in the report.
template_type -- indicates the template type and
therefore the information to extract from the report.
self -- Dict Report with the information extracted from the dicom XML.
position -- dictionary where it's stored parent and
its children position
"""
substitution_words = {}
if (template_type in MULTIPLE_PROPERTIES.keys()):
if (template_type == DICOM_LEVEL):
# Get keys for this template
levels_tag = MULTIPLE_PROPERTIES[DICOM_LEVEL][0]
attrs_tag = MULTIPLE_PROPERTIES[DICOM_LEVEL][1]
level_name = MULTIPLE_PROPERTIES[DICOM_LEVEL][3]
level_num = MULTIPLE_PROPERTIES[DICOM_LEVEL][2]
code = MULTIPLE_PROPERTIES[DICOM_LEVEL][4]
meaning = MULTIPLE_PROPERTIES[DICOM_LEVEL][5]
#Init dictinary will hold the substitution.
# Keys are language code and values contains
# values to fill the template
for language in languages:
substitution_words[language] = {
levels_tag: [],
attrs_tag: []}
#Get container's concept and its attributes
containers = []
attributes = []
self.report.get_set_data(containers, attributes)
for container in containers:
ontology = get_ontology_level(
ontology_id=self.get_ontology(),
tree_level=container.get_level(),
languages_tag=language)
for language in languages:
aux = {}
aux[level_num] = container.get_level()
aux[level_name] = (unicode(ontology, "utf-8"))
aux[code] = container.get_code().lower()
aux[meaning] = container.get_meaning()[language]
substitution_words[language][levels_tag].\
append(aux.copy())
for attribute in attributes:
for language in languages:
aux = {}
aux[code] = attribute.code.lower()
aux[meaning] = attribute.meaning[language]
substitution_words[language][attrs_tag].\
append(aux.copy())
elif (template_type == CHILDREN_ARRAYS):
nodes_tag = MULTIPLE_PROPERTIES[CHILDREN_ARRAYS][0]
parent_tag = MULTIPLE_PROPERTIES[CHILDREN_ARRAYS][1]
children_tag = MULTIPLE_PROPERTIES[CHILDREN_ARRAYS][2]
# Get a flat version of the report
# TODO: check if the flat version of the tree is
# always the same
flat = {}
self.report.get_flat_tree(flat)
#Delete leaf items. They are not needed
flat = {key: flat[key] for key in flat if flat[key]}
if languages:
for language in languages:
substitution_words[language] = {nodes_tag: []}
for parent, children in flat.iteritems():
for language in languages:
aux = {parent_tag: parent.get_code().lower(),
children_tag: []}
#.........这里部分代码省略.........