Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python OrderedDict - LRU

from time import time

class TimeBoundedLRU:
    "LRU Cache that invalidates and refreshes old entries."

    def __init__(self, func, maxsize=128, maxage=30):
        self.cache = OrderedDict()      # { args : (timestamp, result)}
        self.func = func
        self.maxsize = maxsize
        self.maxage = maxage

    def __call__(self, *args):
        if args in self.cache:
            self.cache.move_to_end(args)
            timestamp, result = self.cache[args]
            if time() - timestamp <= self.maxage:
                return result
        result = self.func(*args)
        self.cache[args] = time(), result
        if len(self.cache) > self.maxsize:
            self.cache.popitem(0)
        return result
Comment

PREVIOUS NEXT
Code Example
Python :: expected a list of items but got type int . django 
Python :: how to count the iteration a list python 
Python :: python nasa api 
Python :: python str and repr 
Python :: how to change the starting number for the loop count in pythin 
Python :: multiprocessing write to dict 
Python :: how to write a first program in machine learning 
Python :: python get num chars 
Python :: python random number between x and y 
Python :: pandas form multiindex to column 
Python :: HttpResponse Object Error in view.py 
Python :: iif python 
Python :: restart device micropython 
Python :: Jupyter get cell output 
Python :: python pytest use same tests for multiple modules 
Python :: indexers in python 
Python :: python parameter pack 
Python :: ploting bargraph with value_counts(with title x and y label and name angle) 
Python :: howmanydays python 
Python :: python [a]*b means [a,a,...b times] 
Python :: list of thing same condition 
Python :: accessing location of a csv cell in python 
Python :: set colour to inserplaintext qtextedit in python 
Python :: python get function from string name 
Python :: django create view filter options 
Python :: combining sparse class 
Python :: python consecutive numbers difference between 
Python :: inspect last 5 rows of dataframe 
Python :: fibonacci series program in python 
Python :: python multiprocessing queu empty error 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =