Skip to content

Commit

Permalink
Cleanup memoize-method code
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Sep 22, 2024
1 parent 63ecd86 commit 3314f73
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions ubelt/util_memoize.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,41 +254,24 @@ def __get__(self, instance, cls=None):
"""
import types
unbound = self._func
# bound = unbound.__get__(instance, cls)
cache = instance.__dict__.setdefault(self._cache_name, {})

# https://stackoverflow.com/questions/71413937/what-does-using-get-on-a-function-do

# This works better, but it creates a new method each time. whereas
# the descriptor binding process seems to cache the bound method.
@functools.wraps(unbound)
def memoizer(instance, *args, **kwargs):
key = _make_signature_key(args, kwargs)
if key not in cache:
cache[key] = unbound(instance, *args, **kwargs)
return cache[key]

# Bind the unbound memoizer to the instance
bound_memoizer = types.MethodType(memoizer, instance)

# Set the attribute to prevent calling __get__ again
# Is there a better way to do this?
setattr(instance, self._func.__name__, bound_memoizer)
return bound_memoizer

self._instance = instance
return self

# def __call__(self, *args, **kwargs):
# """
# The wrapped function call
# """
# cache = self._instance.__dict__.setdefault(self._cache_name, {})
# key = _make_signature_key(args, kwargs)
# if key in cache:
# return cache[key]
# else:
# value = cache[key] = self._func(self._instance, *args, **kwargs)
# return value


def memoize_property(fget):
"""
Expand Down

0 comments on commit 3314f73

Please sign in to comment.