1st Sketch at at ReadOnly dict

C

Charles Hixson

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top