Carroll, Barry wrote: >Greetings: > >I have a function that computes the checksum of an integer, including or >excluding digits in the computation based on the content of a mask string. >For example, > > cksum(123456789, '***...***') > >will do its computation on 123789, masking out the three non-significant >digits. > >My question concerns assigning the value of the mask string. The class that >defines the function also pre-defines several masks, say '*********', >'***...***', and '......***'. The masks have names: 'all', 'first-last', >'last'. Of these, 'all' is the most commonly used. The user may select one of >these masks, or may supply their own, arbitrary value. Further, the user can >choose to add their value to the group of pre-defines, and reuse that value >later in the session. (The user-defined mask is not saved between sessions; >no permanent storage is required.) > >So far, this structure looks like a dictionary. However, the user also wants >to access the group of pre-defined masks as if they were elements of a list: > > masks[0] returns '*********' > masks[1] returns '***...***' > >and so on. To make matters even more complex, if the user does not specify a >mask to use, the function should use the mask employed in the previous >invocation, defaulting to masks[0] if this is the first invocation. Finally, >the user can set a mask as 'default', which essentially marks a mask as 'last >used' without invoking the function. > >Is there a derived type or data structure in existence that implements these >capabilities (in descending order of importance? > > 1. Access by name (dict) > 2. Access by position (list) > 3. Addition of new items (dict, list) > 4. Set a 'last used' item (??????) > 5. Set a 'default' item (dict???) > >Thanks in advance for your help. > >Regards, > >Barry >[EMAIL PROTECTED] >541-302-1107 >________________________ >We who cut mere stones must always be envisioning cathedrals. >-Quarry worker's creed > > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Would the following fit the bill?
>>> class maskdict(dict): def __init__(self, seq={}): dict.__init__(self,{True:["*********","***...***","......***"],False:{"all":"*********","first-last":"***...***","last":"......***",None:"**********"}}) self.update(seq) def __getitem__(self, index=None): dict.__getitem__(self, isinstance(index,int))[index] self[None] = dict.__getitem__(self,isinstance(index,int))[index] return dict.__getitem__(self, False)[None] def __setitem__(self, index, value): if isinstance(index, int): return dict.__setitem__(self, isinstance(index,int), dict.__getitem__(self,isinstance(index,int))+[value]) return dict.__setitem__(dict.__getitem__(self, False), index, value) def setdef(self, default): self[None] = default >>> md = maskdict() >>> md[0] '*********' >>> md["all"] '*********' >>> md[1] '***...***' >>> md["first-last"] '***...***' >>> md[2] '......***' >>> md["last"] '......***' >>> md.__getitem__() # md[] results in a syntax error instead of passing None on to __getitem__ like you'd expect '......***' >>> md[1] '***...***' >>> md.__getitem__() '***...***' >>> md[0] '*********' >>> md.__getitem__() '*********' >>> md[3] = "****....." >>> md[3] '****.....' >>> md["first-four"] = md[3] >>> md["first-four"] '****.....' >>> md.setdef(md[3]) >>> md.__getitem__() '****.....' You'd still have to figure out how to integrate it in to your checksum function as well as make meaningful error messages, but it should give you a good start. Cheers, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor