In <ieu88s$hm...@reader1.panix.com> kj <no.em...@please.post> writes:

>Where in the Python documentation can one find the information
>required to determine the minimal[1] set of methods that one would
>need to override to achieve this goal?
>
I don't know the answer to that question, but imho it's
the wrong question to ask.  Instead you  should be asking what
design will let me implement the subclass making the fewest possible
assumptions about the superclass.  (hint: the design you're
thinking about leads to a world of pain.)
>
>2. other than the added capability described in (1), an instance
>of TSDict should behave *exactly* like a built-in dictionary.

(minor point: what about repr?  it shouldn't look like a dict, imho)

>
>#---------------------------------------------------------------------------
>from time import time

>class TSDict(dict):
>    def __setitem__(self, key, value):
>        # save the value and timestamp for key as a tuple;
>        # see footnote [2]
>        dict.__setitem__(self, key, (value, time()))

>    def __getitem__(self, key):
>        # extract the value from the value-timestamp pair and return it
>        return dict.__getitem__(self, key)[0]

>    def get_timestamp(self, key):
>        # extract the timestamp from the value-timestamp pair and return it
>        return dict.__getitem__(self, key)[1]
>#---------------------------------------------------------------------------


man, you're asking for trouble!  even if you knew all you want to know,
unless you had some guarantee that it wouldn't change in a later release,
you'd still have to override pretty much all the methods.  the problem
here is not lack of information, but a horrible design (sorry to be blunt).

Just keep the timestamps in a parallel dict and get on with life:

from time import time
from collections import MutableMapping

class TSDict2(dict, MutableMapping):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        self.__ts = dict.fromkeys(self.keys(), time())

    def __setitem__(self, key, value):
        self.__ts[key] = time()
        dict.__setitem__(self, key, value)

    setdefault = MutableMapping.setdefault
    update = MutableMapping.update

    def get_timestamp(self, key):
        if self.has_key(key):
            return self.__ts[key]
        raise KeyError(key)


that just took the time needed to type it and worked the first time:

>>> d = TSDict2((('uno', 1), ('dos', 2)), tres=3, cuatro=4); d['cinco'] = 5; 
>>> d.setdefault('six', 6);
6
>>> d.update((('tres', 'amigos'), ('seven', 7),), eight=8); d
{'seven': 7, 'six': 6, 'cuatro': 4, 'cinco': 5, 'eight': 8, 'dos': 2, 'tres': 
'amigos', 'uno': 1}
>>> for p in sorted([(k, d.get_timestamp(k)) for k in d.keys()], key=lambda p: 
>>> p[1]): print "%s\t%f" % p
... 
cuatro  1293131496.917215
dos     1293131496.917215
uno     1293131496.917215
cinco   1293131496.917233
six     1293131496.917279
tres    1293131501.676962
seven   1293131501.676974
eight   1293131501.676981


If you insist on bashing your skull against your original problem,
take a look at collections.OrderedDict or collections.Counter to
see how they use ABCs to tame dict.

(still, even if you used all the remaining available
MutableMapping methods in your class, i don't know how you'd
get the dict constructor to return the right value, ie no timestamps,
when you pass it an
instance of your subclass as argument.  i don't think
there's a TSDict.__method__ you can write for that... anyway
my TSDict2 doesn't have this problem either.)

take home message: respect the privacy of your superclasses and they'll
be nice to you.

--bill
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to