def cache_disk(function):
""" Lazy evaluation of function, by storing the results to the disk,
and not rerunning the function twice for the same arguments.
It works by explicitly saving the output to a file and it is
designed to work with non-hashable and potentially large input
and output data types such as numpy arrays
Note
----
Any change in the function code will result re-cache
Example
-------
>>> import numpy as np
>>> @cache_memory
>>> def doit(x):
... y = np.random.rand(1208, 1208)
... print("Function called:", np.sum(y))
... return y
>>> for _ in range(12):
... print(np.sum(doit(8)))
"""
def decorator_apply(dec, func):
"""Decorate a function by preserving the signature even if dec
is not a signature-preserving decorator.
This recipe is derived from
http://micheles.googlecode.com/hg/decorator/documentation.html#id14
"""
return FunctionMaker.create(
func, 'return decorated(%(signature)s)',
dict(decorated=dec(func)), __wrapped__=func)
return decorator_apply(__memory.cache, function)
# ===========================================================================
# Cache
# ===========================================================================
评论列表
文章目录