CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
frozendict.py
Go to the documentation of this file.
1 # frozendict clas, from http://code.activestate.com/recipes/414283-frozen-dictionaries/
2 
3 import copy
4 
5 class frozendict(dict):
7  raise AttributeError("A frozendict cannot be modified.")
8  _blocked_attribute = property(_blocked_attribute)
9 
10  __delitem__ = __setitem__ = clear = _blocked_attribute
11  pop = popitem = setdefault = update = _blocked_attribute
12 
13  def __new__(cls, *args, **kw):
14  new = dict.__new__(cls)
15 
16  args_ = []
17  for arg in args:
18  if isinstance(arg, dict):
19  arg = copy.copy(arg)
20  for k, v in arg.items():
21  if isinstance(v, dict):
22  arg[k] = frozendict(v)
23  elif isinstance(v, list):
24  v_ = list()
25  for elm in v:
26  if isinstance(elm, dict):
27  v_.append( frozendict(elm) )
28  else:
29  v_.append( elm )
30  arg[k] = tuple(v_)
31  args_.append( arg )
32  else:
33  args_.append( arg )
34 
35  dict.__init__(new, *args_, **kw)
36  return new
37 
38  def __init__(self, *args, **kw):
39  pass
40 
41  def __hash__(self):
42  try:
43  return self._cached_hash
44  except AttributeError:
45  h = self._cached_hash = hash(frozenset(self.items()))
46  return h
47 
48  def __repr__(self):
49  return "frozendict(%s)" % dict.__repr__(self)
50