def cache()

in pythonflow/operations.py [0:0]


def cache(operation, get, put, key=None):
    """
    Cache the values of `operation`.

    Parameters
    ----------
    operation : Operation
        Operation to cache.
    get : callable(object)
        Callable to retrieve an item from the cache. Should throw `KeyError` or `FileNotFoundError`
        if the item is not in the cache.
    put : callable(object, object)
        Callable that adds an item to the cache. The first argument is the key, the seconde the
        value.
    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.
    """
    if not key:
        dependencies = operation.args + tuple(operation.kwargs.values())
        key = hash_(dependencies)

    return try_(
        func_op(get, key), [
            ((KeyError, FileNotFoundError),
             identity(operation, dependencies=[func_op(put, key, operation)]))
        ]
    )