Created
July 5, 2020 21:01
-
-
Save sergeant-wizard/df302086a9a46f21c639e38257ae62bd to your computer and use it in GitHub Desktop.
cached dataframe that's immutable
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
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