view pylearn/dataset_ops/memo.py @ 1417:f49801e39fe3

TensorDataset - added single_shape and batch_size properties
author James Bergstra <bergstrj@iro.umontreal.ca>
date Fri, 04 Feb 2011 16:02:57 -0500
parents c9ec065ff736
children
line wrap: on
line source

"""Provide a decorator that caches expensive functions
"""
import logging
_logger = logging.getLogger(__file__)
info = _logger.info
def infop(*args):
    info(' '.join(str(a) for a in args))

def memo(f):
    #TODO: support kwargs to rval.  This requires looking up the names of f's parameters to 
    #      construct a proper key.
    cache = {}
    def rval(*args):
        if args not in cache:
            cache[args] = f(*args)
        return cache[args]
    def forget():
        for k in cache.keys():
            del cache[k]
    rval.cache = cache
    rval.forget = forget
    rval.__name__ = 'memo@%s'%f.__name__
    rval.cache = cache
    rval.__doc__ = f.__doc__
    return rval