annotate HashTree.py @ 35:38cbbeaf266b

more work on projects. responsibleScientistsList has new format.
author casties
date Thu, 25 Apr 2013 21:39:57 +0200
parents b8ced08ebea9
children bbad6a092861
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
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))
29
224023958871 renamed method.
casties
parents: 27
diff changeset
51 sub.extend(self.children.get(k).getSubtreeAsList())
27
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.
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
88 If key is None, returns root node.
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
89 """
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
90 if key is None:
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
91 return self.root
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
92
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
93 if isinstance(key, basestring):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
94 keys = key.split(self.keySeparator)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
95 if self.keyFn is not None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
96 keys = [self.keyFn(k) for k in keys]
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
97
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
98 elif isinstance(key, list):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
99 keys = key
30
aa4ab114c28a more work on projects.
casties
parents: 29
diff changeset
100
27
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
101 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
102 keys = [key]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
103
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
104 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
105 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
106 #logging.debug("getNode node=%s k=%s"%(node, repr(k)))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
107 node = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
108 #logging.debug("getNode got:%s"%(node))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
109 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
110 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
111
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
112 return node
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
113
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
114
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
115 def get(self, key):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
116 """gets value under key from the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
117 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
118 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
119 node = self.getNode(key)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
120 if node is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
121 return None
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
122
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
123 return node.value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
124
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
125
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
126 def add(self, key, value):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
127 """adds value under key to the tree.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
128 key can be sequence of key string or a single string using keySeparator.
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
129 """
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
130 #logging.debug("add(key=%s, value=%s)"%(repr(key), value))
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
131 if isinstance(key, basestring):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
132 keys = key.split(self.keySeparator)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
133 if self.keyFn is not None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
134 keys = [self.keyFn(k) for k in keys]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
135 elif isinstance(key, list):
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
136 keys = key
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
137 else:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
138 keys = [key]
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
139
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
140 node = self.root
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
141 for k in keys:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
142 nextnode = node.getNode(k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
143 if nextnode is None:
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
144 nextnode = HashTreeNode(key=k)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
145 node.addNode(nextnode)
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
146
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
147 node = nextnode
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
148
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
149 node.value = value
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
150
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
151
9a75eb1b31b3 more work on projects.
casties
parents:
diff changeset
152