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
import nltk
import pandas
......
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)
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