ak.with_cache — Awkward Array documentation

# ak.with_cache

Defined in awkward.operations.structure on line 4264.

ak.with_cache(array, cache, highlevel=True, behavior=None)
  Parameters:

    - array – Data to search for nested virtual arrays.

    - cache (None or MutableMapping) – If None, arrays are generated every time they are needed; otherwise, generated arrays are stored in the mapping with `__setitem__`, retrieved with `__getitem__`, and only re-generated if `__getitem__` raises a `KeyError`. This mapping may evict elements according to any caching algorithm (LRU, LFR, RR, TTL, etc.). If “new”, a new dict (keep-forever cache) is created.

    - highlevel (bool) – If True, return an `ak.Array`; otherwise, return a low-level `ak.layout.Content` subclass.

    - behavior (None or dict) – Custom `ak.behavior` for the output array, if high-level.

Remove caches from all virtual arrays nested within `array` if `cache` is None; adds a cache otherwise.

For example:

    >>> cache1 = {}
    >>> one = ak.virtual(lambda: [[1.1, 2.2, 3.3], [], [4.4, 5.5]], cache=cache1, length=3)
    >>> two = ak.virtual(lambda: [100, 200, 300], cache=cache1, length=3)
    >>> array1 = ak.zip({"x": one, "y": two}, depth_limit=1)
    >>> len(cache1)
    0

creates an array of records with virtual fields that would fill `cache1`.

We can then switch every instance of `cache1` in `array` with `cache2`:

    >>> cache2 = {}
    >>> array2 = ak.with_cache(array1, cache2)
    >>> array2["x"]
    <Array [[1.1, 2.2, 3.3], [], [4.4, 5.5]] type='3 * var * float64'>
    >>>
    >>> len(cache1), len(cache2)
    (0, 1)

Viewing the `array2["x"]` filled `cache2` and not `cache1`.

See `ak.virtual`.