Q: How can I cache API data in a class attribute? I would like to have a class, similar to the one below, where I can use a property to access data from an API. I would like to be able to cache the data for a certain period of time, though, so that I may access the attribute rapidly without being ratelimited. What would be the cleanest way to implement this functionality?
from requests import get


class GitHubUser:
    def __init__(self, user):
        self.user = user
        print("{user} has {repos} public repos!".format(
            user=user, repos=self.public_repos
        ))

    @property
    def _api_data(self):
        return get(
            "https://api.github.com/users/{user}".format(user=self.user)
        ).json()

    @property
    def public_repos(self):
        return self._api_data["public_repos"]


A: Here's one way to make it look neat (it may seem overcomplicated but it's not really):
from uuid import uuid4
from datetime import datetime

class cached_descriptor(object):
    def __init__(self, func, timeout):
        self.__doc__ = getattr(func, '__doc__')
        self.func = func
        self.uuid = str(uuid.uuid4())
        self.timeout = timeout

    def __get__(self, obj, cls):
        if obj is None:
            return self
        if not hasattr(obj, '_cache'):
            obj._cache = {}
        if self.uuid not in obj._cache:
            obj._cache[self.uuid] = []
        data = obj._cache[self.uuid]
        now = datetime.now()
        if not data or (now - data[1]).total_seconds() > self.timeout:
            obj._cache[self.uuid] = (self.func(obj), now)
        return obj._cache[self.uuid][0]

class cached_property(object):
    def __init__(self, timeout):
        self.timeout = timeout

    def __call__(self, func):
        return cached_descriptor(func, self.timeout)

To break it down:


*

*cached_property is a factory for decorators that takes a timeout argument in seconds

*cached_descriptor is a read-only descriptor that stores the cached value and the timestamp in the object itself in _cache dict, under a randomly generated uuid to avoid conflicts between multiple cached properties

*upon the first call, the function will always be called

*each next call, the function will only be called if the timeout has been exceeded


Here's an example of how this works:
import time

class A(object):
    def __init__(self):
        self.n_f = self.n_g = 0

    @cached_property(0.1)
    def f(self):
        self.n_f += 1
        print('calling f', self.n_f)
        return self.n_f

    @cached_property(0.5)
    def g(self):
        self.n_g += 1
        print('calling g', self.n_g)
        return self.n_g

a = A()
print('f', a.f)
print('g', a.g)
print('f', a.f)
print('g', a.g)
print('sleep 0.2')
time.sleep(0.2)
print('f', a.f)
print('g', a.g)
print('sleep 0.4')
time.sleep(0.4)
print('f', a.f)
print('g', a.g)

which outputs
calling f 1
f 1
calling g 1
g 1
f 1
g 1
sleep 0.2
calling f 2
f 2
g 1
sleep 0.4
calling f 3
f 3
calling g 2
g 2

