Skip to content

Instantly share code, notes, and snippets.

@fmarani
Created January 19, 2013 16:55
Show Gist options
  • Save fmarani/4573628 to your computer and use it in GitHub Desktop.
Save fmarani/4573628 to your computer and use it in GitHub Desktop.
some ideas about offline pre-computation of functions in python
import inspect
REGISTRY = []
CACHE = {}
def offline_runner():
for fn, kwargs in REGISTRY:
#print "OFFLINE", fn, kwargs
CACHE[(fn, str(kwargs))] = fn(**kwargs)
def offline(**kwargs):
def real_offline(fn):
fnargs = inspect.getargspec(fn)
REGISTRY.append((fn, kwargs))
def wrapped(*inner_args):
initial_env = dict(zip(fnargs.args, inner_args))
#print "GETTING ", fn, initial_env
return CACHE.get((fn, str(initial_env)), None)
return wrapped
return real_offline
@offline()
def test():
return 1 + 1
@offline(a=5)
def test2(a):
return a * 5
#@offline(a__list=range(5))
#def test3(a):
# return a * 5
print test()
print test2(5)
offline_runner()
print test()
print test2(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment