import pickle


def get_data_or_compute(fname, comp_func, recompute=False, *args, **kwargs):
    """
    A simple pickle_cache for computing stuff.
    Parameters
    ----------
    fname : str
        path to where store the data
    comp_func : callable
        the function for which to compute values
    recompute : bool
    args:
        positional arguments to be passed to comp_func
    kwargs:
        keyword arguments to be passed to comp_func
    Returns
    -------
    data: object
        the data object returned by comp_fund
    """
    try:
        if recompute:
            raise RuntimeError("Recompute!")
        with open(fname, "rb") as f:
            print("Loading data")
            data = pickle.load(f)
    except (RuntimeError, TypeError, EOFError, IOError) as e:
        print("Tried to load data from disk, but failed. This is expected if the data has not been yet computed)")
        print("The provided error message was: " + str(e))
        with open(fname, "wb") as f:
            data = comp_func(*args, **kwargs)
            pickle.dump(data, f, -1)
    return data