Go to the documentation of this file.00001
00002
00003 import copy
00004
00005 class frozendict(dict):
00006 def _blocked_attribute(obj):
00007 raise AttributeError, "A frozendict cannot be modified."
00008 _blocked_attribute = property(_blocked_attribute)
00009
00010 __delitem__ = __setitem__ = clear = _blocked_attribute
00011 pop = popitem = setdefault = update = _blocked_attribute
00012
00013 def __new__(cls, *args, **kw):
00014 new = dict.__new__(cls)
00015
00016 args_ = []
00017 for arg in args:
00018 if isinstance(arg, dict):
00019 arg = copy.copy(arg)
00020 for k, v in arg.items():
00021 if isinstance(v, dict):
00022 arg[k] = frozendict(v)
00023 elif isinstance(v, list):
00024 v_ = list()
00025 for elm in v:
00026 if isinstance(elm, dict):
00027 v_.append( frozendict(elm) )
00028 else:
00029 v_.append( elm )
00030 arg[k] = tuple(v_)
00031 args_.append( arg )
00032 else:
00033 args_.append( arg )
00034
00035 dict.__init__(new, *args_, **kw)
00036 return new
00037
00038 def __init__(self, *args, **kw):
00039 pass
00040
00041 def __hash__(self):
00042 try:
00043 return self._cached_hash
00044 except AttributeError:
00045 h = self._cached_hash = hash(frozenset(self.items()))
00046 return h
00047
00048 def __repr__(self):
00049 return "frozendict(%s)" % dict.__repr__(self)
00050