diff --git a/EDdA/classification/nGramsFrequencies.py b/EDdA/classification/nGramsFrequencies.py index f80816a36972b6125864d1baf12f44b17f4e7a7e..664be4a5f1446e05796d159cc1f86228b194ac3a 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 4a96f83bbcdcebfc00b24cbee1149e58f16a9a27..108a470733b2d98ce04eed5da5529de7b92c5042 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)