cache_utils.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:odin 作者: imito 项目源码 文件源码
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
# ===========================================================================
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号