Created
January 14, 2010 11:24
-
-
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
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 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