-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_struct.py
More file actions
65 lines (49 loc) · 1.63 KB
/
data_struct.py
File metadata and controls
65 lines (49 loc) · 1.63 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
57
58
59
60
61
62
63
64
65
import json
import sublime
from .utils import get_file_path_cache
STORAGE_CACHE_NAME = "typing_learn_word"
class BaseDataStruct(object):
def __init__(self):
self._storage = {}
self.read_storage()
def read_storage(self):
path = get_file_path_cache(STORAGE_CACHE_NAME)
try:
fp = open(path)
content = fp.read()
fp.close()
self._storage = json.loads(content) or {}
except Exception:
sublime.error_message("Cann't read local storage data.")
def save_storage(self):
path = get_file_path_cache(STORAGE_CACHE_NAME)
try:
fp = open(path, "w+")
fp.write(json.dumps(self._storage))
fp.close()
except Exception:
sublime.error_message("Cann't save local storage data.")
class Book(BaseDataStruct):
def __init__(self, name):
super().__init__()
self.name = name
if self._storage.get(name) is None:
self._storage[name] = []
def add_word(self, word):
self._storage[self.name].append(word)
self.save_storage()
def remove_word(self, word):
self._storage[self.name].remove(word)
self.save_storage()
def remove_all_words(self):
self._storage[self.name] = []
self.save_storage()
def get_words(self):
return self._storage[self.name]
class BooksManager(BaseDataStruct):
def __init__(self):
super().__init__()
def get_book_list(self):
return list(self._storage.keys())
def get_word_by_book_name(self, name):
return self._storage.get(name, [])