Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
514B

  1. """
  2. Useful but messy crap
  3. """
  4. def cache_to_disk(file_name):
  5. """ A decorator to cache the output of a function to disk """
  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