Can I memoize a Python generator?

后端 未结 4 1113
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 18:03

I have a function called runquery that makes calls to a database and then yields the rows, one by one. I wrote a memoize decorator (or more accurately, I just stole

4条回答
  •  故里飘歌
    2021-02-01 18:46

    Yes. There's a decorator posted here. Take note that as the poster says, you lose some of the benefit of lazy evaluation.

    def memoize(func):
        def inner(arg):
            if isinstance(arg, list):
                # Make arg immutable
                arg = tuple(arg)
            if arg in inner.cache:
                print "Using cache for %s" % repr(arg)
                for i in inner.cache[arg]:
                    yield i
            else:
                print "Building new for %s" % repr(arg)
                temp = []
                for i in func(arg):
                    temp.append(i)
                    yield i
                inner.cache[arg] = temp
        inner.cache = {}
        return inner
    
    
    @memoize
    def gen(x):
        if not x:
            yield 0
            return
    
        for i in xrange(len(x)):
            for a in gen(x[i + 1:]):
                yield a + x[0]
    
    
    print "Round 1"
    for a in gen([2, 3, 4, 5]):
        print a
    
    print
    print "Round 2"
    for a in gen([2, 3, 4, 5]):
        print a
    

提交回复
热议问题