Support for persistent functions in .sage files.

Persistent functions are functions whose values are stored on disk so they do not have to be recomputed.

The inputs to the function must be hashable (so lists are not allowed). Though a hash is used, in the incredibly unlikely event that a hash collision occurs, your function will not return an incorrect result because of this (though the cache might not be used either).

This is meant to be used from .sage files, not from library .py files.

To use this disk caching mechanism, just put @func_persist right before your function definition. For example,

@func_persist
def bern(n):
    "Return the n-th Bernoulli number, caching the result to disk."
    return bernoulli(n)

You can then use the function bern as usual, except it will almost instantly return values that have already been computed, even if you quit and restart.

The disk cache files are stored by default in the subdirectory func_persist of the current working directory, with one file for each evaluation of the function.

class sage.misc.func_persist.func_persist(f, dir='func_persist')
Put @func_persist right before your function definition to cache values it computes to disk.

Previous topic

Object persistence

Next topic

Evaluating a String in Sage

This Page