-
Notifications
You must be signed in to change notification settings - Fork 19
/
flashcards.py
159 lines (136 loc) · 5.91 KB
/
flashcards.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import datetime
import pickle
from hashlib import md5
from os import path
import sm2
from random import randint
from config import getflashcardsTag
import utils
class Flashcard:
def __init__(self, question, answer, source):
self.question = question
self.answer = answer
self.source = source
self.next = datetime.datetime(2021, 1, 1).timestamp()
self.lastAnswered = datetime.datetime(2021, 1, 1).timestamp()
self.history = []
def __repr__(self):
a = [self.question, self.answer, self.next, self.source, self.history]
return str(a) #"[% s ][% s]" % (self.question, self.answer)
def updateProperties(self, next, history):
self.next = next
self.history = history
SEPARATOR = "#"
flashcardsDB = "flashcards.db"
def scan4Flashcards(content):
Qlist = []
buildFlashcardList(content, Qlist)
return (Qlist)
def countIdent(line): # TODO change that to .index and merge with utils function
count = 0
if(len(line) > 0):
while line[count] == SEPARATOR:
if(count == len(line) - 1):
break
count += 1
return count
def buildFlashcardList(content, Qlist):
lines = content.split('\n')
i = 0
source = ""
while i <= len(lines) - 1:
if 'title:' in (lines[i]).lower():
source = lines[i].strip()
if getflashcardsTag() in lines[i]:
flashcardIndent = countIdent(lines[i])
isSub = True
i += 1
flashcard = Flashcard("-1", "-1", source)
while isSub:
if( i == len(lines)):
break
currentIdent = countIdent(lines[i])
if(currentIdent == flashcardIndent + 1):
if(flashcard.question != "-1"):
Qlist.append(flashcard)
flashcard = Flashcard("-1", "-1", source)
flashcard.question = lines[i][currentIdent:].strip()
i += 1
elif(currentIdent > flashcardIndent + 1): # scan for answer
blockRef = utils.containsRefBlock(lines[i])
answer = ""
if (blockRef):
origLine = (lines[i][currentIdent:]).replace("(("+blockRef+"))","").strip()
if(origLine):
answer = origLine + " "
answer += utils.findOrigBlock(blockRef)
else:
answer = lines[i][currentIdent:]
if(flashcard.answer == "-1"):
flashcard.answer = ""
flashcard.answer += answer.strip() + "\n"
i += 1
else:
isSub = False
i -= 1
if(flashcard.answer != "-1"):
Qlist.append(flashcard)
i += 1
def saveFlashcardsDB(flashcardList, dump=False):
if(dump): # don't check for differences
with open(flashcardsDB, "wb") as fp: # Pickling
pickle.dump(flashcardList, fp)
print (str(len(flashcardList)) + " cards saved")
elif(not(path.exists(flashcardsDB))): # new DB
with open(flashcardsDB, "wb") as fp: # Pickling
pickle.dump(flashcardList, fp)
return [str(len(flashcardList)), "0" ]
else: # updating exsiting DB
savedDB = loadFlashcardsDB()
tmpset = set((x.question) for x in savedDB)
newFlashcards = [ x for x in flashcardList if (x.question) not in tmpset ]
tmpset = set((x.question, x.answer) for x in savedDB)
updatedFlashcards = [ x for x in flashcardList if ((x.question, x.answer) not in tmpset and x not in newFlashcards)]
if updatedFlashcards: # no need to check for new since they will be saved with the updated ones (if any)
for updatedFlashcard in updatedFlashcards:
cardDetails = getFlashcardDetails(updatedFlashcard.question, savedDB)
cardIndex = flashcardList.index(updatedFlashcard)
print(cardDetails.index)
flashcardList[cardIndex].updateProperties(cardDetails[0].next, cardDetails[0].history)
print(flashcardList[cardIndex])
# print(updatedAnswers)
saveFlashcardsDB(flashcardList, True)
elif newFlashcards:
savedDB += newFlashcards
saveFlashcardsDB(savedDB, True)
print(newFlashcards)
return [str(len(newFlashcards)), str(len(updatedFlashcards)) ]
def loadFlashcardsDB():
with open(flashcardsDB, "rb") as fp: # Unpickling
db = pickle.load(fp)
return db
def getFlashcardDetails(question, flashcardList = ""):
if (not(flashcardList)):
flashcardList = loadFlashcardsDB() # in saved DB
#print (flashcardList[0])
return [x for x in flashcardList if x.question == question]
def updateFlashcard(flaschard):
flashcardsDB = loadFlashcardsDB()
flaschard.lastAnswered = (datetime.datetime.now()).timestamp()
flaschard.next = flaschard.lastAnswered + sm2.supermemo_2(flaschard.history) * 86400
cardIndex = next(i for i, x in enumerate(flashcardsDB) if x.question == flaschard.question)
print(flashcardsDB[cardIndex])
print(datetime.datetime.fromtimestamp(flashcardsDB[cardIndex].next) )
flashcardsDB[cardIndex] = flaschard
print(flashcardsDB[cardIndex])
print(datetime.datetime.fromtimestamp(flashcardsDB[cardIndex].next) )
saveFlashcardsDB(flashcardsDB, True)
return datetime.datetime.fromtimestamp(flaschard.next).strftime("%Y-%m-%d")
def getFlashcardFromPool():
flashcardsList = loadFlashcardsDB()
tsToday = (datetime.datetime.now()).timestamp()
overdueFC = [ x for x in flashcardsList if (x.next) <= tsToday ] # get overdue FC
if overdueFC:
return(overdueFC[randint(0,len(overdueFC)-1)])
else:
return None