|
List Info
Thread: Data Type with Dictionary and List Behavior
|
|
| Data Type with Dictionary and List
Behavior |

|
2006-03-27 23:51:03 |
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
barry.carroll psc.com
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
|
|
| Data Type with Dictionary and List
Behavior |

|
2006-03-28 00:33:46 |
On 28/03/06, Carroll, Barry <Barry.Carroll psc.com> wrote:
> 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???)
Probably your best bet is to define your own container
class. If you
define __getitem__, you can use [] with instances of your
class, just
like lists or dicts.
Something like this, perhaps (untested):
class MaskDict(UserDict.DictMixin):
def __init__(self):
self.keys = []
self.data = {}
self.default = None
def __getitem__(self, key):
if isinstance(key, (int, long)):
self.default = self.keys[key]
return self.data[self.default]
else:
self.default = key
return self.data[key]
def __setitem__(self, key, value):
try:
self.keys.remove(key)
except ValueError:
pass
self.keys.append(key)
self.data[key] = value
def __delitem__(self, key, value):
del self.data[key]
self.keys.remove(key)
def keys(self):
return self.keys
def setDefault(self, key):
self.default = key
HTH!
--
John.
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Data Type with Dictionary and List
Behavior |

|
2006-03-28 21:24:04 |
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
>barry.carroll psc.com
>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&qu
ot;:"*********","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
|
|
[1-3]
|
|