From d84138f668dce79862a7438dca47969f82516118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Sierro?= Date: Wed, 28 Feb 2024 14:25:56 +0100 Subject: [PATCH] better cache decorator --- src/scgenerator/cache.py | 8 ++++--- tests/test_cache.py | 45 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/scgenerator/cache.py b/src/scgenerator/cache.py index 13b2b87..24e25c1 100644 --- a/src/scgenerator/cache.py +++ b/src/scgenerator/cache.py @@ -10,11 +10,13 @@ import string import tomllib from functools import wraps from pathlib import Path -from typing import Any, Callable, Mapping, ParamSpec, Self, TypeVar, TypeVarTuple +from typing import Any, Callable, Mapping, Self, TypeVar, TypeVarTuple CACHE_DIR = os.getenv("SCGENERATOR_CACHE_DIR") or Path.home() / ".cache" / "scgenerator" CACHE_DIR = Path(CACHE_DIR) -ACCEPTED = re.compile(string.ascii_letters + string.digits + r" \-_()\[\]\*~\.,=\+") +ACCEPTED = re.compile(f"[^{string.ascii_letters}{string.digits}" r" \-_()\[\]\*~\.,=\+" "]") +WHITESPACE = re.compile(r"\s+") +PRECACHED = {} Ts = TypeVarTuple("Ts") T = TypeVar("T") @@ -27,7 +29,7 @@ def sort_dict(value: Any) -> dict[str, Any]: def normalize_path(s: str) -> str: - return ACCEPTED.sub("_", s) + return ACCEPTED.sub("_", " ".join(WHITESPACE.split(s))).strip() class Cache: diff --git a/tests/test_cache.py b/tests/test_cache.py index 754d287..90ebf56 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -1,5 +1,5 @@ import scgenerator.cache as scc -from scgenerator.cache import Cache +from scgenerator.cache import Cache, methodcache, normalize_path def test_io(): @@ -60,8 +60,21 @@ def test_toml(): assert not cache3.dir.exists() +class Stuff: + call_count = 0 + + @classmethod + def from_other(cls, stuff: str): + cls.call_count += 1 + return cls(stuff[:5]) + + def __init__(self, x): + self.x = x + + def test_decorator(): cache = Cache("Test") + cache.compile_class(Stuff) cache.delete() call_count = 0 @@ -77,12 +90,38 @@ def test_decorator(): call_count += 1 return sum(some_list) + Stuff.from_other = cache(lambda _, *args: str(args))(Stuff.from_other) + assert func("hello") == "hellohello" assert func2([0, 1, 2, 80]) == 83 assert func2([0, 1, 2, 80]) == 83 - assert (scc.CACHE_DIR / "Test" / "test_decorator..func hello").exists() - assert (scc.CACHE_DIR / "Test" / "test_decorator..func2 0-80 4").exists() + assert (scc.CACHE_DIR / "Test" / "test_decorator._locals_.func hello").exists() + assert (scc.CACHE_DIR / "Test" / "test_decorator._locals_.func2 0-80 4").exists() + + assert Stuff.from_other("Bonjour").x == "Bonjo" + assert Stuff.from_other("Bonjour").x == "Bonjo" + assert Stuff.call_count == 1 assert call_count == 2 cache.delete() + + +def test_normalize(): + assert normalize_path("abd") == "abd" + assert normalize_path("a/b") == "a_b" + assert normalize_path("bonjour\nsdfk") == "bonjour sdfk" + assert normalize_path("a\\b") == "a_b" + assert ( + normalize_path( + """sdfl asdfk + + + + asd + + + """ + ) + == "sdfl asdfk asd" + )