本文整理汇总了Python中tree.Tree.add_node方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.add_node方法的具体用法?Python Tree.add_node怎么用?Python Tree.add_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.add_node方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: range
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [as 别名]
cursor = cnx.cursor()
query = ("SELECT * FROM imm_web_node_relation ")
cursor.execute(query)
(_ROOT, _DEPTH, _BREADTH) = range(3)
tree = Tree()
for (parent, child) in cursor:
print("parent: {}, child: {}".format(parent, child))
try:
tree[parent]
except KeyError:
tree.add_node(parent)
try:
tree[child]
except KeyError:
tree.add_node(child)
tree[parent].add_child(child)
tree[child].add_parent(parent)
query = ("SELECT id, name FROM imm_web")
cursor.execute(query)
id_name_dict = {}
for (id, name) in cursor:
id_name_dict[id] = name
示例2: Tree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [as 别名]
MLBD_all = []
# record the chosen nodes with the lowest MLBD of each iteration
nodes_all = []
# record the time for each iteration
time_iteration = []
print "Start optimizing..."
index=0
global num_node
num_node = 0
current_node = 0
#Claim a tree
tree = Tree()
node = tree.add_node(current_node, theta_L, theta_U)
num_node = num_node + 1
start_all = time.clock()
print x_all[-1]
while index < MAXITER:
start = time.clock()
print "----------------------------iteration %d---------------------------" % index
# Solve the subproblem
objOpt, thetaOpt, lamOpt, muOpt = sub.solve_subproblem(y, xBar) #(objOpt) upper bound
thetaBar.append(thetaOpt)
lamBar.append(lamOpt)
示例3: running_groups
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [as 别名]
if __name__ == "__main__":
from running_vms import running_vms
from running_groups import running_groups
from vm_snaps import vm_snaps
from tree import Tree
rgs = running_groups()
rg = rgs.pop()
snaps_tree = Tree()
snaps_tree.add_node(rg, None)
for vm in running_vms():
vm_snaps(vm, snaps_tree, rg)
snaps_tree.display(rg)
示例4: Copyright
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [as 别名]
# Copyright (C) by Brett Kromkamp 2011-2014 ([email protected])
# You Programming (http://www.youprogramming.com)
# May 03, 2014
from tree import Tree
(_ROOT, _DEPTH, _BREADTH) = range(3)
tree = Tree()
tree.add_node("Harry") # root node
tree.add_node("Jane", "Harry")
tree.add_node("Bill", "Harry")
tree.add_node("Joe", "Jane")
tree.add_node("Diane", "Jane")
tree.add_node("George", "Diane")
tree.add_node("Mary", "Diane")
tree.add_node("Jill", "George")
tree.add_node("Carol", "Jill")
tree.add_node("Grace", "Bill")
tree.add_node("Mark", "Jane")
tree.display("Harry")
print("***** DEPTH-FIRST ITERATION *****")
for node in tree.traverse("Harry"):
print(node)
print("***** BREADTH-FIRST ITERATION *****")
for node in tree.traverse("Harry", mode=_BREADTH):
print(node)
示例5: importData
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [as 别名]
def importData(fname, displayTree=False, colSep=',', headerLine=False, verbose=False):
"""
Import tree data from a CSV (text) file or list.
The data should be in the following format:
node_ID_number,node_parent_ID_number,data_item1,data_item2,...,data_itemN\n
The separator can, optionally, be a character other than ","
The root node must have a parent id of 0 and normally should also have an index of 1
From MATLAB one can produce tree structures and dump data in the correct format
using https://github.com/raacampbell13/matlab-tree and the tree.dumptree method
Inputs:
fname - if a string, importData assumes it is a file name and tries to load the tree from file.
if it is a list, importData assumes that each line is a CSV data line and tries to
convert to a tree.
displayTree - if True the tree is printed to standard output after creation
colSep - the data separator, a comma by default.
headerLine - if True, the first line is stripped off and considered to be the column headings.
headerLine can also be a CSV string or a list that defines the column headings. Must have the
same number of columns as the rest of the file.
verbose - prints diagnositic info to screen if true
"""
if verbose:
print "tree.importData importing file %s" % fname
#Error check
if isinstance(fname,str):
if os.path.exists(fname)==False:
print "Can not find file " + fname
return
#Read in data
fid = open(fname,'r')
contents = fid.read().split('\n')
fid.close()
elif isinstance(fname,list):
contents=fname #assume that fname is data rather than a file name
#Get header data if present
if headerLine==True:
header = contents.pop(0)
header = header.rstrip('\n').split(colSep)
elif isinstance(headerLine,str):
header = headerLine.rstrip('\n').split(colSep)
elif isinstance(headerLine,list):
header = headerLine
else:
header = False
data = []
for line in contents:
if len(line)==0:
continue
dataLine = line.split(colSep)
if len(header) !=len(dataLine):
print "\nTree file appears corrupt! header length is %d but data line length is %d.\ntree.importData is aborting.\n" % (len(header),len(dataLine))
return False
theseData = map(int,dataLine[0:2]) #add index and parent to the first two columns
#Add data to the third column. Either as a list or as a dictionary (if header names were provided)
if header != False: #add as dictionary
dataCol = dict()
for ii in range(len(header)-2):
ii+=2
dataCol[header[ii]]=dataTypeFromString.convertString(dataLine[ii])
else:
dataCol = dataLine[2:] #add as list of strings
theseData.append(dataCol)
data.append(theseData)
if verbose:
print "tree.importData read %d rows of data from %s" % (len(data),fname)
#Build tree
tree = Tree()
tree.add_node(0)
for thisNode in data:
tree.add_node(thisNode[0],thisNode[1])
tree[thisNode[0]].data = thisNode[2]
#Optionally dump the tree to screen (unlikely to be useful for large trees)
if displayTree:
tree.display(0)
#.........这里部分代码省略.........
示例6: range
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [as 别名]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#Generate an example if run from the command line
if __name__ == '__main__':
from tree import Tree
(_ROOT, _DEPTH, _WIDTH) = range(3)
print "\n\n --------- Tree of life --------- \n"
treeOfLife = Tree()
treeOfLife.add_node("Life") # root node
treeOfLife.add_node("Archaebacteria", "Life")
treeOfLife.add_node("Eukaryotes", "Life")
treeOfLife.add_node("Protista", "Eukaryotes")
treeOfLife.add_node("Plants", "Eukaryotes")
treeOfLife.add_node("Fungi", "Eukaryotes")
treeOfLife.add_node("Algae", "Plants")
treeOfLife.add_node("Mosses", "Plants")
treeOfLife.add_node("Ferns", "Plants")
treeOfLife.add_node("Animals", "Eukaryotes")
treeOfLife.add_node("Sponges","Animals")
treeOfLife.add_node("Flatworms","Animals")
treeOfLife.add_node("Arthropods","Animals")
treeOfLife.add_node("Insects","Arthropods")
treeOfLife.add_node("Crustaceans","Arthropods")
treeOfLife.add_node("Vertebrates","Animals")
示例7: DicomSR
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_node [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: []}
#.........这里部分代码省略.........