3 """a dict preserving order of keys"""
6 dict.__init__(self,*args,**kw)
9 if not hasattr(args[0],
'iterkeys'):
12 for x,y
in iter(args[0]):
22 meat =
', '.
join([
'%s: %s' % (repr(key), repr(val))
for key,val
in self.
iteritems() ])
23 return '{' + meat +
'}'
29 dict.__setitem__(self, key, value)
30 if not key
in self.
list:
33 dict.__delitem__(self, key)
36 return [(key, dict.__getitem__(self, key))
for key
in self.
list]
39 yield key, dict.__getitem__(self, key)
45 yield dict.__getitem__(self,key)
49 return [ dict.__getitems__(self, key)
for key
in self.
list]
53 """a sorted dictionary with fixed/frozen keys"""
55 raise AttributeError,
"A SortedAndFixedKeysDict cannot be modified."
56 _blocked_attribute = property(_blocked_attribute)
57 __delitem__ = __setitem__ = clear = _blocked_attribute
58 pop = popitem = setdefault = update = _blocked_attribute
60 new = SortedKeysDict.__new__(cls)
61 SortedKeysDict.__init__(new, *args, **kw)
66 return "SortedAndFixedKeysDict(%s)" % SortedKeysDict.__repr__(self)
72 raise AttributeError,
"A FixedKeysDict cannot be modified."
73 _blocked_attribute = property(_blocked_attribute)
75 __delitem__ = __setitem__ = clear = _blocked_attribute
76 pop = popitem = setdefault = update = _blocked_attribute
78 new = dict.__new__(cls)
79 dict.__init__(new, *args, **kw)
84 return "FixedKeysDict(%s)" % dict.__repr__(self)
87 if __name__==
"__main__":
93 self.assertEqual(d[
'a'],1)
94 self.assertEqual(d[
'b'],[3])
95 self.assertRaises(AttributeError,operator.setitem,*(d,
'a',2))
97 self.assertEqual(d[
'b'],[3,2])
106 for key
in sd.iterkeys():
107 self.assertEqual(count,sd[key])
111 for key
in sd2.iterkeys():
112 self.assertEqual(count,sd2[key])
116 for key
in sd3.iterkeys():
117 self.assertEqual(count,sd3[key])
119 self.assertEqual(count-1,len(sd3))
122 for key
in sd3.iterkeys():
124 self.assertEqual(count-1,len(sd3))
126 self.assertEqual(5,sd[
'd'])
131 self.assertEqual(sd[
'a'],1)
132 self.assertEqual(sd[
'b'],[3])
133 self.assertRaises(AttributeError,operator.setitem,*(sd,
'a',2))
135 self.assertEqual([
'a',
'b'], [x
for x
in iter(sd)])
def testSortedAndFixedKeysDict
static std::string join(char **cmd)