Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: integrate SymPy version in cache path #409

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/ampform/sympy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@

import itertools
import logging
import os
import pickle # noqa: S403
import re
import sys
import warnings
from abc import abstractmethod
from os.path import abspath, dirname
from pathlib import Path
from typing import TYPE_CHECKING, Iterable, Sequence, SupportsFloat

import sympy as sp
Expand All @@ -43,6 +42,10 @@
make_commutative, # pyright: ignore[reportUnusedImport] # noqa: F401
)

if sys.version_info < (3, 8):
from importlib_metadata import version
else:
from importlib.metadata import version
if sys.version_info < (3, 12):
from typing_extensions import override
else:
Expand Down Expand Up @@ -335,7 +338,7 @@ def _warn_if_scipy_not_installed() -> None:


def perform_cached_doit(
unevaluated_expr: sp.Expr, cache_directory: str | None = None
unevaluated_expr: sp.Expr, cache_directory: Path | str | None = None
) -> sp.Expr:
"""Perform :meth:`~sympy.core.basic.Basic.doit` and cache the result to disk.

Expand All @@ -357,11 +360,14 @@ def perform_cached_doit(
"""
if cache_directory is None:
system_cache_dir = get_system_cache_directory()
cache_directory = abspath(f"{system_cache_dir}/ampform")
sympy_version = version("sympy")
cache_directory = Path(system_cache_dir) / "ampform" / f"sympy-v{sympy_version}"
if not isinstance(cache_directory, Path):
cache_directory = Path(cache_directory)
cache_directory.mkdir(exist_ok=True, parents=True)
h = get_readable_hash(unevaluated_expr)
filename = f"{cache_directory}/{h}.pkl"
os.makedirs(dirname(filename), exist_ok=True)
if os.path.exists(filename):
filename = cache_directory / f"{h}.pkl"
if filename.exists():
with open(filename, "rb") as f:
return pickle.load(f) # noqa: S301
_LOGGER.warning(
Expand Down
2 changes: 1 addition & 1 deletion src/ampform/sympy/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
def get_system_cache_directory() -> str:
r"""Return the system cache directory for the current platform.

>>> import sys, pytest
>>> import sys
>>> if sys.platform.startswith("darwin"):
... assert get_system_cache_directory().endswith("/Library/Caches")
>>> if sys.platform.startswith("linux"):
Expand Down
Loading