multidict.py (5467:6d9df90d70d7) | multidict.py (12563:8d59ed22ae79) |
---|---|
1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 12 unchanged lines hidden (view full) --- 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# Authors: Nathan Binkert 28 | 1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 12 unchanged lines hidden (view full) --- 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# Authors: Nathan Binkert 28 |
29from __future__ import print_function 30 |
|
29__all__ = [ 'multidict' ] 30 31class multidict(object): 32 def __init__(self, parent = {}, **kwargs): 33 self.local = dict(**kwargs) 34 self.parent = parent 35 self.deleted = {} 36 --- 74 unchanged lines hidden (view full) --- 111 try: 112 return self[key] 113 except KeyError: 114 self.deleted.pop(key, False) 115 self.local[key] = default 116 return default 117 118 def _dump(self): | 31__all__ = [ 'multidict' ] 32 33class multidict(object): 34 def __init__(self, parent = {}, **kwargs): 35 self.local = dict(**kwargs) 36 self.parent = parent 37 self.deleted = {} 38 --- 74 unchanged lines hidden (view full) --- 113 try: 114 return self[key] 115 except KeyError: 116 self.deleted.pop(key, False) 117 self.local[key] = default 118 return default 119 120 def _dump(self): |
119 print 'multidict dump' | 121 print('multidict dump') |
120 node = self 121 while isinstance(node, multidict): | 122 node = self 123 while isinstance(node, multidict): |
122 print ' ', node.local | 124 print(' ', node.local) |
123 node = node.parent 124 125 def _dumpkey(self, key): 126 values = [] 127 node = self 128 while isinstance(node, multidict): 129 if key in node.local: 130 values.append(node.local[key]) 131 node = node.parent | 125 node = node.parent 126 127 def _dumpkey(self, key): 128 values = [] 129 node = self 130 while isinstance(node, multidict): 131 if key in node.local: 132 values.append(node.local[key]) 133 node = node.parent |
132 print key, values | 134 print(key, values) |
133 134if __name__ == '__main__': 135 test1 = multidict() 136 test2 = multidict(test1) 137 test3 = multidict(test2) 138 test4 = multidict(test3) 139 140 test1['a'] = 'test1_a' --- 4 unchanged lines hidden (view full) --- 145 146 test2['a'] = 'test2_a' 147 del test2['b'] 148 test2['c'] = 'test2_c' 149 del test1['a'] 150 151 test2.setdefault('f', multidict) 152 | 135 136if __name__ == '__main__': 137 test1 = multidict() 138 test2 = multidict(test1) 139 test3 = multidict(test2) 140 test4 = multidict(test3) 141 142 test1['a'] = 'test1_a' --- 4 unchanged lines hidden (view full) --- 147 148 test2['a'] = 'test2_a' 149 del test2['b'] 150 test2['c'] = 'test2_c' 151 del test1['a'] 152 153 test2.setdefault('f', multidict) 154 |
153 print 'test1>', test1.items() 154 print 'test2>', test2.items() 155 #print test1['a'] 156 print test1['b'] 157 print test1['c'] 158 print test1['d'] 159 print test1['e'] | 155 print('test1>', test1.items()) 156 print('test2>', test2.items()) 157 #print(test1['a']) 158 print(test1['b']) 159 print(test1['c']) 160 print(test1['d']) 161 print(test1['e']) |
160 | 162 |
161 print test2['a'] 162 #print test2['b'] 163 print test2['c'] 164 print test2['d'] 165 print test2['e'] | 163 print(test2['a']) 164 #print(test2['b']) 165 print(test2['c']) 166 print(test2['d']) 167 print(test2['e']) |
166 167 for key in test2.iterkeys(): | 168 169 for key in test2.iterkeys(): |
168 print key | 170 print(key) |
169 170 test2.get('g', 'foo') 171 #test2.get('b') 172 test2.get('b', 'bar') 173 test2.setdefault('b', 'blah') | 171 172 test2.get('g', 'foo') 173 #test2.get('b') 174 test2.get('b', 'bar') 175 test2.setdefault('b', 'blah') |
174 print test1 175 print test2 176 print `test2` | 176 print(test1) 177 print(test2) 178 print(`test2`) |
177 | 179 |
178 print len(test2) | 180 print(len(test2)) |
179 180 test3['a'] = [ 0, 1, 2, 3 ] 181 | 181 182 test3['a'] = [ 0, 1, 2, 3 ] 183 |
182 print test4 | 184 print(test4) |