From deabb234685e12be1a2390f3d1368533ea305c60 Mon Sep 17 00:00:00 2001 From: Alice BRENON <alice.brenon@ens-lyon.fr> Date: Tue, 29 Mar 2022 11:16:11 +0200 Subject: [PATCH] Add more general file utils to the cache module and rename it 'store' --- EDdA/classification/nGramsFrequencies.py | 2 +- EDdA/{cache.py => store.py} | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) rename EDdA/{cache.py => store.py} (78%) diff --git a/EDdA/classification/nGramsFrequencies.py b/EDdA/classification/nGramsFrequencies.py index f80816a..664be4a 100644 --- a/EDdA/classification/nGramsFrequencies.py +++ b/EDdA/classification/nGramsFrequencies.py @@ -1,4 +1,4 @@ -from EDdA.cache import Cache +from EDdA.store import Cache from EDdA import data import nltk import pandas diff --git a/EDdA/cache.py b/EDdA/store.py similarity index 78% rename from EDdA/cache.py rename to EDdA/store.py index 4a96f83..108a470 100644 --- a/EDdA/cache.py +++ b/EDdA/store.py @@ -1,13 +1,14 @@ -import json import os import os.path +def preparePath(template, **kwargs): + path = template.format(**kwargs) + os.makedirs(os.path.dirname(path), exist_ok=True) + return path + class Cache: ROOT = "cache" - def filePath(symbolicPath): - return "{root}/{path}".format(root=Cache.ROOT, path=symbolicPath) - def __init__(self, loader, pathPolicy=lambda *args:str(args) , serializer=None, unserializer=None): self.RAM = {} @@ -26,14 +27,14 @@ class Cache: def heat(self, symbolicPath): if self.unserializer and symbolicPath not in self.RAM: - path = Cache.filePath(symbolicPath) + path = "{root}/{path}".format(root=Cache.ROOT, path=symbolicPath) if os.path.isfile(path): with open(path, 'r') as f: self.RAM[symbolicPath] = self.unserializer(f) def save(self, symbolicPath): if self.serializer and symbolicPath in self.RAM: - path = Cache.filePath(symbolicPath) + path = preparePath("{root}/{path}", root=Cache.ROOT, path=symbolicPath) os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, 'w') as f: self.serializer(self.RAM[symbolicPath], f) -- GitLab