-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_storage.py
51 lines (38 loc) · 1012 Bytes
/
memory_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#########
### Just store stuff in memory - not pretty but it'll be fine for this use
#########
from uuid import uuid4
from copy import copy
class MemoryStorage(object):
def __init__(self):
self.docs = {}
self.types = {}
def save(self, doc):
if not 'id' in doc:
doc['id'] = uuid4().hex
self.docs[doc['id']] = doc
if 'type' in doc:
if doc['type'] not in self.types:
self.types[doc['type']] = {}
self.types[doc['type']][doc['id']] = doc
return doc['id']
def get_by_type(self, doc_type):
if doc_type not in self.types:
return []
return [copy(doc) for doc in self.types[doc_type].values()]
def get(self, key):
if self.exists(key):
return copy(self.docs[key])
return None
def exists(self, key):
if key in self.docs:
return True
return False
def get_by_keys(self, keys):
return [self.get(key) for key in keys if self.exists(key)]
def empty(self):
self.docs = {}
self.types = {}
memory_storage = MemoryStorage()
def get_db():
return memory_storage