1st Sketch at at ReadOnly dict
Charles Hixson
charleshixsn at earthlink.net
Mon Jan 20 15:09:59 EST 2014
This is just a first sketch, and I haven't yet attempted to test it, so
what I'm hoping for is criticisms on the general approach.
## Read Only dict class.
# @note Instances can be created only from existing dicts.
# @warning values within the RODict can be accessed, so if they hold
# references to other items, they can be altered.
class RODict:
#Instance Variable Doc
## @var _ddict
# This variable holds the reference to the dict.
## Class initializer.
# @param ddict The data dictionary to which this is a read
only
# access.
# @throws TypeError if ddict is not a dict.
def __init__ (self, ddict = {}):
if not isinstance(ddict, dict):
raise TypeError("ddict must be a dict. It is " +
repr(ddict))
self._ddict = ddict
## Test for containment.
# @param key The item to be found.
# @return True If key is in the instance, otherwise False.
def __contains__(self, key):
return key in self._ddict
## Pass along the __getitem call.
def __getitem__(self, key):
return self._ddict.__getitem__(key)
## Pass along the get call.
def get (self, key, default = None):
return self._ddict.get(key, default)
## Return a DictView of the items of the instance.
def items(self):
return self._ddict.items()
## Return a DictView of the keys of the instance.
def keys(self):
return self._ddict.keys()
## Return a DictView of the values of the instance.
def values(self):
return self._ddict.values()
## Return the length of the dict.
def __len__(self):
return len(self._ddict)
--
Charles Hixson
More information about the Python-list
mailing list