Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily keep a key and value in a dictionary

I have a python dictionary which is added with info from time to time.

Conditions:

  1. There should be 3 or less keys in the dictionary at all time. If there is a need to add another key-value pair, the earliest added key in the dictionary at that moment should be deleted and this new one should be added.
  2. Once a key-value is added, after 1 day, it should be deleted.

Example:

result = {}

def add_key(key, val):
    test = result.get(key) # Test to see if key already exists
    if test is None:
        if len(result.keys()) < 3:
            result[key] = val # This key and pair need to be deleted from the dictionary after
                            # 24 hours
        else:
            # Need code to remove the earliest added key.

add_key('10', 'object 2')

## Need another code logic to delete the key-value pair after 24 hours

How should I go about this issue?

like image 203
nifeco Avatar asked Dec 04 '25 06:12

nifeco


2 Answers

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)
like image 98
martineau Avatar answered Dec 07 '25 00:12

martineau


Here's one idea:

  • Subclass dict with your own class type (see this question).
  • In each function that deals with key, transform the key into a tuple of (datetime.now(), key).
  • At the beginning of each function, first do the 24-hour check and remove any expired items.
  • At the end of __setitem__ truncate the container down to 3 elements if necessary.
like image 37
0x5453 Avatar answered Dec 06 '25 23:12

0x5453