Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

18 行
492B

  1. """
  2. Provides a decorator to cache function output to disk
  3. """
  4. import cPickle
  5. def cache_to_disk(file_name):
  6. def wrap(func):
  7. def modified(*args, **kwargs):
  8. try:
  9. output = cPickle.load(open(file_name, "r"))
  10. except (IOError, ValueError):
  11. output = func(*args, **kwargs)
  12. with open(file_name, "w") as f:
  13. cPickle.dump(output, f)
  14. return output
  15. return modified
  16. return wrap