在Python记忆化装饰器类中设置get/set属性

4 投票
2 回答
1340 浏览
提问于 2025-04-18 07:08

我创建了一个装饰器记忆化的类,正在用它来缓存我的调用。其实已经有很多很棒的建议,教你怎么在Python中实现记忆化。

我这个类目前使用了获取和设置的方法来设置缓存超时时间。这两个方法分别叫做 getCacheTimeOut()setCacheTimeOut()。虽然这样做可以解决问题,但我希望能用 @property@cacheTimeOut.setter 这些装饰器,让函数可以直接像这样调用:cacheTimeOut=120

问题出在细节上。我不知道怎么在 __get__ 方法中让这些属性可用。__get__ 方法会把类里面定义的不同函数调用分配给 functions.partial。

下面是我为Python 2.7设计的脚本示例。

import time
from functools import partial
import cPickle

class memoize(object):
    def __init__(self, func):
        self.func = func
        self._cache = {}
        self._timestamps = {}
        self._cacheTimeOut = 120
        self.objtype = None

    def __new__(cls, *args, **kwargs):
        return object.__new__(cls,*args, **kwargs)

    def __get__(self, obj, objtype=None):
    """Used for object methods where decorator has been placed before methods."""
        self.objtype = objtype
        fn = partial(self, obj)
        fn.resetCache = self.resetCache
        fn.getTimeStamps = self.getTimeStamps
        fn.getCache = self.getCache
        fn._timestamps = self._timestamps
        fn.setCacheTimeOut = self.setCacheTimeOut
        fn.getCacheTimeOut = self.getCacheTimeOut
        return fn

    def __argsToKey(self, *args, **kwargs):
        args = list(args)

        for x, arg in enumerate(args):    # remove instance from
            if self.objtype:
                 if isinstance(arg, self.objtype):
                     args.remove(arg)

        str = cPickle.dumps(args, 1)+cPickle.dumps(kwargs, 1)

        return str

    def __call__(self, *args, **kwargs):
        """Main calling function of decorator."""
         key = self.__argsToKey(*args, **kwargs)    
         now = time.time()    # get current time to query for key
         if self._timestamps.get(key, now) > now:    
             return self._cache[key]
         else:
             value = self.func(*args, **kwargs)
             self._cache[key] = value
             self._timestamps[key] = now + self._cacheTimeOut
         return value

    def __repr__(self):
        '''Return the function's docstring.'''
        return self.func.__doc__

    def resetCache(self):
        """Resets the cache.  Currently called manually upon request."""
        self._cache = {}
        self._timestamps = {}

    def getCacheTimeOut(self):
    """Get the cache time out used to track stale data."""
        return self._cacheTimeOut

    def setCacheTimeOut(self, timeOut):
    """Set the cache timeout to some other value besides 120.  Requires an integer     value.  If you set timeOut to zero you are ignoring the cache"""
        self._cacheTimeOut = timeOut

    def getCache(self):
    """Returns the cache dictionary."""
        return self._cache

    def getTimeStamps(self):
    """Returns the encapsulated timestamp dictionary."""
        return self._timestamps

    @property
    def cacheTimeOut(self):
    """Get cacheTimeOut."""
        return self._cacheTimeOut

    @cacheTimeOut.setter
    def cacheTimeOut(self, timeOut):
    """Set cacheTimeOut."""
        self._cacheTimeOut = timeOut

memoize
def increment(x):
    increment.count+=1
    print("increment.count:%d, x:%d"%(increment.count, x))
    x+=1
    return x


increment.count = 0   # Define the count to track whether calls to increment vs cache


class basic(object):
    def __init__(self):
        self.count = 0

    @memoize
    def increment(self, x):
        self.count+=1
        print("increment.count:%d, x:%d"%(increment.count, x))
        x+=1
        return x


def main():
    print increment(3)
    print increment(3)

    # What I am actually doing
    print increment.getCacheTimeOut()  # print out default of 120
    increment.setCacheTimeOut(20)      # set to 20
    print increment.getCacheTimeOut()  # verify that is has been set to 120

    # What I would like to do and currently does not work
    print increment.cacheTimeOut
    # Assign to property
    increment.cacheTimeOut = 20


    myObject = basic()
    print myObject.increment(3)
    print myObject.count
    print myObject.increment(3)
    print myObject.count
    print myObject.increment(4)
    print myObject.count



####### Unittest code. 
import sys
import time
import unittest
from memoize import memoize

class testSampleUsages(unittest.TestCase):
# """This series of unit tests is to show the user how to apply memoize calls."""
    def testSimpleUsageMemoize(self):
        @memoize
        def increment(var=0):
            var += 1
            return var

        increment(3)
        increment(3)

    def testMethodBasedUsage(self):
        """Add the @memoize before method call."""
        class myClass(object):
            @memoize
            def increment(self,var=0):
                var += 1
                return var

            @memoize
            def decrement(self, var=0):
                var -=1
                return var

        myObj = myClass()
        myObj.increment(3)
        myObj.increment(3)
        myObj.decrement(6)
        myObj.decrement(6)

    def testMultipleInstances(self):
        @memoize
        class myClass(object):
            def __init__(self):
               self.incrementCountCalls = 0
               self.decrementCountCalls = 0
               self.powCountCall = 0

            # @memoize
            def increment(self,var=0):
                var += 1
                self.incrementCountCalls+=1
                return var

            # @memoize
            def decrement(self, var=0):
                self.decrementCountCalls+=1
                var -=1
                return var

            def pow(self, var=0):
                self.powCountCall+=1
                return var*var


        obj1 = myClass()   # Memoizing class above does not seem to work.  
        obj2 = myClass()
        obj3 = myClass()

        obj1.increment(3)
        obj1.increment(3)
        #obj2.increment(3)
        #obj2.increment(3)
        #obj3.increment(3)
        #obj3.increment(3)

        obj1.pow(4)
        obj2.pow(4)
        obj3.pow(4)

2 个回答

0

其实有一种方法可以做到这一点——通过将装饰器重新绑定为实例对象,并使用一个调用方法。

class Helper(object):

    def __init__(self, d, obj):
        self.d = d
        self.obj = obj
        self.timeout = 0

    def __call__(self, *args, **kwargs):
        print self, self.timeout
        return self.d.func(self.obj, *args, **kwargs)


class decorator(object):

    def __init__(self, func):
        self.func = func
        self.name = func.__name__



    def __get__(self, obj, clazz):
        if object is not None:
            obj.__dict__[self.name] = Helper(self, obj)
        return obj.__dict__[self.name]


class Foo(object):

    @decorator
    def bar(self, args):
        return args * 2



f = Foo()
g = Foo()

f.bar.timeout = 10
g.bar.timeout = 20

print f.bar(10)
print g.bar(20)

希望这对你有帮助。

1

你不能把一个 property 附加到单独的实例上。因为 property 是描述符,它们必须在类的定义中才能正常工作。这意味着你不能轻易地把它们添加到你在 __get__ 中创建的 partial 对象上。

当然,你可以自己创建一个类,重新实现 partial 的行为,并添加你想要的 property。不过,我觉得这个限制其实对你是有好处的。如果 memo 被应用到一个方法上,它的状态会被这个类的所有实例共享(甚至可能包括子类的实例)。如果你允许通过实例来调整缓存的细节,可能会让用户感到困惑,比如出现以下情况:

obj1 = basic()
print obj1.increment.getCacheTimeout() # prints the initial value, e.g. 120

obj2 = basic()
obj2.increment.setCacheTimeOut(20)     # change the timeout value via another instance

print obj1.increment.getCacheTimeout() # the value via the first instance now prints 20

我建议你让被装饰方法的与缓存相关的接口只能通过类来访问,而不是通过实例。为了实现这一点,你需要更新你的 __get__ 方法,使其在 objNone 时也能正常工作。它可以简单地返回 self

def __get__(self, obj, objtype=None):
    if obj is None:
        return self

    self.objtype = objtype
    return partial(self, obj) # no need to attach our methods to the partial anymore

通过这个改动,使用类中的 property 来访问 memo 就可以正常工作了:

basic.increment.cacheTimeOut = 20  # set property of the "unbound" method basic.increment

撰写回答