Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Utility function to help with local prototyping to minimize trips to fetch data.
- # Import the function and decorate any data-retrieval function to use cache if
- # available, make one if not. Remove the decorator before submitting to production.
- import cPickle as pickle
- import logging
- import os
- from functools import wraps
- THIS_DIR = os.getcwd()
- def persist_cache_to_disk(filename):
- def decorator(original_func):
- @wraps(original_func)
- def new_func(*args, **kwargs):
- # Use the cache if available
- if os.path.exists(filename):
- logging.info("Cache found at {}, loading it now".format(
- os.path.join(THIS_DIR, filename)
- ))
- with open(filename, 'r') as f:
- cache = pickle.load(f)
- logging.info('Cache loaded')
- else:
- cache = None
- if cache is None:
- logging.info("No cache found at {}, getting data".format(
- os.path.join(THIS_DIR, filename)
- ))
- cache = original_func(*args, **kwargs)
- logging.info("Persisting data to cache at {}".format(
- os.path.join(THIS_DIR, filename)
- ))
- pickle.dump(cache, open(filename, "w"))
- logging.info("Finished persisting data")
- return cache
- return new_func
- return decorator
Add Comment
Please, Sign In to add comment