Guest User

Untitled

a guest
Feb 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. # Utility function to help with local prototyping to minimize trips to fetch data.
  2. # Import the function and decorate any data-retrieval function to use cache if
  3. # available, make one if not. Remove the decorator before submitting to production.
  4. import cPickle as pickle
  5. import logging
  6. import os
  7. from functools import wraps
  8.  
  9.  
  10. THIS_DIR = os.getcwd()
  11.  
  12.  
  13. def persist_cache_to_disk(filename):
  14. def decorator(original_func):
  15.  
  16. @wraps(original_func)
  17. def new_func(*args, **kwargs):
  18. # Use the cache if available
  19. if os.path.exists(filename):
  20. logging.info("Cache found at {}, loading it now".format(
  21. os.path.join(THIS_DIR, filename)
  22. ))
  23. with open(filename, 'r') as f:
  24. cache = pickle.load(f)
  25.  
  26. logging.info('Cache loaded')
  27.  
  28. else:
  29. cache = None
  30.  
  31. if cache is None:
  32. logging.info("No cache found at {}, getting data".format(
  33. os.path.join(THIS_DIR, filename)
  34. ))
  35. cache = original_func(*args, **kwargs)
  36.  
  37. logging.info("Persisting data to cache at {}".format(
  38. os.path.join(THIS_DIR, filename)
  39. ))
  40. pickle.dump(cache, open(filename, "w"))
  41. logging.info("Finished persisting data")
  42.  
  43. return cache
  44.  
  45. return new_func
  46.  
  47. return decorator
Add Comment
Please, Sign In to add comment