-
Notifications
You must be signed in to change notification settings - Fork 6
/
note.py
77 lines (65 loc) · 1.81 KB
/
note.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
70
71
72
73
74
75
76
77
import json
import exceptions
import sys
class Notes:
def __init__(self, note_dic=None,
bkp_file=None):
self.note_dic = note_dic or dict()
self.bkp_file = bkp_file or '.notes'
self.load()
def load(self):
bkp = open(self.bkp_file, 'r')
self.note_dic = json.loads(bkp.read())
def read(self):
while True:
sys.stdout.write("Which? ")
name = sys.stdin.readline()\
.strip()
try:
print(self.note_dic[name])
break
except exceptions.KeyError:
pass
def write(self):
sys.stdout.write("Name? ")
name = sys.stdin.readline().strip()
sys.stdout.write("Body? ")
body = sys.stdin.readline().strip()
self.note_dic[name] = body
def delete(self):
while True:
sys.stdout.write("Which? ")
name = sys.stdin.readline()\
.strip()
try:
del self.note_dic[name]
break
except exceptions.KeyError:
pass
def list(self):
for name in self.note_dic:
print(name)
def persist(self):
bkp_file = open(self.bkp_file, 'w')
bkp_file.write(
json.dumps(self.note_dic))
bkp_file.close()
#read, list, write, delete, save
def run():
notes = Notes()
while True:
sys.stdout.write("what? ")
action = sys.stdin.readline().strip()
if action == 'r':
notes.read()
elif action == 'l':
notes.list()
elif action == 'w':
notes.write()
elif action == 'd':
notes.delete()
elif action == 's':
notes.persist()
elif action == 'q':
break
run()