Skip to content
Snippets Groups Projects
Commit deabb234 authored by Alice Brenon's avatar Alice Brenon
Browse files

Add more general file utils to the cache module and rename it 'store'

parent d7e9f12b
No related branches found
No related tags found
No related merge requests found
from EDdA.cache import Cache from EDdA.store import Cache
from EDdA import data from EDdA import data
import nltk import nltk
import pandas import pandas
......
import json
import os import os
import os.path import os.path
def preparePath(template, **kwargs):
path = template.format(**kwargs)
os.makedirs(os.path.dirname(path), exist_ok=True)
return path
class Cache: class Cache:
ROOT = "cache" ROOT = "cache"
def filePath(symbolicPath):
return "{root}/{path}".format(root=Cache.ROOT, path=symbolicPath)
def __init__(self, loader, pathPolicy=lambda *args:str(args) def __init__(self, loader, pathPolicy=lambda *args:str(args)
, serializer=None, unserializer=None): , serializer=None, unserializer=None):
self.RAM = {} self.RAM = {}
...@@ -26,14 +27,14 @@ class Cache: ...@@ -26,14 +27,14 @@ class Cache:
def heat(self, symbolicPath): def heat(self, symbolicPath):
if self.unserializer and symbolicPath not in self.RAM: 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): if os.path.isfile(path):
with open(path, 'r') as f: with open(path, 'r') as f:
self.RAM[symbolicPath] = self.unserializer(f) self.RAM[symbolicPath] = self.unserializer(f)
def save(self, symbolicPath): def save(self, symbolicPath):
if self.serializer and symbolicPath in self.RAM: 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) os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w') as f: with open(path, 'w') as f:
self.serializer(self.RAM[symbolicPath], f) self.serializer(self.RAM[symbolicPath], f)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment