Home > Software engineering >  Temporarily keep a key and value in a dictionary
Temporarily keep a key and value in a dictionary

Time:01-06

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?

CodePudding user response:

I know this is only a partial answer to your question, but here is an approach for the first part of the question:

Define a variable holding the last added key on the same scope as the dictionary. Than you can remove said key in your else statement. Don't forget to update the variable each time you add a key.

CodePudding user response:

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.

CodePudding user response:

Inspired by the SO question Python Datatype for a fixed-length FIFO


One way of doing this is by using collections.

I created this short demo using collections.deque

We start by initializing the variable x to a list containing 5 empty dictionaries, and a max length of 5. Then creating the method add_v which uses the method appendleft to have it behave like a FIFO stack with a size limit of 5.

import collections

x = collections.deque([{}]*5, 5)


def add_key(key, value):
    x.appendleft({key: value})

Note: x will be a list containing dictionaries and not a dictionary containing entries.

CodePudding user response:

This code is my best effort at solving the first problem. It adds an indicator to the end of the they key and always deletes the earliest key when a new key is added:

global myDict
myDict = {}
globals()["currVal"] = 3
def add_key(key, val):
    if key not in [k[0:-1] for k in myDict.keys()]: # Test to see if key already exists
        if len(myDict.keys()) < 3:
            myDict[str(key)   str(len(myDict.keys()) 1)] = val # This key and pair need to be deleted from the dictionary after        
        else:
            deleteKeys = []
            for k in myDict:
                if k.endswith(str(globals()["currVal"]-2)):
                    deleteKeys.append(k) 
            myDict[str(key)   str(globals()["currVal"] 1)] = val
            for ks in deleteKeys:
                del myDict[ks]
            globals()["currVal"]  = 1
    return myDict

add_key('100', 'object 1')
print(myDict)
add_key('101', 'object 2')
print(myDict)
add_key('102', 'object 3')
print(myDict)
add_key('103', 'object 4')
print(myDict)
add_key('104', 'object 5')
print(myDict)
add_key('105', 'object 6')
print(myDict)

Output:

{'1001': 'object 1'}
{'1001': 'object 1', '1012': 'object 2'}
{'1001': 'object 1', '1012': 'object 2', '1023': 'object 3'}
{'1012': 'object 2', '1023': 'object 3', '1034': 'object 4'}
{'1023': 'object 3', '1034': 'object 4', '1045': 'object 5'}
{'1034': 'object 4', '1045': 'object 5', '1056': 'object 6'}

CodePudding user response:

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 reduces the number of methods that have to be implemented in the subclass.

Note 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
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:
            classname = type(self).__name__
            raise RuntimeError(f'{classname} initialized with too many items.')

        # Initialize value timestamps.
        now = datetime.now()
        self._timestamps = {key: now for key in self._data.keys()}

    def __getitem__(self, key):
        return self._data.__getitem__(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 (along with 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):
        del self._data[key]
        del self._timestamps[key]

    def __iter__(self):
        return self._data.__iter__()

    def __len__(self):
        return self._data.__len__()


if __name__ == '__main__':

    ld = LimitedDict(a=1, b=2)
    pprint(ld._data)
    pprint(ld._timestamps)
    sleep(1)
    print()
    ld['c'] = 3  # Add 3rd item.
    pprint(ld._data)
    pprint(ld._timestamps)
    sleep(1)
    print()
    ld['d'] = 4  # Add an item that should cause limit to be exceeded.
    pprint(ld._data)
    pprint(ld._timestamps)
  •  Tags:  
  • Related