daniel-s
3/24/2017 - 10:20 PM

Pure function result cache decorator.

Pure function result cache decorator.

from functools import wraps

def cache(func):
  saved = {}
  @wraps(func)
  def newfunc(*args):
    if args in saved:
      return saved[args]
    result = func(*args)
    saved[args] = result
    return result
  return newfunc


@cache
def web_lookup(url):
  return urllib.urlopen(url).read()