1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
from collections.abc import Mapping
class Frozen(Mapping):
"""Wrapper around an object implementing the mapping interface to make it
immutable. If you really want to modify the mapping, the mutable version is
saved under the `_mapping` attribute.
"""
def __init__(self, mapping):
self._mapping = mapping
def __getitem__(self, key):
return self._mapping[key]
def __iter__(self):
return iter(self._mapping)
def __len__(self):
return len(self._mapping)
def __contains__(self, key):
return key in self._mapping
def __repr__(self):
return f"{type(self).__name__}({self._mapping!r})"
|