Created
June 23, 2019 22:56
-
-
Save heronyang/39af69b03fff018389b9e6cbf16ea9db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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