Raymond Hettinger added the comment:

If you're interested, here is a starting point for experimenting with any 
variations you want (invalidate a specific entry, changeable maxsize, 
pickle/unpickle, expiration of entries after a specific time, inspection of the 
internal contents, ability to inject known values, testing whether a specific 
argument tuple is in the cache, logging of cache access, or just about anything 
you could do with a regular dictionary):

    class LRU(OrderedDict):

        def __init__(self, func, maxsize=128):
            self.maxsize = 128
            self.func = func

        def __call__(self, *args):
            if args in self:
                value = self[args]
                self.move_to_end(args)
                return value
            value = self.func(*args)
            if len(self) >= self.maxsize:
                self.popitem(False)
            self[args] = value
            return value

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30153>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to