From 2afd44008381b8cfc5776b964d6f5eb624ae3066 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 17 Jun 2020 16:55:46 -0500 Subject: [PATCH 1/2] Allow complex identifiers in @memoize_in --- pytools/__init__.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pytools/__init__.py b/pytools/__init__.py index 8b7b4bc..bbd3e0a 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -800,16 +800,20 @@ class memoize_in(object): # noqa """Adds a cache to a function nested inside a method. The cache is attached to *object*. - Requires Python 2.5 or newer. + .. versionchanged :: 2020.3 + + *identifier* no longer needs to be a :class:`str`, + but it needs to be hashable. """ def __init__(self, container, identifier): - key = "_pytools_memoize_in_dict_for_"+identifier try: - self.cache_dict = getattr(container, key) + memoize_in_dict = container._pytools_memoize_in_dict except AttributeError: - self.cache_dict = {} - setattr(container, key, self.cache_dict) + memoize_in_dict = {} + container._pytools_memoize_in_dict = memoize_in_dict + + self.cache_dict = memoize_in_dict.setdefault(identifier, {}) def __call__(self, inner): from functools import wraps -- GitLab From af479bce573029f461a7e12cb21670f5b5380c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 18 Jun 2020 00:03:05 +0200 Subject: [PATCH 2/2] Apply suggestion to pytools/__init__.py --- pytools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytools/__init__.py b/pytools/__init__.py index bbd3e0a..585d4b8 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -798,7 +798,7 @@ def memoize_method_nested(inner): class memoize_in(object): # noqa """Adds a cache to a function nested inside a method. The cache is attached - to *object*. + to *container*. .. versionchanged :: 2020.3 -- GitLab