Skip to content

Commit

Permalink
ENH: use pathlib for OS-independent folders
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Mar 9, 2024
1 parent 9dafbc8 commit 65c4465
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
if: startsWith(github.ref, 'refs/tags')
secrets: inherit
uses: ComPWA/actions/.github/workflows/publish-to-pypi.yml@v1
with:
package-name: ampform
push:
if: startsWith(github.ref, 'refs/tags') && !github.event.release.prerelease
secrets: inherit
Expand Down
15 changes: 8 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 Down Expand Up @@ -335,7 +334,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 +356,13 @@ def perform_cached_doit(
"""
if cache_directory is None:
system_cache_dir = get_system_cache_directory()
cache_directory = abspath(f"{system_cache_dir}/ampform")
cache_directory = Path(system_cache_dir) / "ampform"
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

0 comments on commit 65c4465

Please sign in to comment.