CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DictTypes.py
Go to the documentation of this file.
1 # helper classes for sorted and fixed dicts
3  """a dict preserving order of keys"""
4  # specialised __repr__ missing.
5  def __init__(self,*args,**kw):
6  dict.__init__(self,*args,**kw)
7  self.list = list()
8  if len(args) == 1:
9  if not hasattr(args[0],'iterkeys'):
10  s = set()
11  #must protect against adding the same key multiple times
12  for x,y in iter(args[0]):
13  if x not in s:
14  self.list.append(x)
15  s.add(x)
16  else:
17  self.list = list(args[0].iterkeys())
18  return
19  self.list = list(super(SortedKeysDict,self).iterkeys())
20 
21  def __repr__(self):
22  meat = ', '.join([ '%s: %s' % (repr(key), repr(val)) for key,val in self.iteritems() ])
23  return '{' + meat + '}'
24 
25  def __iter__(self):
26  for key in self.list:
27  yield key
28  def __setitem__(self, key, value):
29  dict.__setitem__(self, key, value)
30  if not key in self.list:
31  self.list.append(key)
32  def __delitem__(self, key):
33  dict.__delitem__(self, key)
34  self.list.remove(key)
35  def items(self):
36  return [(key, dict.__getitem__(self, key)) for key in self.list]
37  def iteritems(self):
38  for key in self.list:
39  yield key, dict.__getitem__(self, key)
40  def iterkeys(self):
41  for key in self.list:
42  yield key
43  def itervalues(self):
44  for key in self.list:
45  yield dict.__getitem__(self,key)
46  def keys(self):
47  return self.list
48  def values(self):
49  return [ dict.__getitems__(self, key) for key in self.list]
50 
51 
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
59  def __new__(cls, *args, **kw):
60  new = SortedKeysDict.__new__(cls)
61  SortedKeysDict.__init__(new, *args, **kw)
62  return new
63  def __init__(self, *args, **kw):
64  pass
65  def __repr__(self):
66  return "SortedAndFixedKeysDict(%s)" % SortedKeysDict.__repr__(self)
67 
68 
69 #helper based on code from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/414283
72  raise AttributeError, "A FixedKeysDict cannot be modified."
73  _blocked_attribute = property(_blocked_attribute)
74 
75  __delitem__ = __setitem__ = clear = _blocked_attribute
76  pop = popitem = setdefault = update = _blocked_attribute
77  def __new__(cls, *args, **kw):
78  new = dict.__new__(cls)
79  dict.__init__(new, *args, **kw)
80  return new
81  def __init__(self, *args, **kw):
82  pass
83  def __repr__(self):
84  return "FixedKeysDict(%s)" % dict.__repr__(self)
85 
86 
87 if __name__=="__main__":
88  import unittest
89  class TestDictTypes(unittest.TestCase):
90  def testFixedKeysDict(self):
91  import operator
92  d = FixedKeysDict({'a':1, 'b':[3]})
93  self.assertEqual(d['a'],1)
94  self.assertEqual(d['b'],[3])
95  self.assertRaises(AttributeError,operator.setitem,*(d,'a',2))
96  d['b'].append(2)
97  self.assertEqual(d['b'],[3,2])
98 
99  def testSortedKeysDict(self):
100  sd = SortedKeysDict()
101  sd['a']=1
102  sd['b']=2
103  sd['c']=3
104  sd['d']=4
105  count =1
106  for key in sd.iterkeys():
107  self.assertEqual(count,sd[key])
108  count +=1
109  sd2 = SortedKeysDict(sd)
110  count =1
111  for key in sd2.iterkeys():
112  self.assertEqual(count,sd2[key])
113  count +=1
114  sd3 = SortedKeysDict([('a',1),('b',2),('c',3),('d',4)])
115  count =1
116  for key in sd3.iterkeys():
117  self.assertEqual(count,sd3[key])
118  count +=1
119  self.assertEqual(count-1,len(sd3))
120  sd3 = SortedKeysDict(a=1,b=2,c=3,d=4)
121  count =1
122  for key in sd3.iterkeys():
123  count +=1
124  self.assertEqual(count-1,len(sd3))
125  sd['d']=5
126  self.assertEqual(5,sd['d'])
127 
129  import operator
130  sd = SortedAndFixedKeysDict({'a':1, 'b':[3]})
131  self.assertEqual(sd['a'],1)
132  self.assertEqual(sd['b'],[3])
133  self.assertRaises(AttributeError,operator.setitem,*(sd,'a',2))
134  sd = SortedAndFixedKeysDict([('a',1), ('b',2),('a',3)])
135  self.assertEqual(['a','b'], [x for x in iter(sd)])
136  unittest.main()
static std::string join(char **cmd)
Definition: RemoteFile.cc:18