better cache decorator
This commit is contained in:
@@ -10,11 +10,13 @@ import string
|
|||||||
import tomllib
|
import tomllib
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from pathlib import Path
|
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 = os.getenv("SCGENERATOR_CACHE_DIR") or Path.home() / ".cache" / "scgenerator"
|
||||||
CACHE_DIR = Path(CACHE_DIR)
|
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")
|
Ts = TypeVarTuple("Ts")
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
@@ -27,7 +29,7 @@ def sort_dict(value: Any) -> dict[str, Any]:
|
|||||||
|
|
||||||
|
|
||||||
def normalize_path(s: str) -> str:
|
def normalize_path(s: str) -> str:
|
||||||
return ACCEPTED.sub("_", s)
|
return ACCEPTED.sub("_", " ".join(WHITESPACE.split(s))).strip()
|
||||||
|
|
||||||
|
|
||||||
class Cache:
|
class Cache:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import scgenerator.cache as scc
|
import scgenerator.cache as scc
|
||||||
from scgenerator.cache import Cache
|
from scgenerator.cache import Cache, methodcache, normalize_path
|
||||||
|
|
||||||
|
|
||||||
def test_io():
|
def test_io():
|
||||||
@@ -60,8 +60,21 @@ def test_toml():
|
|||||||
assert not cache3.dir.exists()
|
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():
|
def test_decorator():
|
||||||
cache = Cache("Test")
|
cache = Cache("Test")
|
||||||
|
cache.compile_class(Stuff)
|
||||||
cache.delete()
|
cache.delete()
|
||||||
call_count = 0
|
call_count = 0
|
||||||
|
|
||||||
@@ -77,12 +90,38 @@ def test_decorator():
|
|||||||
call_count += 1
|
call_count += 1
|
||||||
return sum(some_list)
|
return sum(some_list)
|
||||||
|
|
||||||
|
Stuff.from_other = cache(lambda _, *args: str(args))(Stuff.from_other)
|
||||||
|
|
||||||
assert func("hello") == "hellohello"
|
assert func("hello") == "hellohello"
|
||||||
assert func2([0, 1, 2, 80]) == 83
|
assert func2([0, 1, 2, 80]) == 83
|
||||||
assert func2([0, 1, 2, 80]) == 83
|
assert func2([0, 1, 2, 80]) == 83
|
||||||
|
|
||||||
assert (scc.CACHE_DIR / "Test" / "test_decorator.<locals>.func hello").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 (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
|
assert call_count == 2
|
||||||
cache.delete()
|
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"
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user