Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64373 > unrolled thread
| Started by | Charles Hixson <charleshixsn@earthlink.net> |
|---|---|
| First post | 2014-01-20 12:09 -0800 |
| Last post | 2014-01-20 12:09 -0800 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
1st Sketch at at ReadOnly dict Charles Hixson <charleshixsn@earthlink.net> - 2014-01-20 12:09 -0800
| From | Charles Hixson <charleshixsn@earthlink.net> |
|---|---|
| Date | 2014-01-20 12:09 -0800 |
| Subject | 1st Sketch at at ReadOnly dict |
| Message-ID | <mailman.5757.1390248690.18130.python-list@python.org> |
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
Back to top | Article view | comp.lang.python
csiph-web