-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStore.py
More file actions
44 lines (37 loc) · 1.57 KB
/
DataStore.py
File metadata and controls
44 lines (37 loc) · 1.57 KB
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
#!/usr/bin/env python3
import json
import os.path
from os import path
class DataStore:
"""
DataStore is a utiliy for storing and retrieving lists of dictionaries from flat file storage.
This is meant to emulate a relational database and it is highly suggested, but not enforced, that the
dictionaries' keys be the same for each entry. Scalability was not a major consideration and DataStore
is not reccomended for more than 1000 rows.
"""
def __init__(self, filename):
if not filename.endswith('.json'):
raise NameError("Invalid file name chosen. DataStore filename must end in '.json' .")
self.filename = filename
if path.exists( filename ):
self.data = DataStore.__read_data_from_file(filename)
else:
self.data = []
# i'm not sure if an empty file should be created when the DataStore is initialized
# self.save()
def save(self):
with open( self.filename, 'w+' ) as f:
f.write( json.dumps( self.data, indent=2 ) )
def __read_data_from_file( filename ):
with open(filename) as f:
return json.loads( f.read() )
def add(self, element):
# This method will fail if the data is not appendable
self.data.append(element)
def removeAll(self, key, value):
# This is not a very fast way of preforming this operation. User beware 🐢.
newdata = []
for i in self.data:
if i[key] != value:
newdata.append(i)
self.data = newdata