Mercurial > pylearn
diff pylearn/dataset_ops/memo.py @ 832:67b92a42f86b
added dataset_ops
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Fri, 16 Oct 2009 12:04:05 -0400 |
parents | |
children | 7ccce98da2b6 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pylearn/dataset_ops/memo.py Fri Oct 16 12:04:05 2009 -0400 @@ -0,0 +1,22 @@ +"""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. + + #TODO: use weak references instead of a normal dict so that the cache doesn't prevent + # garbage collection + cache = {} + def rval(*args): + if args not in cache: + cache[args] = f(*args) + return cache[args] + rval.__name__ = 'memo@%s'%f.__name__ + return rval +