annotate HashTree.py @ 48:f59bdd5f4890

Merge with 5c6ad316e1ceef48e323907ab81dd50e7ef743b2
author dwinter
date Mon, 29 Apr 2013 16:02:24 +0200
parents 196db636a8fd
children 1ed79b33200c
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
29
224023958871 renamed method.
casties
parents: 27
diff changeset
33 def getSubtreeAsList(self):
34
b8ced08ebea9 working on projects.
casties
parents: 30
diff changeset
34 """returns the subtree as flattened list sorted by key (depth first)"""
27
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
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
47 for k in sorted(self.children.keys()):
29
224023958871 renamed method.
casties
parents: 27
diff changeset
48 sub.extend(self.children.get(k).getSubtreeAsList())
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
49
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
50 return sub
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
51
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
52
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
53 def getSubtreeAsText(self):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
54 """prints whole tree as text"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
55 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
56 return "(%s:%s)"%(self.key, self.value)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
57 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
58 sub = "(%s:%s):["%(self.key, self.value)
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
59 for k in sorted(self.children.keys()):
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
60 sub += self.children.get(k).getSubtreeAsText()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
61 sub += ", "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
62
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
63 return sub + "] "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
64
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
65
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
66
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
67
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
68 class HashTree:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
69 """Tree using dictionaries"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
70
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
71 # root HashTreeNode
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
72 root = None
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
73 # separator by which key strings are split into parts
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
74 keySeparator = '.'
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
75 # function applied to key parts
27
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):
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
79 """creates a HashTree.
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
80
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
81 @param keySeparator string by which key strings are split into parts
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
82 @param keyFn function applied to key parts (e.g. int if key parts are numbers)
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
83 """
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
84 self.root = HashTreeNode()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
85 self.keySeparator = keySeparator
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
86 self.keyFn = keyFn
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
87
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
88
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
89 def _splitkey(self, key):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
90 """returns a list of key parts"""
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
91 if isinstance(key, basestring):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
92 # its a string - split
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
93 keys = key.split(self.keySeparator)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
94 if self.keyFn is not None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
95 keys = [self.keyFn(k) for k in keys]
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
96
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
97 elif isinstance(key, list):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
98 # its a list - keep
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
99 keys = key
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
100
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
101 else:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
102 # not a list - wrap in list
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
103 keys = [key]
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
104
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
105 return keys
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
106
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
107
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
108 def getNode(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
109 """gets node under key from the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
110 key can be sequence of key string or a single string using keySeparator.
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
111 If key is None, returns root node.
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
112 """
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
113 if key is None:
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
114 return self.root
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
115
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
116 keys = self._splitkey(key)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
117
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
118 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
119 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
120 node = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
121 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
122 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
123
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
124 return node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
125
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
126
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
127 def get(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
128 """gets value under key from the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
129 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
130 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
131 node = self.getNode(key)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
132 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
133 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
134
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
135 return node.value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
136
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
137
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
138 def add(self, key, value):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
139 """adds value under key to the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
140 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
141 """
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
142 keys = self._splitkey(key)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
143 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
144 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
145 nextnode = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
146 if nextnode is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
147 nextnode = HashTreeNode(key=k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
148 node.addNode(nextnode)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
149
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
150 node = nextnode
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
151
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
152 node.value = value
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
153
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
154
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
155 def getChildrenOf(self, key):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
156 """returns the list of child (values) of the node under key."""
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
157 node = self.getNode(key)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
158 if node.children is None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
159 return []
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
160 else:
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
161 # sort children by key
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
162 childlist = sorted(node.children.items(), key=lambda x:x[0])
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
163 # and return the values
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
164 return [n.value for (k, n) in childlist if n.value is not None]
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
165
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
166
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
167 def getAncestorsOf(self, key):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
168 """returns the list of ancestor (values) of the node under key.
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
169 ordered from root up."""
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
170 keys = self._splitkey(key)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
171 node = self.root
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
172 ancestors = []
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
173 for k in keys:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
174 if node.value is not None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
175 ancestors.append(node.value)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
176
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
177 node = node.getNode(k)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
178 if node is None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
179 return ancestors
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
180
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
181 return ancestors
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
182
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
183