<jupyter_start><jupyter_text>Idea- function turns `dataarray` with attached `hash` into new `dataarray` with attached `hash`- `dataarray` can then be saved to `cache` directory<jupyter_code>import numpy as np
import scipy.integrate as si
import xarray as xr

from xrcache import cached, get_hash, keys
# Input dataarray

x = np.arange(10)
y = np.sin(x)

# initial hash
h = get_hash(np.array([x, y]))

da = xr.DataArray(y, dims="x", coords={"x": x}, name="some_array")
da.attrs.update({keys.hash: h})
da
# function that performs integration on the data
@cached
def cumtrapz(array: xr.DataArray, dx: float = 1.0, axis: int = 0):
    """perform trapz on array and return"""
    y = np.asarray(array)
    x = np.asarray(array.coords[array.dims[0]])
    i = si.cumtrapz(y, x=x, dx=dx, axis=axis, initial=0)

    # create dataarray
    da = xr.DataArray(i, dims=array.dims, coords=array.coords, attrs=array.attrs)

    return da


cumtrapz(da)
def stupid_function(array=None):
    array = array.copy()
    for i, x in enumerate(array):
        for j, y in enumerate(array):
            array[i] += i + y
    return array


@cached
def cached_stupid_function(array=None):
    return stupid_function(array)
%%timeit
stupid_function(da)
%%timeit
cached_stupid_function(da)<jupyter_output>1.64 ms ± 7.58 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
{'fraction_non_alphanumeric': 0.13175425610658772, 'fraction_numerical': 0.013323464100666173, 'mean_word_length': 4.1603053435114505, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '384150', 'license': '0BSD', 'n_tokens_mistral': 515, 'n_tokens_neox': 496, 'n_words': 145, 'path': 'caching/caching.ipynb', 'repo_name': 'flokno/python_recipes', 'size': '1351'}