Created
July 24, 2018 11:40
-
-
Save jcarlosroldan/2dd538843792539df0035a0eb261fc9c to your computer and use it in GitHub Desktop.
Cache the result of any function
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
# -- the cache snippet --------------------------------------------------------- | |
from os import makedirs | |
from os.path import exists, dirname | |
from pickle import load as pload, dump as pdump | |
from re import sub | |
from time import time | |
def cache(target, args, identifier=None, cache_life=3 * 24 * 3600): | |
""" Run the target function with the given args, and store it to a pickled | |
cache folder using the given identifier or the name of the function. It will | |
be stored for cache_life seconds. """ | |
if identifier == None: | |
identifier = target.__name__ | |
path = ".pickled/%s.pk" % sub(r"[/\\\*;\[\]\":=,<>]", "_", identifier) | |
makedirs(dirname(path), exist_ok=True) | |
now = time() | |
if exists(path): | |
with open(path, "rb") as fp: | |
save_time, value = pload(fp) | |
if now - save_time <= cache_life: | |
return value | |
res = target(*args) | |
with open(path, "wb") as fp: | |
pdump((now, res), fp, protocol=3) | |
return res | |
# -- the usage example --------------------------------------------------------- | |
def fact_mod(n, mod): | |
""" Dummy function to give an example of use. Computes n! % mod. """ | |
res = n | |
while n > 1: | |
n -= 1 | |
res *= n | |
return res % mod | |
if __name__ == "__main__": | |
n = 100000 | |
mod = 104729 | |
name = "fact_%s_mod_%s" % (n, mod) | |
value = cache(fact_mod, (n, mod), identifier=name) | |
print(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment