files.cache

files.cache

Functions

Name Description
cache_dataframe A decorator that caches the result of a function, which is a pandas DataFrame
cache_dataset A decorator that caches the dataset returned by a function in a netcdf file
cache_figure A decorator that caches the figure returned by a function in a png file
cache_json A decorator that caches the result of a function to a json file.
cache_pickle A decorator that caches the result of a function to a pickle file.
cachefunc A decorator that caches the return of a function in a file, with
hashparams Generates a deterministic hash based on arguments.

cache_dataframe

files.cache.cache_dataframe(cache_file, inputs='check')

A decorator that caches the result of a function, which is a pandas DataFrame

inputs: “check” [default]: store and check the function inputs “store”: store but don’t check the function inputs “ignore”: ignore the function inputs

cache_dataset

files.cache.cache_dataset(cache_file, attrs=None, **kwargs)

A decorator that caches the dataset returned by a function in a netcdf file

The attribute dictionary attrs is stored in the file, and verified upon reading.

Other kwargs (ex: chunks) are passed to xr.open_dataset

cache_figure

files.cache.cache_figure(cache_dir, overwrite=False, verbose=False, **kwargs)

A decorator that caches the figure returned by a function in a png file

The name of the png file is deduced by the decorator and is the concatenation of the function name and the hash of its params (e.g. function_a8f6d)

Other kwargs are passed to fig.savefig()

Returns the path to the cached png file.

cache_json

files.cache.cache_json(cache_file, inputs='check')

A decorator that caches the result of a function to a json file.

inputs: “check” [default]: store and check the function inputs “store”: store but don’t check the function inputs “ignore”: ignore the function inputs

cache_pickle

files.cache.cache_pickle(
    cache_file,
    inputs='check',
    check_out=lambda x, y: x == y,
)

A decorator that caches the result of a function to a pickle file.

inputs: “check” [default]: store and check the function inputs “store”: store but don’t check the function inputs “ignore”: ignore the function inputs

cachefunc

files.cache.cachefunc(
    cache_file,
    reader,
    writer,
    check_in=None,
    check_out=None,
    fg_kwargs=None,
)

A decorator that caches the return of a function in a file, with customizable format

reader: a function that reads inputs/output from the cache file reader(filename) -> {‘output’: …, ‘input’: …}

writer: a function that writes the inputs/output to the cache file writer(filename, output, input_args, input_kwargs)

check_in: a custom function to test the equality of the inputs checker(obj1, obj2) -> bool (defaults to None -> no checking)

check_out: a custom function to test the equality of the outputs checker(obj1, obj2) -> bool (defaults to ==)

fg_kwargs: kwargs passed to filegen (ex: lock_timeout=-1)

hashparams

files.cache.hashparams(*args, hash_digest_size=16, **kwargs)

Generates a deterministic hash based on arguments. Simplifies caching based on inputs

Args: *args: Positional arguments (hashed with index-based keys). **kwargs: Keyword arguments.

Returns: str: 16-character hexadecimal hash. (or specified digest size)