-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterfaces.py
More file actions
56 lines (40 loc) · 1.21 KB
/
Copy pathinterfaces.py
File metadata and controls
56 lines (40 loc) · 1.21 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
45
46
47
48
49
50
51
52
53
54
55
56
import os
import json
from json import JSONEncoder
class Book(object):
def __init__(self, **kwargs):
for arg in kwargs.items():
setattr(self, arg[0], arg[1])
def __eq__(self, value):
return self.__dict__ == value.__dict__
class Preference(Book):
pass
class ObjEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
class LoaderInterfacee(object):
def load_data(self):
pass
def save_data(self, data):
pass
class JsonLoaderInterface(LoaderInterfacee):
encoder = ObjEncoder
obj = None
def load_data(self):
self.data = []
if not hasattr(self, "path"):
raise ValueError()
if os.path.isfile(self.path):
with open(self.path, 'r') as f:
cache = json.load(f)
for item in cache:
self.data.append(self.obj(**item))
def save_data(self, datas):
if not hasattr(self, "path"):
raise ValueError()
with open(self.path, 'w') as f:
f.write(json.dumps(self.data, cls=self.encoder))
class JsonBookLoader(JsonLoaderInterface):
obj = Book
class JsonPrefLoader(JsonLoaderInterface):
obj = Preference