-
Notifications
You must be signed in to change notification settings - Fork 5
/
scar_pickles.py
executable file
·69 lines (53 loc) · 2.03 KB
/
scar_pickles.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pprint
import sys
import os
import pickle
from collections.abc import Iterable
class SCARPickles(dict):
@classmethod
def loader(cls,file):
if os.path.isfile(file):
with open(file, "rb") as f:
return pickle.load(f)
else:
return False
def __init__(self, pickle_name, data = None):
dict.__init__(self)
if getattr(sys, 'frozen', False):
self['application_path'] = sys._MEIPASS
else:
self['application_path'] = os.path.dirname(os.path.abspath(__file__))
self['pickle_name'] = pickle_name
#see if the pickle already exists
existing_path = os.path.join( self['application_path'], 'data', self['pickle_name'] + '.pkl' )
existing_pickle = SCARPickles.loader( existing_path )
if existing_pickle and isinstance(existing_pickle, Iterable):
for key in existing_pickle.keys():
self[key] = existing_pickle[key]
if data and isinstance(data, Iterable):
for key in data.keys():
self[key] = data[key]
self.save()
def dump(self):
results = {}
for key in self.keys():
results[key] = self[key]
return results
def list(self):
return list(self.keys())
def get(self, key):
if key in self.keys():
return self[key]
else:
return None
def set(self, key, value):
self[key] = value
self.save()
def append(self, key, value):
if key in self.keys() and isinstance(self[key], Iterable):
self[key].append(value)
self.save()
def save(self):
if 'application_path' in self.keys() and 'pickle_name' in self.keys():
with open(os.path.join(self['application_path'], "data", f"{self['pickle_name']}.pkl"), "wb") as f:
pickle.dump(self, f)