Skip to content

Instantly share code, notes, and snippets.

@JeanOlivier
Created June 28, 2017 13:28
Show Gist options
  • Save JeanOlivier/091fe88553363bf5255f013956710d8d to your computer and use it in GitHub Desktop.
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.
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