API#

@bamboo_stash.stash(f: Callable[[P], R]) StashedFunction[P, R][source]#

Convenience decorator for when you don’t care about where the cached data is stored.

The first time this function is called, this automatically creates a Stash object for you with default arguments. Subsequent calls will re-use that object.

The automatically created Stash object is intentionally hidden from you. If you need to access attributes such as Stash.base_dir, you should explicitly create a Stash object instead.

class bamboo_stash.Stash(base_dir: PathLike[str] | None = None)[source]#

An object that can be used to decorate functions to transparently cache its calls.

If the chosen directory doesn’t exist, it will be created (along with its parents) the first time a function call is cached to disk.

Parameters:

base_dir – Directory for storing cached data. If the value is None (the default), an appropriate cache directory is automatically chosen in the user’s home directory. This automatically chosen value can be seen using Stash.base_dir.

base_dir: Path#

Base directory for storing cached data.

clear() None[source]#

Delete all cached data.

__call__(f: Callable[[P], R]) StashedFunction[P, R][source]#

Decorator to wrap a function to cache its calls.

You wouldn’t call this method explicitly; this method exists to make the Stash object itself callable as a decorator.

For example:

from bamboo_stash import Stash

stash = Stash()

@stash  # <-- This line invokes stash.__call__
def my_function(): ...
class bamboo_stash.StashedFunction(base_dir: Path, f: Callable[[P], R])[source]#

Callable object that wraps your original function.

path_for(*args: ~typing.~P, **kwargs: ~typing.~P) Path[source]#

File where data for a function call with these arguments should be stored.

clear_for(*args: ~typing.~P, **kwargs: ~typing.~P) None[source]#

Delete cached data (if any exists) for this specific set of arguments.

clear() None[source]#

Delete all cached data for this function.

__call__(*args: ~typing.~P, **kwargs: ~typing.~P) R[source]#

If a cached file for these arguments exist, returns the cached result.

Otherwise, calls the wrapped function, caches that result to a file, and returns that result.