Created
November 17, 2024 21:43
-
-
Save snim2/5b43601b85fd45d62e7bcf43b2e02621 to your computer and use it in GitHub Desktop.
This file contains 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
from abc import ABC, abstractmethod | |
class PickleCache(ABC): | |
"""Cache Python objects on disk to save on API calls | |
Clients can treat this class like a key/value store. The fetch() method | |
must be overridden to fetch a value from the API for a specific key. | |
Then subclasses can be used as in this example: | |
cache = SubclassedPickleCache(filepath) | |
... | |
datum = cache.get(datum_key) | |
""" | |
def __init__(self, cache_file_path: str) -> None: | |
self.cache_file_path = cache_file_path | |
self.updated = False | |
self.data = {} | |
if os.path.exists(self.cache_file_path): | |
print(f"Opening cache file {self.cache_file_path}...") | |
with gzip.open(self.cache_file_path, "rb") as fp: | |
self.data = pickle.load(fp) | |
else: | |
self.updated = True | |
@abstractmethod | |
def fetch(self, key: Any) -> Any: | |
"""Fetch data about a given key from an API""" | |
def get(self, key: Any) -> Any: | |
"""Get information about a given key""" | |
if key not in self.data: | |
self.data[key] = self.fetch(key) | |
self.updated = True | |
return self.data[key] | |
def __del__(self) -> None: | |
"""Write the cache to disk, if necessary""" | |
if self.updated: | |
print(f"Saving cache to {self.cache_file_path}...") | |
with gzip.open(self.cache_file_path, "wb") as fp: | |
pickle.dump(self.data, fp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment