Created
June 28, 2017 13:28
-
-
Save JeanOlivier/091fe88553363bf5255f013956710d8d to your computer and use it in GitHub Desktop.
A function to cache the result of a function call. For long calculations/data loading.
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
import os | |
from numpy import savez_compressed, load | |
def functionCache(fn, fct, *args, **kwargs): | |
""" | |
force_recalc = True -> self-descriptive | |
""" | |
force_recalc = kwargs.pop('force_recalc', False) | |
if os.path.isfile(fn) and not force_recalc: | |
return load(fn)['arr_0'] | |
else: | |
tmp = fct(*args, **kwargs) | |
savez_compressed(fn, tmp) | |
return tmp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment