-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
122 lines (114 loc) · 3.41 KB
/
commands.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from include import *
from word import *
from random import randint
from random import random
def display(cmd,effe,cmdlen=15,indent=2):
for i in range(0,indent):
print(" ",end="")
print(cmd,end="")
for i in range(0,cmdlen-len(cmd)):
print(" ",end="")
print(effe)
def know(namespace):
words=read()
if namespace.eng:
idx=-1
for i in range(0,len(words)):
if namespace.eng==words[i][0]:
idx=i
break
else:
print(namespace.eng+" not found!",file=stderr)
return
else:
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"r") as f:
idx=json.load(f)
except:
print("Can't fine the last word",file=stderr)
exit()
words[idx][2]*=0.9
words[idx][2]+=0.1
write(words)
def add(namespace):
words=read()
for word in words:
if namespace.eng==word[0]:
print(namespace.eng+" exists!")
return
words.append([namespace.eng,namespace.chi,0,1])
write(words)
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"w") as f:
json.dump((-1),f)
except FileNotFoundError:
pass
def version(namespace):
import version
print("version: %d.%d.%d"% (version.major, version.minor, version.revision))
print("last update", version.date)
def show(namespace):
words=read()
for word in words:
display(word[0],word[1],indent=0)
print(str(len(words))+" words in total!")
def word(namespace):
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"r") as f:
i=json.load(f)
except:
i=0
words=read()
if words == []:
print("No words in current vocabulary list!", file = stderr)
exit()
if i == -1:
i = len(words)-1
else:
while True:
i+=randint(0,len(words)-1)
i%=len(words)
if random()>words[i][2]:
break
word=words[i]
display(word[0],word[1],indent=0)
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"w") as f:
json.dump((i),f)
except FileNotFoundError:
pass
def init(namespace):
print("Are you sure you want to initialize? ALL files will be cleared unless you backup them manually")
print("""Input "Yes, I know exactly what I'm doing" to continue""")
if input()=="Yes, I know exactly what I'm doing":
words=[]
write(words)
else:
print("Initialization canceled")
def remove(namespace):
print(namespace)
return
words=read()
if namespace.word:
for i in range(0,len(words)):
if words[i][0]==namespace.word or words[i][1]==namespace.word:
del words[i]
write(words)
return
print(namespace.word+" not found!")
else:
try:
with open(os.path.expanduser("~/.vocab/last_word.json"),"r") as f:
i=json.load(f)
except:
print("Can't fine the last word",file=stderr)
exit()
print("The last word is ",end="")
display(words[i][0],words[i][1],indent=0)
print("Would you like to delete it from the list?(y for yes)")
if input() in {"y","yes"}:
del words[i]
write(words)
return
else:
print("operation canceled!")