cache from toml must be used with a title

This commit is contained in:
2024-07-11 15:55:37 +02:00
parent c3db790281
commit 03602bd816
2 changed files with 9 additions and 6 deletions

View File

@@ -65,9 +65,12 @@ class Cache:
return cls(group) return cls(group)
@classmethod @classmethod
def from_toml(cls, s: str, /, create: bool = True) -> Self: def from_toml(cls, name: str, toml_str: str, /, create: bool = True) -> Self:
hashed = hashlib.md5(pickle.dumps(sort_dict(tomllib.loads(s)))).hexdigest() hashed = hashlib.md5(pickle.dumps(sort_dict(tomllib.loads(toml_str)))).hexdigest()
group = f"TOML-{hashed}" if len(name) > 50:
warnings.warn(f"name {name!r} is longer than 30 characters")
name = name[:50]
group = f"TOML-{name}-{hashed}"
if create: if create:
os.makedirs(CACHE_DIR / group, exist_ok=True) os.makedirs(CACHE_DIR / group, exist_ok=True)
return cls(group) return cls(group)

View File

@@ -51,9 +51,9 @@ def test_toml():
rin_measurement='./DualComb1GHz_updated_corrected_extrapolated_1GHz_noqn.csv' rin_measurement='./DualComb1GHz_updated_corrected_extrapolated_1GHz_noqn.csv'
""" """
cache1 = Cache.from_toml(s1) cache1 = Cache.from_toml("test", s1)
cache3 = Cache.from_toml(s3, create=False) cache3 = Cache.from_toml("test", s3, create=False)
assert cache1.dir == Cache.from_toml(s2).dir assert cache1.dir == Cache.from_toml("test", s2).dir
assert cache1.dir != cache3.dir assert cache1.dir != cache3.dir
assert cache1.dir.exists() assert cache1.dir.exists()
cache1.delete() cache1.delete()