Skip to content

util

file_cache(*, ext, domain)

Caches the result of a function to a file.

Parameters:

Name Type Description Default
ext str

The file extension for the cache file.

required
domain str

The domain name for the cache file.

required

Returns:

Type Description
Callable

A wrapper function that caches the result of the original function.

Source code in src/finch/util/cache.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def file_cache(*, ext: str, domain: str) -> Callable:
    """Caches the result of a function to a file.

    Args:
        ext: The file extension for the cache file.
        domain: The domain name for the cache file.

    Returns:
        A wrapper function that caches the result of the original function.
    """

    def decorator(f: FunctionType) -> FunctionType:
        nonlocal domain
        nonlocal ext
        ext = ext.lstrip(".")
        if config.get("cache_enable"):
            cache_dir = Path(config.get("data_path")) / "cache" / get_version() / domain
        else:
            cache_dir = Path(
                tempfile.mkdtemp(
                    prefix=str(Path(config.get("data_path")) / "tmp" / domain)
                )
            )
            atexit.register(
                lambda: shutil.rmtree(cache_dir) if cache_dir.exists() else None
            )

        cache_dir.mkdir(parents=True, exist_ok=True)

        def inner(*args):
            id = uuid.uuid5(finch_uuid, str((f.__name__, f.__module__, args)))
            filename = cache_dir / f"{f.__name__}_{id}.{ext}"
            if not config.get("cache_enable") or not filename.exists():
                f(str(filename), *args)
            return filename

        return inner

    return decorator

get_version()

Get the version of finch.

Source code in src/finch/util/config.py
79
80
81
82
83
84
def get_version():
    """
    Get the version of finch.
    """

    return version("finch-tensor")