Skip to content

Instantly share code, notes, and snippets.

@bruntonspall
Created January 14, 2010 11:24
Show Gist options
  • Save bruntonspall/277091 to your computer and use it in GitHub Desktop.
Save bruntonspall/277091 to your computer and use it in GitHub Desktop.
A simple memoize function that writes json out to a file, momoizing between python executions
import json
import functools
def load_or_build(filename):
def wraps(fun):
@functools.wraps(fun)
def wrapped(*args, **kwargs):
try:
f = file(filename)
return json.load(f)
except IOError:
d = fun(*args, **kwargs)
f = file(filename, 'w')
json.dump(d, f, indent=2)
f.close()
return d
return wrapped
return wraps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment