annotate HashTree.py @ 116:f2be4e850d0c

AJAX slider takes height of loaded content.
author casties
date Wed, 29 May 2013 11:11:26 +0200
parents 014efa0923be
children afc96bc56817
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
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
33 def getSubtreeAsList(self, depthFirst=True):
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
34 """Return the subtree as flattened list sorted by key."""
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
35 if depthFirst:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
36 return self.getSubtreeAsListDepthFirst()
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
37 else:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
38 return self.getSubtreeAsListBreadthFirst()
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
39
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
40
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
41 def getSubtreeAsListDepthFirst(self):
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
42 """Return the subtree as flattened list, depth first, sorted by key."""
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
43 if self.children is None:
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
44 if self.value is None:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
45 return []
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
46 else:
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
47 return [self.value]
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
48
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
49 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
50 if self.value is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
51 sub = []
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
52 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
53 sub = [self.value]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
54
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
55 for k in sorted(self.children.keys()):
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
56 sub.extend(self.children.get(k).getSubtreeAsListDepthFirst())
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
57
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
58 return sub
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
59
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
60
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
61 def getSubtreeAsListBreadthFirst(self):
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
62 """Return the subtree as flattened list, breadth first, sorted by key."""
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
63 q = [self]
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
64 sub = []
115
014efa0923be removed limit from testing in getSubtreeAsList.
casties
parents: 114
diff changeset
65 while len(q) > 0:
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
66 node = q.pop(0)
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
67 if node.value is not None:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
68 sub.append(node.value)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
69
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
70 if node.children is not None:
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
71 clist = sorted(node.children.values(), key=lambda x:x.key)
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
72 q.extend(clist)
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
73
105
246d87d33f25 CLOSED - # 79: sortierung der liste der projekte pro abteilung
casties
parents: 97
diff changeset
74 return sub
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
75
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
76
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
77 def getSubtreeAsText(self):
114
1acfcaaa5ca3 fixed HashTree.getSubTreeAsList when using breadth first.
casties
parents: 105
diff changeset
78 """Return whole tree as text. Depth first."""
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
79 if self.children is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
80 return "(%s:%s)"%(self.key, self.value)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
81 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
82 sub = "(%s:%s):["%(self.key, self.value)
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
83 for k in sorted(self.children.keys()):
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
84 sub += self.children.get(k).getSubtreeAsText()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
85 sub += ", "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
86
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
87 return sub + "] "
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
88
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
89
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
90 class HashTree:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
91 """Tree using dictionaries"""
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
92
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
93 # root HashTreeNode
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
94 root = None
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
95 # separator by which key strings are split into parts
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
96 keySeparator = '.'
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
97 # function applied to key parts
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
98 keyFn = None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
99
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
100 def __init__(self, keySeparator='.', keyFn=None):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
101 """Create a HashTree.
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
102
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
103 @param keySeparator string by which key strings are split into parts
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
104 @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
105 """
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
106 self.root = HashTreeNode()
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
107 self.keySeparator = keySeparator
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
108 self.keyFn = keyFn
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
109
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
110
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
111 def _splitkey(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
112 """Return a list of key parts"""
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
113 if isinstance(key, basestring):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
114 # its a string - split
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
115 keys = key.split(self.keySeparator)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
116 if self.keyFn is not None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
117 keys = [self.keyFn(k) for k in keys]
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
118
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
119 elif isinstance(key, list):
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
120 # its a list - keep
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
121 keys = key
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
122
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
123 else:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
124 # not a list - wrap in list
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
125 keys = [key]
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
126
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
127 return keys
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
128
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
129
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
130 def getNode(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
131 """Return node under key from the tree.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
132
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
133 key can be sequence of key string or a single string using keySeparator.
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
134 If key is None, returns root node.
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
135 """
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
136 if key is None:
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
137 return self.root
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
138
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
139 keys = self._splitkey(key)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
140
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
141 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
142 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
143 node = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
144 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
145 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
146
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
147 return node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
148
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
149
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
150 def get(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
151 """Return value under key from the tree.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
152
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
153 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
154 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
155 node = self.getNode(key)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
156 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
157 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
158
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
159 return node.value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
160
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
161
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
162 def add(self, key, value):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
163 """Add value under key to the tree.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
164
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
165 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
166 """
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
167 keys = self._splitkey(key)
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
168 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
169 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
170 nextnode = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
171 if nextnode is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
172 nextnode = HashTreeNode(key=k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
173 node.addNode(nextnode)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
174
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
175 node = nextnode
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
176
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
177 node.value = value
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
178
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
179
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
180 def getChildrenOf(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
181 """Return the list of child (values) of the node under key."""
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
182 node = self.getNode(key)
97
7b96a85552aa fix bugs in project editing.
casties
parents: 52
diff changeset
183 if getattr(node, 'children', None) is None:
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
184 return []
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
185 else:
43
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
186 # sort children by key
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
187 childlist = sorted(node.children.items(), key=lambda x:x[0])
196db636a8fd fixed sorting of project lists.
casties
parents: 39
diff changeset
188 # and return the values
97
7b96a85552aa fix bugs in project editing.
casties
parents: 52
diff changeset
189 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
190
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
191
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
192 def getAncestorsOf(self, key):
52
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
193 """Return the list of ancestor (values) of the node under key.
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
194
1ed79b33200c more work on projects and cleanup.
casties
parents: 43
diff changeset
195 Order: root element first."""
39
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
196 keys = self._splitkey(key)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
197 node = self.root
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
198 ancestors = []
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
199 for k in keys:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
200 if node.value is not None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
201 ancestors.append(node.value)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
202
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
203 node = node.getNode(k)
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
204 if node is None:
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
205 return ancestors
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
206
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
207 return ancestors
bbad6a092861 more work on projects.
casties
parents: 34
diff changeset
208
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
209