Skip to content

Instantly share code, notes, and snippets.

@suminb
Last active November 19, 2019 13:27
Show Gist options
  • Save suminb/f2a685875d48099fa020b0da8caed607 to your computer and use it in GitHub Desktop.
Save suminb/f2a685875d48099fa020b0da8caed607 to your computer and use it in GitHub Desktop.
I could use this as an example for SBCW
def cached_fetch(key: callable, base_path=".", max_age=None):
"""Decorates a function that returns Response object from Python Requests.
Example 1:
@cached_fetch(generate_key)
def make_cached_request(url, *args, **kwargs):
return requests.get(url, *args, **kwargs)
Example 2:
resp = cached_fetch(lambda: key)(requests.get)(url, params=params)
:param key: A function that generates a key
:param base_path:
:param max_age: Maximum age to keep the page cached (in seconds)
"""
if max_age:
raise NotImplementedError()
# TODO: Check if base_path is a directory
if not os.path.exists(base_path):
raise OSError(f"Base path '{base_path}' does not exist")
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
path = os.path.join(base_path, key())
if os.path.exists(path):
with open(path) as fin:
return fin.read()
else:
resp = func(*args, **kwargs)
with open(path, "w") as fout:
# TODO: Handle binary data properly
fout.write(resp.text)
return resp
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment