[Tutor] Data Type with Dictionary and List Behavior
Barry.Carroll at psc.com
Tue Mar 28 01:51:03 CEST 2006
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,
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 returns '*********'
masks 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 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.
barry.carroll at psc.com
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
More information about the Tutor