Skip to content

Instantly share code, notes, and snippets.

@sergeant-wizard
Created July 5, 2020 21:01
Show Gist options
  • Save sergeant-wizard/df302086a9a46f21c639e38257ae62bd to your computer and use it in GitHub Desktop.
Save sergeant-wizard/df302086a9a46f21c639e38257ae62bd to your computer and use it in GitHub Desktop.
cached dataframe that's immutable
import functools
import time
from typing import Any, Callable
import pandas
def df_cache(f: Callable[..., Any]) -> Callable[..., Any]:
functools.wraps(f)
def wrapper(*args: Any, **kwargs: Any) -> Any:
print("before")
return f(*args, **kwargs)
return wrapper
@functools.lru_cache(1)
def long_method() -> pandas.DataFrame:
time.sleep(3)
return pandas.DataFrame([3])
@df_cache
def hoge() -> pandas.DataFrame:
return long_method().copy()
df = hoge()
df.iloc[0, 0] += 1
print(df)
print(hoge())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment