Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(self,': 0.09; '__init__': 0.09; 'false.': 0.09; 'instance.': 0.09; 'none):': 0.09; 'def': 0.12; 'attempted': 0.16; 'charles': 0.16; 'default)': 0.16; 'dict': 0.16; 'dictview': 0.16; 'items(self):': 0.16; 'key):': 0.16; 'keys(self):': 0.16; 'message-id:@earthlink.net': 0.16; 'received:dsl.mindspring.com': 0.16; 'variable': 0.18; 'header :User-Agent:1': 0.23; 'instance,': 0.24; "haven't": 0.24; 'class.': 0.26; 'holds': 0.26; 'references': 0.26; 'pass': 0.26; 'values': 0.27; 'raise': 0.29; 'along': 0.30; "i'm": 0.30; 'doc': 0.31; 'keys': 0.31; 'class': 0.32; 'call.': 0.33; 'skip:_ 10': 0.34; 'received:66': 0.35; 'created': 0.35; 'test': 0.35; 'found.': 0.36; 'instances': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'length': 0.61; 'first': 0.61; 'within': 0.65; 'default': 0.69; 'hoping': 0.75; 'dict.': 0.84; 'approach.': 0.91; 'items,': 0.91 Date: Mon, 20 Jan 2014 12:09:59 -0800 From: Charles Hixson User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: python-list@python.org Subject: 1st Sketch at at ReadOnly dict Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390248690 news.xs4all.nl 2848 [2001:888:2000:d::a6]:47758 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64373 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