Skip to content

pybes3._cache_numba

Rare Help for Users

There is rare help for users in this page. pybes3._cache_numba.md defines internal functions and never exposed to users.


cache_auto_clear(sources, caches, silent=True, force=False)

Automatically clear the Numba cache if the source files have changed.

Due to limitation of numba cache, modification of external files like geometry data will not trigger the cache clearing. This function is designed to trigger the cache clearing when those external files are modified.

This function compares the modification times of the source files with the modification times of the cached files. If any source file is newer than the cached files, the numba cache in cache directory will be cleared.

Note

There is no need to trace *.py files, as numba will automatically clear the cache when the source code is modified.

Parameters:

Name Type Description Default
sources Union[Union[Path, str], list[Union[Path, str]]]

A glob pattern or a list of glob patterns to match the source files.

required
caches Union[Union[Path, str], list[Union[Path, str]]]

A glob pattern or a list of glob patterns to match the cached files.

required
silent bool

If True, suppresses the output messages.

True
force bool

If True, forces the cache clearing even if the source files are not newer.

False

Returns:

Type Description
list[str]

list[str]: A list of cache files that were removed.

Source code in src/pybes3/_cache_numba.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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def cache_auto_clear(
    sources: Union[Union[Path, str], list[Union[Path, str]]],
    caches: Union[Union[Path, str], list[Union[Path, str]]],
    silent: bool = True,
    force: bool = False,
) -> list[str]:
    """
    Automatically clear the Numba cache if the source files have changed.

    Due to limitation of numba cache, modification of external files like
    geometry data will not trigger the cache clearing. This function is
    designed to trigger the cache clearing when those external files are
    modified.

    This function compares the modification times of the source files with the
    modification times of the cached files. If any source file is newer than
    the cached files, the numba cache in cache directory will be cleared.

    !!! note
        There is no need to trace `*.py` files, as numba will automatically
        clear the cache when the source code is modified.

    Args:
        sources: A glob pattern or a list of glob patterns to match the source files.
        caches: A glob pattern or a list of glob patterns to match the cached files.
        silent: If True, suppresses the output messages.
        force: If True, forces the cache clearing even if the source files are not newer.

    Returns:
        list[str]: A list of cache files that were removed.
    """
    # Transform sources and caches to lists of strings
    sources: list[str] = (
        glob(str(sources))
        if isinstance(sources, (Path, str))
        else sum([glob(str(s)) for s in sources], [])
    )

    caches: list[str] = (
        glob(str(caches))
        if isinstance(caches, (Path, str))
        else sum([glob(str(c)) for c in caches], [])
    )

    # Check if sources and caches are empty
    if not sources:
        raise ValueError("No source files found.")

    if not caches:
        return []

    # Compare modification times
    removed_caches = []
    failed_removed_caches = []

    src_latest_mtime = max([os.path.getmtime(s) for s in sources if os.path.isfile(s)])
    cache_earliest_mtime = min([os.path.getmtime(c) for c in caches if os.path.isfile(c)])

    if src_latest_mtime > cache_earliest_mtime or force:
        # Remove the cache files
        for c in caches:
            try:
                os.remove(c)
                removed_caches.append(c)
            except FileNotFoundError:
                pass
            except Exception:
                failed_removed_caches.append(c)

    # Check failure of removing
    if failed_removed_caches:
        error_msg = "Cannot remove cache files:\n"
        for c in failed_removed_caches:
            error_msg += f" - {c}\n"
        error_msg += "This may cause wrong results from corresponding functions. "
        error_msg += "Please check their permissions or remove them manually."
        raise ImportError(error_msg)

    # Print messages if not silent
    if not silent:
        if removed_caches:
            removed_caches_str = "\n - ".join(removed_caches)
            print(f"Removed cache files: {removed_caches_str}")

    # Return the list of removed caches
    return removed_caches

check_numba_cache()

Check the cache files and remove them if the source files are newer. This function should be called when the module is imported.

When environment variable PYBES3_NUMBA_CACHE_SILENT is set to 1, the function will print out removal messages.

Source code in src/pybes3/_cache_numba.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def check_numba_cache():
    """
    Check the cache files and remove them if the source files are newer.
    This function should be called when the module is imported.

    When environment variable `PYBES3_NUMBA_CACHE_SILENT` is set to 1,
    the function will print out removal messages.
    """
    silent = os.getenv("PYBES3_NUMBA_CACHE_SILENT", "0") == "0"
    for src, cache in src_cache_list:
        cache_auto_clear(
            sources=src,
            caches=cache,
            silent=silent,
            force=False,
        )

clear_numba_cache()

Clear the cache files regardless of the source files.

When environment variable PYBES3_NUMBA_CACHE_SILENT is set to 1, the function will print out removal messages.

Source code in src/pybes3/_cache_numba.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def clear_numba_cache():
    """
    Clear the cache files regardless of the source files.

    When environment variable `PYBES3_NUMBA_CACHE_SILENT` is set to 1,
    the function will print out removal messages.
    """
    silent = os.getenv("PYBES3_NUMBA_CACHE_SILENT", "0") == "0"
    for src, cache in src_cache_list:
        cache_auto_clear(
            sources=src,
            caches=cache,
            silent=silent,
            force=True,
        )