-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.py
117 lines (99 loc) · 3.59 KB
/
playlist.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
from collections import deque
from . import (
Deck,
Flashcard,
get_scoped_session,
get_universal_session,
FlashcardAnswer,
)
from .enums import (
DifficultyLevel,
StudyType,
)
import random
class Playlist:
# Define difficulty levels
difficulties = {
"Easy": [DifficultyLevel.Easy, DifficultyLevel.Medium, DifficultyLevel.Hard],
"Hard": [DifficultyLevel.Hard, DifficultyLevel.Medium, DifficultyLevel.Easy],
}
def __init__(self, deck_ids, difficulty=None, study_type=None):
self.flashcard_queue = deque()
# MAYBE WRONG. I do not know !!!
self.session = get_universal_session()
# Get flashcards from database
flashcards = (
self.session.query(Flashcard).filter(Flashcard.Deck_id.in_(deck_ids)).all()
)
self.sort_flascalrds(flashcards, difficulty, study_type)
self.flashcard_queue.extend(flashcards)
self.cur_card = None
def sort_flascalrds(self, flashcards, difficulty, study_type):
random.shuffle(flashcards)
if difficulty in self.difficulties:
order = self.difficulties[difficulty]
flashcards.sort(key=lambda x: order.index(x.difficulty_level))
# if study_type == StudyType.Learn:
# flashcards.sort(
# key=lambda x: (
# 1
# if x.FlashcardAnswers
# and any(answer.is_correct for answer in x.FlashcardAnswers)
# else 0,
# self.difficulties[difficulty].index(x.difficulty_level)
# if difficulty in self.difficulties
# else 0,
# )
# )
# elif study_type == StudyType.Review:
# flashcards.sort(
# key=lambda x: (
# 0
# if x.FlashcardAnswers
# and any(answer.is_correct for answer in x.FlashcardAnswers)
# else 1,
# self.difficulties[difficulty].index(x.difficulty_level)
# if difficulty in self.difficulties
# else 0,
# )
# )
# TODO: (#24) Add strategy pattern for sorting flashcards
if study_type == StudyType.Learn:
reverse = False
elif study_type == StudyType.Review:
reverse = True
else:
return
flashcards.sort(
key=lambda x: (
sum(1 for answer in x.FlashcardAnswers if answer.is_correct),
self.difficulties[difficulty].index(x.difficulty_level)
if difficulty in self.difficulties
else 0,
),
reverse=reverse,
)
def next(self):
if self.has_next():
self.cur_card = self.flashcard_queue.popleft()
return self.cur_card
else:
return None
def has_next(self):
return len(self) > 0
def record_flashcard_answer(self, is_correct: bool):
flashcard_answer = FlashcardAnswer()
flashcard_answer.is_correct = is_correct
flashcard_answer.Flashcard = self.cur_card
self.session.add(flashcard_answer)
self.session.commit()
def handle_cur(self, is_correct):
# TODO add entry to flashcard answers
self.record_flashcard_answer(is_correct)
if self.cur_card is None:
return
if not is_correct:
self.flashcard_queue.append(self.cur_card)
self.cur_card = None
def __len__(self):
return len(self.flashcard_queue)