annotate HashTree.py @ 27:9a75eb1b31b3

more work on projects.
author casties
date Mon, 22 Apr 2013 21:01:00 +0200
parents
children 224023958871
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
1 import logging
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
2
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
3 class HashTreeNode:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
4 """Node of a HashTree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
5 Has a value and keeps children in a dict indexed by key."""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
6 key = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
7 value = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
8 children = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
9
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
10 def __init__(self, key=None, value=None, children=None):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
11 """create node"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
12 self.key = key
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
13 self.value = value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
14 self.children = children
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
15
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
16
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
17 def getNode(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
18 """return node under key"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
19 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
20 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
21 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
22 return self.children.get(key, None)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
23
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
24
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
25 def addNode(self, node):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
26 """add child node using key from node"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
27 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
28 self.children = {node.key : node}
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
29 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
30 self.children[node.key] = node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
31
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
32
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
33 def getSubtreeList(self):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
34 """returns the subtree as list sorted by key (depth first)"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
35 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
36 if self.value is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
37 return []
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
38 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
39 return [self.value]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
40
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
41 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
42 if self.value is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
43 sub = []
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
44 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
45 sub = [self.value]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
46
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
47 keys = self.children.keys()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
48 keys.sort()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
49 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
50 #logging.debug("getSubtreeList: k=%s sub=%s"%(k,sub))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
51 sub.extend(self.children.get(k).getSubtreeList())
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
52
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
53 return sub
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
54
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
55
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
56 def getSubtreeAsText(self):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
57 """prints whole tree as text"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
58 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
59 return "(%s:%s)"%(self.key, self.value)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
60 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
61 sub = "(%s:%s):["%(self.key, self.value)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
62 keys = self.children.keys()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
63 keys.sort()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
64 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
65 sub += self.children.get(k).getSubtreeAsText()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
66 sub += ", "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
67
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
68 return sub + "] "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
69
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
70
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
71 class HashTree:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
72 """Tree using dictionaries"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
73
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
74 root = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
75 keySeparator = '.'
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
76 keyFn = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
77
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
78 def __init__(self, keySeparator='.', keyFn=None):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
79 """creates a HashTree"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
80 self.root = HashTreeNode()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
81 self.keySeparator = keySeparator
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
82 self.keyFn = keyFn
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
83
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
84
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
85 def getNode(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
86 """gets node under key from the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
87 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
88 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
89 #logging.debug("getNode(key=%s)"%(key))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
90 if isinstance(key, basestring):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
91 keys = key.split(self.keySeparator)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
92 if self.keyFn is not None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
93 keys = [self.keyFn(k) for k in keys]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
94 elif isinstance(key, list):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
95 keys = key
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
96 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
97 keys = [key]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
98
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
99 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
100 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
101 #logging.debug("getNode node=%s k=%s"%(node, repr(k)))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
102 node = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
103 #logging.debug("getNode got:%s"%(node))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
104 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
105 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
106
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
107 return node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
108
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
109
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
110 def get(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
111 """gets value under key from the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
112 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
113 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
114 node = self.getNode(key)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
115 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
116 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
117
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
118 return node.value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
119
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
120
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
121 def add(self, key, value):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
122 """adds value under key to the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
123 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
124 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
125 #logging.debug("add(key=%s, value=%s)"%(repr(key), value))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
126 if isinstance(key, basestring):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
127 keys = key.split(self.keySeparator)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
128 if self.keyFn is not None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
129 keys = [self.keyFn(k) for k in keys]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
130 elif isinstance(key, list):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
131 keys = key
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
132 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
133 keys = [key]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
134
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
135 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
136 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
137 nextnode = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
138 if nextnode is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
139 nextnode = HashTreeNode(key=k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
140 node.addNode(nextnode)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
141
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
142 node = nextnode
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
143
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
144 node.value = value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
145
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
146
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
147