Remembering an entire block in Python
Let's say I have some code that creates multiple variables:
# Some code
# Beginning of the block to memoize
a = foo()
b = bar()
...
c =
# End of the block to memoize
# ... some more code
I would like to memoize the entire block above without being explicit for every variable created / modified in the block, or comb them manually. How can I do this in Python?
Ideally I would like to be able to wrap it with something ( if/else
or with
) and have a flag that forces it to update if I want to.
Conceptually speaking, it would look like this:
# Some code
# Flag that I can set from outside to save or force a reset of the chache
refresh_cache = True
if refresh_cache == False
load_cache_of_block()
else:
# Beginning of the block to memoize
a = foo()
b = bar()
...
c = stuff()
# End of the block to memoize
save_cache_of_block()
# ... some more code
Is there a way to do this without having to explicitly define every variable that is defined or changed in code? (i.e. at the end of the first run we save, and later we just reuse the values)
source to share
There are many ways to do this, but I think the closest thing to what you are describing would be using pythons module scope
as your memoized and import
or reload
as needed. Something like that:
# a.py
import b
print b.a, b.b
b.func(5)
b.b = 'world'
print b.a, b.b
if b.needs_refresh():
reload(b)
print b.a, b.b
With your "variable scope" being a module b
:
# b.py
a = 0
b = 'hello'
def func(i):
global a
a += i
def needs_refresh():
return a >= 5
Doing this will result in the expected:
0 hello
5 world
0 hello
Edit: so that you can copy and save the entire scope, you could just use the class scope:
memo_stack = list()
class MemoScope(object):
def __init__(self):
self.a = 0
self.b = 'hello'
memo = MemoScope()
memo.a = 2
memo.b = 3
memo_stack.append(memo)
memo_stack.append(MemoScope())
for i, o in enumerate(memo_stack):
print "memo t%i.a = %s" % (i, o.a)
print "memo t%i.b = %s" % (i, o.b)
if o.a == 2:
memo_stack[i] = MemoScope()
print "refreshed"
# memo t0.a = 2
# memo t0.b = 3
# refreshed
# memo t1.a = 0
# memo t1.b = hello
source to share
How to use locals()
to get a list of local variables by storing them in a dict in pickle then using (below is more conceptual):
for k,v in vars_from_pickle:
run_string = '%s=%s' % (k,v)
exec(run_string)
to restore the local stack. It might be better to use a list instead of a dict to preserve the stack order.
source to share