-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveLoadUtils.py
More file actions
46 lines (34 loc) · 1.14 KB
/
saveLoadUtils.py
File metadata and controls
46 lines (34 loc) · 1.14 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
import os
import json
def save_to_file(todoList, doneList):
"""
Save the current todoList to the ~/todosave file
"""
filePath = os.path.expanduser('~/todoSave')
f = open(filePath, 'w+')
# wipe the old save file
f.truncate(0)
obj = {"todo": todoList, "done": doneList}
f.write(json.dumps(obj, indent=4))
def loadFromFile():
"""
Load the current todoList from the ~/todosave file
"""
# if we cant get a file, return empty arrays
try:
filePath = os.path.expanduser('~/todoSave')
fileContents = open(filePath, 'r').read()
todoList = json.loads(fileContents)["todo"]
doneList = json.loads(fileContents)["done"]
todoListFiltered = []
doneListFiltered = []
# we want to filter any non formatted "[ ] ..." strings from the list as they are errors
for item in todoList:
if "[ ]" in item:
todoListFiltered.append(item)
for item in doneList:
if "[ ]" in item:
doneListFiltered.append(item)
return todoListFiltered, doneListFiltered
except:
return [], []