Here's how to do what I recommended in my comment under you question — namely to do it by implementing a dictionary subclass that does what's needed via metadata it maintains internally about each of the values it contains.
This can be done fairly easily via the abstract base classes for containers in the collections.abc
module because it minimizes the number of methods that need to be implemented in the subclass. In this case abc.MutableMapping
was used at the base class.
Note that I've add a new method named prune()
that show how all entries older than a specified amount could be removed. In addition to that, I've left some extraneous calls to print()
in the code to make what it's doing clearer when testing it — which you'll probably want to remove.
from collections import abc
from datetime import datetime, timedelta
from operator import itemgetter
from pprint import pprint
from time import sleep
class LimitedDict(abc.MutableMapping):
LIMIT = 3
def __init__(self, *args, **kwargs):
self._data = dict(*args, **kwargs)
if len(self) > self.LIMIT:
raise RuntimeError(f'{type(self).__name__} initialized with more '
f'than the limit of {self.LIMIT} items.')
# Initialize value timestamps.
now = datetime.now()
self._timestamps = {key: now for key in self._data.keys()}
def __getitem__(self, key):
return self._data[key]
def __setitem__(self, key, value):
if key not in self: # Adding a key?
if len(self) >= self.LIMIT: # Will doing so exceed limit?
# Find key of oldest item and delete it (and its timestamp).
oldest = min(self._timestamps.items(), key=itemgetter(1))[0]
print(f'deleting oldest item {oldest=} to make room for {key=}')
del self[oldest]
# Add (or update) item and timestamp.
self._data[key], self._timestamps[key] = value, datetime.now()
def __delitem__(self, key):
"""Remove item and associated timestamp."""
del self._data[key]
del self._timestamps[key]
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
def prune(self, **kwargs) -> None:
""" Remove all items older than the specified maximum age.
Accepts same keyword arguments as datetime.timedelta - currently:
days, seconds, microseconds, milliseconds, minutes, hours, and weeks.
"""
max_age = timedelta(**kwargs)
now = datetime.now()
self._data = {key: value for key, value in self._data.items()
if (now - self._timestamps[key]) <= max_age}
self._timestamps = {key: self._timestamps[key] for key in self._data.keys()}
if __name__ == '__main__':
ld = LimitedDict(a=1, b=2)
pprint(ld._data)
pprint(ld._timestamps)
sleep(1)
ld['c'] = 3 # Add 3rd item.
print()
pprint(ld._data)
pprint(ld._timestamps)
sleep(1)
ld['d'] = 4 # Add an item that should cause limit to be exceeded.
print()
pprint(ld._data)
pprint(ld._timestamps)
ld.prune(seconds=2) # Remove all items more than 2 seconds old.
print()
pprint(ld._data)
pprint(ld._timestamps)