本文整理汇总了Python中treelib.Tree.add_node方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.add_node方法的具体用法?Python Tree.add_node怎么用?Python Tree.add_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类treelib.Tree
的用法示例。
在下文中一共展示了Tree.add_node方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: node2TreeOfTwo
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
def node2TreeOfTwo(self,tuple1,tuple2):
t = Tree()
node1 = Node(tuple1,tuple1[0])
node2 = Node(tuple2,tuple2[0])
freq = tuple1[1] + tuple2[1]
tag = tuple1[0] + tuple2[0]
t.create_node("("+str(tag)+", " + str(freq)+")",tag,None)
if tuple1[1] >= tuple2[1]:
t.add_node(node1,tag)
t.add_node(node2,tag)
else:
t.add_node(node2,tag)
t.add_node(node1,tag)
return t
示例2: StateMachine
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
class StateMachine(object):
"""A class to track information about a state machine"""
def __init__(self, name):
self.name = name
self.events = {}
self.effects = {}
self.state_tree = Tree()
self.current_state = None
# Add the Root state automatically
self.add_state('Root')
def add_state(self, name):
assert isinstance(name, str)
state_node = Node(identifier=name, data=State(name))
if self.current_state is None:
self.state_tree.add_node(state_node)
self.current_state = state_node.data
else:
self.state_tree.add_node(state_node, self.current_state.name)
def add_event(self, ev):
assert isinstance(ev, Event)
self.events[ev.name] = ev
def add_effect(self, eff):
assert isinstance(eff, Effect)
self.effects[eff.name] = eff
def enter_state(self, state):
self.current_state = state
def exit_state(self, state):
self.current_state = self.state_tree.parent(state.name).data
def get_state_by_name(self, state_name):
return self.state_tree.get_node(state_name).data
示例3: init_phase
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
def init_phase(rubik, steps_required = []):
tree = Tree()
tree.add_node(Node(identifier = rubik.copy(), tag = {'steps': steps_required}))
return tree
示例4: load
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
def load(path):
gtype = None
graph = None
nodes = []
#TEMP: LOAD FILE FROM PATH#
file = open(path,"r",0)
#TEMP#
gtype = file.next()
gtype = int(gtype)
if (gtype == 0):
graph = Tree()
nodes = []
if (gtype == 1):
graph = digraph()
graph.nodeAttrs = []
graph.edgeAttrs = []
for linenum,line in enumerate(file):
if (linenum == 0):
parts = line.split()
nodeAttrs = parts[1].split(';')
numNodeAttrs = nodeAttrs.pop(0)
graph.nodeAttrs = nodeAttrs
continue
if (linenum == 1):
for node in line.split():
tokens = node.split(';')
index = tokens.pop(0)
if (isInt(index)):
index = int(index)
if (gtype == 0):
nodes.append(Node(identifier=index,data=tokens))
if (index == 0):
graph.add_node(nodes[0])
if (gtype == 1):
graph.add_node(index, attrs=tokens)
continue
if (linenum == 2):
if (gtype == 0):
numEdges = int(line)
if (gtype == 1):
parts = line.split()
lineAttrs = parts[1].split(';')
numLineAttrs = lineAttrs.pop(0)
continue
if (linenum > 2):
#CHECK THAT PARTS ARE INT
parts = line.split()
tail = int(parts[0])
head = int(parts[1])
if (gtype == 0):
graph.add_node(nodes[head],tail)
if (gtype == 1):
attributes = parts[2].split(';')
weight = attributes.pop(0)
graph.add_edge((tail,head),weight,attrs=attributes)
return graph
示例5: node2Tree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
def node2Tree(self,tuple1,tuple2,tuple3):
t = Tree()
#Id of each node = freq of that node(i[1])
#Node(tag,identifier(ID)) #create_node(tag,identifier(ID),parent)
#Nodes added in decreasing order of Frequency
node1 = Node(tuple1,tuple1[0])
node2 = Node(tuple2,tuple2[0])
node3 = Node(tuple3,tuple3[0])
freq = tuple1[1] + tuple2[1] + tuple3[1]
tag = tuple1[0] + tuple2[0] + tuple3[0]
t.create_node("("+str(tag)+", " + str(freq)+")",tag,None)
#Addded the nodes as left right mid according to there frequency
#so the first node has always the highest frequency
if tuple1[1] >= tuple2[1] and tuple1[1] >= tuple3[1]:
if tuple2[1] >= tuple3[1]:
t.add_node(node1,tag)
t.add_node(node2,tag)
t.add_node(node3,tag)
else:
t.add_node(node1,tag)
t.add_node(node3,tag)
t.add_node(node2,tag)
elif tuple2[1] >= tuple3[1] and tuple2[1] >= tuple1[1]:
if tuple1[1] >= tuple3[1]:
t.add_node(node2,tag)
t.add_node(node1,tag)
t.add_node(node3,tag)
else:
t.add_node(node2,tag)
t.add_node(node3,tag)
t.add_node(node1,tag)
else:
if tuple1[1] >= tuple2[1]:
t.add_node(node3,tag)
t.add_node(node1,tag)
t.add_node(node2,tag)
else:
t.add_node(node3,tag)
t.add_node(node2,tag)
t.add_node(node1,tag)
#t.show()
return t
示例6: load
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
def load(path):
gtype = None
graph = None
nodes = []
#TEMP: LOAD FILE FROM PATH#
file = open(path,"r",0)
#TEMP#
gtype = file.next()
#print isInt(gtype) #ASSERT LINE 0 IS AN INTEGER
gtype = int(gtype)
#print gtype
if (gtype == 0):
graph = Tree()
nodes = []
print 'GRAPH TYPE: TREE'
if (gtype == 1):
graph = digraph()
print 'GRAPH TYPE: EWD'
#print type(graph)
for linenum,line in enumerate(file):
if (linenum == 0):
parts = line.split()
#print parts[0]
nodeAttrs = parts[1].split(';')
numNodeAttrs = nodeAttrs.pop(0)
#print numNodeAttrs
#print isInt(numNodeAttrs)
#print numNodeAttrs, nodeAttrs
continue
if (linenum == 1):
for node in line.split():
tokens = node.split(';')
index = tokens.pop(0)
#print index #in final, assert numNodes == index
#print isInt(index)
if (isInt(index)):
index = int(index)
if (gtype == 0):
nodes.append(Node(identifier=index,data=tokens))
if (index == 0):
#print "ZERO"
#n = nodes[0]
#print type(n)
graph.add_node(nodes[0])
#graph.show()
#print graph.get_node(0)
if (gtype == 1):
graph.add_node(index, attrs=tokens)
#print tokens
continue
if (linenum == 2):
if (gtype == 0):
#print isInt(line) #ASSERT LINE IS A SINGLE INT
numEdges = int(line)
if (gtype == 1):
parts = line.split()
#print parts[0]
lineAttrs = parts[1].split(';')
numLineAttrs = lineAttrs.pop(0)
#print numLineAttrs
#print isInt(numLineAttrs)
#print numLineAttrs, lineAttrs
continue
if (linenum > 2):
#CHECK THAT PARTS ARE INT
parts = line.split()
tail = int(parts[0])
head = int(parts[1])
if (gtype == 0):
graph.add_node(nodes[head],tail)
if (gtype == 1):
attributes = parts[2].split(';')
weight = attributes.pop(0)
#check head and tail are integers
#check number attributes
#print (tail,head), weight, attributes
graph.add_edge((tail,head),weight,attrs=attributes)
if (gtype == 0):
graph.show()
if (gtype == 1):
for node in graph.nodes():
#.........这里部分代码省略.........
示例7: WarcFileSystem
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import add_node [as 别名]
class WarcFileSystem( LoggingMixIn, Operations ):
"""Filesystem built on a WARC's URI paths."""
def __init__( self, warc ):
self.warc = warc
logger.debug( "Mounting %s" % self.warc )
self.fh = WarcRecord.open_archive( warc, gzip="auto", mode="rb" )
self.tree = Tree()
self._get_records()
def _get_records( self ):
"""Parses a WARC, building a hierarchical tree."""
statinfo = os.stat( self.warc )
self.gid = statinfo.st_gid
self.uid = statinfo.st_uid
self.tree.create_node( self.warc, "/" )
self.records = {}
bar = progressbar.ProgressBar( maxval=statinfo.st_size, widgets=[ progressbar.Bar( "=", "[", "]"), " ", progressbar.Percentage() ] )
bar.start()
for( offset, record, errors ) in self.fh.read_records( limit=None ):
if record is not None and record.type != WarcRecord.WARCINFO:
parent = "/"
segments = [ record.type ] + re.split( "/+", record.url )
for e in segments:
identifier = "/".join( [ parent, e ] )
if not self.tree.contains( identifier ):
node = WarcRecordNode( record, offset, tag=e, identifier=identifier )
self.tree.add_node( node, parent=parent )
parent = identifier
self.records[ record.url ] = ( offset, record )
bar.update( offset )
bar.finish()
logger.debug( self.tree.show() )
# def access( self, path, amode ):
# logger.debug( path )
# raise FuseOSError( EPERM )
def chmod( self, path, mode ):
raise FuseOSError( EPERM )
def chown( self, path, uid, gid ):
raise FuseOSError( EPERM )
def create( self, path, mode ):
raise FuseOSError( EPERM )
def destroy( self, path ):
self.fh.close()
# def flush( self, path, fh ):
# raise FuseOSError( EPERM )
def fsync( self, path, datasync, fh ):
raise FuseOSError( EPERM )
def fsyncdir( self, path, datasync, fh ):
raise FuseOSError( EPERM )
def getattr( self, path, fh=None ):
"""Returns stat info for a path in the tree."""
logger.debug( path )
if path == "/":
stat = os.stat( self.warc )
return dict( [
( "st_mode", ( S_IFDIR | 0444 ) ),
( "st_ino", stat.st_ino ),
( "st_dev", stat.st_dev ),
( "st_nlink", stat.st_nlink ),
( "st_uid", stat.st_uid ),
( "st_gid", stat.st_gid ),
( "st_size", stat.st_size ),
( "st_ctime", stat.st_ctime ),
( "st_mtime", stat.st_mtime ),
( "st_atime", stat.st_atime )
] )
else:
return self.name_to_attrs( "/%s" % path )
def getxattr( self, path, name, position=0 ):
"""Returns the value for an extended attribute."""
if path != "/":
path = "/%s" % path
node = self.tree.get_node( path )
if node is None:
raise FuseOSError( ENOENT )
try:
return node.xattrs[ name ]
except KeyError:
raise FuseOSError( ENODATA )
def init( self, path ):
pass
def link( self, target, source ):
raise FuseOSError( EPERM )
def listxattr( self, path ):
"""Returns a list of extended attribute names."""
#.........这里部分代码省略.........