当前位置: 首页>>代码示例>>Python>>正文


Python Node.get_child_by_id方法代码示例

本文整理汇总了Python中tree.Node.get_child_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Node.get_child_by_id方法的具体用法?Python Node.get_child_by_id怎么用?Python Node.get_child_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tree.Node的用法示例。


在下文中一共展示了Node.get_child_by_id方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from tree import Node [as 别名]
# 或者: from tree.Node import get_child_by_id [as 别名]
class TreeHandler:
	"""
	A class to construct a list to a tree structure.
	"""
	
	def __init__(self,nodesList, idFn, pidFn, rootId=None):
		""" Parameters:
		nodesList - the list to be constructed, each row in the nodesList should hold node's id and node's parent id bascally.
		idFn -  the function to get the node's from each row's data in the list
		pidFn-  the function to get the parent node's id from each row's data in the list
		rootId - the id of the root node, if it's gived, the nodesList holds a root node's info.
		"""
		self.nodesList = nodesList
		self.idFn = idFn
		self.pidFn= pidFn
		
		# create a root node first, the root node is the tree in fact.
		if rootId:
			self.rootNode = self.make_node(rootId)
		else:
			self.rootNode = Node('root')
			self.rootNode.data = []
			
		self.make_tree()
		return 
		
	def make_node(self,nodeId):
		"""
		Filters the row from self.nodesList by nodeId,
		and create a new Node.
		After node's creation,the row will be removed from the nodesList.
		"""
		row = filter( lambda i: self.idFn(i)==nodeId , self.nodesList)[0]
		#print 'make_node,row is %s, node id is %s'%(row,nodeId)
		node = Node(nodeId)
		node.data = row
		self.nodesList.remove(row)		
		return node
		
	def make_tree(self):
		"""
		Constructs the tree by iterating nodesList.
		"""
		while self.nodesList:
			row = self.nodesList.pop(0)
			nodeId = self.idFn(row)
			#print 'make_tree,row is %s, node id is %s'%(row,nodeId)
			
			try:
				node = self.rootNode.get_child_by_id(nodeId)
			except:
				node = None
			
			if not node:
				node = Node(nodeId)
				node.data = row
				#print 'make_tree,created a new node, id is ',node.id
				parentId = self.pidFn(row)	
					
				# creates it's parent node and recursivelly creates the ancestor nodes of this node.
				self.parent(parentId, node)
				
				#print '****make_tree is end, maked branch is ******\n',self.rootNode
				#print 70*'-'
		
		return
			
	def parent(self, parentId, child):
		"""
		Recursivelly creates all the ancestor nodes of the child.
		"""
		#print 'parent,parent node id is %s, child node id is %s, root node id is %s'%(parentId,child.id,self.rootNode.id)
		if not parentId:
			# if this node has not parent, it will be appented to the root node
			parentNode = self.rootNode
		else:
			try:
				parentNode = self.rootNode.get_node_by_id(parentId)
			except:
				parentNode = None
		
		#print 'parent(),parent node is ',parentNode
		
		if parentNode:
			parentNode.append(child)
		else:			
			parentNode = self.make_node(parentId)		
			parentNode.append(child)			 
			
			# recursive constructing this branch in the tree
			self.parent( self.pidFn(parentNode.data), parentNode)
			
		return
		
	def flatten(self):
		"""
		return the flatten tree nodes 
		"""
		return self.rootNode.flatten()
开发者ID:,项目名称:,代码行数:101,代码来源:


注:本文中的tree.Node.get_child_by_id方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。