# -*- coding: utf-8 -*-tree.py模板
#
# Copyright (C) 2010-2013 Renstech Ltd. All Rights Reserved
#
# This file is part of supernova.
#
# Authors:
# 'zhouyifeng'
# pylint: disable-msg=E1101
import json
class Node:
def __init__(self, name, value, level=0):
self.name = name
self.value = value
self.level = level
self.url = None
self.target = '_self'
self.open = False
self.path = name
self.icon = None
self.iconClose = None
self.iconOpen = None
self.parent = None
self.children = []
@property
def isParent(self):
return len(self.children) > 0
def add_node(self, node):
"""
添加子节点
:param node: 所要添加的子节点
:type node: Node
"""
node.level = self.level + 1
node.parent = self
node.path = self.path + '/' + node.name
self.children.append(node)
def insert_node(self, index, node):
node.level = self.level + 1
node.parent = self
node.path = self.path + '/' + node.name
self.children.insert(index, node)
def remove_node(self, node):
self.children.remove(node)
def pop_node(self):
self.children.pop()
def clear(self):
self.children = []
def get_node(self, name):
for child in self.children:
if child.name == name:
return child
return None
def find_node(self, value):
for child in self.children:
if child.value == value:
return child
node = child.find_node(value)
if node:
return node
return None
def search_node(self, name):
for child in self.children:
if child.name == name:
return child
node = child.search_node(name)
if node:
return node
return None
def sort(self):
self.children.sort(lambda x, y: cmp(x.value, y.value))
for child in self.children:
child.sort()
def sort_by_name(self):
self.children.sort(lambda x, y: cmp(x.name, y.name))
for child in self.children:
child.sort_by_name()
def show(self, layer=0):
print " " * layer + self.name
map(lambda child: child.show(layer + 1), self.children)
def show_value(self, layer=0):
print " " * layer + self.value
map(lambda child: child.show(layer + 1), self.children)
def to_dict(self):
result = {'name': self.name,
'value': self.value,
'level': self.level,
'open': self.open,
'path': self.path}
if self.url:
result['url'] = self.url
result['target'] = self.target
if self.icon:
result['icon'] = self.icon
if self.iconClose:
result['iconClose'] = self.iconClose
if self.iconOpen:
result['iconOpen'] = self.iconOpen
if self.children:
result['children'] = [child.to_dict() for child in self.children]
return result
def to_json(self):
return json.dumps(self.to_dict(), indent=4)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Tree:
def __init__(self, name=None):
self.name = name
self.nodes = []
def to_json(self):
return json.dumps([node.to_dict() for node in self.nodes], indent=4)
def add_node(self, node):
"""
树中添加节点
:param node:所要添加的节点
:type node:Node
"""
node.level = 0
node.parent = self
self.nodes.append(node)
def insert_node(self, index, node):
node.level = 0
node.parent = self
self.nodes.insert(index, node)
def remove_node(self, node):
self.nodes.remove(node)
def pop(self):
self.nodes.pop()
def sort(self):
self.nodes.sort(lambda x, y: cmp(x.value, y.value))
for node in self.nodes:
node.sort()
def sort_by_name(self):
self.nodes.sort(lambda x, y: cmp(x.name, y.name))
for node in self.nodes:
node.sort_by_name()
def get_node(self, name):
for node in self.nodes:
if node.name == name:
return node
return None
def find_node(self, value):
for node in self.nodes:
if node.value == value:
return node
child_node = node.find_node(value)
if child_node:
return child_node
return None
def search_node(self, name):
for node in self.nodes:
if node.name == name:
return node
child_node = node.search_node(name)
if child_node:
return child_node
return None
def __unicode__(self):
return self.name
def __str__(self):
return self.name
if __name__ == '__main__':
root = Node("root", "root")
sub_node1 = Node("a1", "a1")
root.add_node(sub_node1)
sub_node1.add_node(Node("a2", "a2"))
sub_node1.add_node(Node("b2", "b2"))
sub_node2 = Node("b1", "b1")
root.add_node(sub_node2)
sub_node2.add_node(Node("d2", "d2"))
sub_node2.add_node(Node("c2", "c2"))
print root.to_json()
root.sort()
print root.to_json()
node = root.find_node("c2")
if node:
print node
else:
print "not find"
node = root.find_node("c3")
if node:
print node
else:
print "not find"
tree.py 模板
最新推荐文章于 2022-03-11 15:28:58 发布