From cb7861e02466c732f09da1a26fefeda2bd256703 Mon Sep 17 00:00:00 2001 From: Thor Whalen Date: Tue, 16 Apr 2024 10:20:28 +0200 Subject: [PATCH] doc: doctest for mk_memoizer --- dol/caching.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dol/caching.py b/dol/caching.py index 255ed67b..ce80b790 100644 --- a/dol/caching.py +++ b/dol/caching.py @@ -27,6 +27,28 @@ def get_cache(cache): # The following is a "Cache-Aside" read-cache with NO builtin cache update or refresh mechanism. def mk_memoizer(cache): + """ + Make a memoizer that caches the output of a getter function in a cache. + + Note: This is a specialized memoizer for getter functions/methods, i.e. + functions/methods that have the signature (instance, key) and return a value. + + :param cache: The cache to use. Must have __getitem__ and __setitem__ methods. + :return: A memoizer that caches the output of the function in the cache. + + >>> cache = dict() + >>> @mk_memoizer(cache) + ... def getter(self, k): + ... print(f"getting value for {k}...") + ... return k * 10 + ... + >>> getter(None, 2) + getting value for 2... + 20 + >>> getter(None, 2) + 20 + + """ def memoize(method): @wraps(method) def memoizer(self, k):