Skip to content

Instantly share code, notes, and snippets.

@heronyang
Created June 23, 2019 22:56
Show Gist options
  • Save heronyang/39af69b03fff018389b9e6cbf16ea9db to your computer and use it in GitHub Desktop.
Save heronyang/39af69b03fff018389b9e6cbf16ea9db to your computer and use it in GitHub Desktop.
class CachableDataSource:
def __init__(self, url):
self.url = url
self.cache_filepath = self.__get_cache_filepath(url)
if os.path.isfile(self.cache_filepath):
self.__load_cache()
else:
self.__load_dataframe()
self.__save_cache()
def __load_cache(self):
self.dataframe = pd.read_pickle(self.cache_filepath)
def __save_cache(self):
self.dataframe.to_pickle(self.cache_filepath)
def __load_dataframe(self):
self.dataframe = pd.read_csv(self.url)
@classmethod
def __get_cache_filepath(cls, url):
return CACHE_PREFIX + hashlib.md5(url.encode("utf-8")).hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment