def cache_file()

in pythonflow/operations.py [0:0]


def cache_file(operation, filename_template, load=None, dump=None, key=None):
    """
    Cache the values of `operation` in a file.

    Parameters
    ----------
    operation : Operation
        Operation to cache.
    filename_template : str
        Template for the filename taking a single `key` parameter.
    load : callable(str)
        Callable to retrieve an item from a given file. Should throw `FileNotFoundError` if the file
        does not exist.
    dump : callable(object, str)
        Callable to save the item to a file. The order of arguments differs from the `put` argument
        of `cache` to be compatible with `pickle.dump`, `numpy.save`, etc.
    key : Operation
        Key for looking up an item in the cache. Defaults to a simple `hash` of the arguments of
        `operation`.

    Returns
    -------
    cached_operation : Operation
        Cached operation.
    """
    load = load or _pickle_load
    dump = dump or _pickle_dump

    return cache(
        operation, lambda key_: load(filename_template % key_),
        lambda key_, value: dump(value, filename_template % key_), key)