Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memoizing an entire block in Python

Say I have some code that creates several 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 having to be explicit about every variable created/changed in the block or pickle 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 statement) and have a flag that forces a refresh if I want.

Conceptually speaking, it woul dbe like:

# 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 any way to do this without having to explicitly pickle each variable defined or changed in the code? (i.e. at the end of the first run we save, and we later just reuse the values)

like image 673
Amelio Vazquez-Reina Avatar asked Feb 27 '26 08:02

Amelio Vazquez-Reina


1 Answers

How about using locals() to get a list of the local variables, 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 your local stack. Maybe its better to use a list instead of a dict to preserve stack ordering.

like image 57
Alex Avatar answered Mar 01 '26 21:03

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!