Example #1
0
 def test_from_card_spec_malformed(self):
     fc = FlashCard.from_card_spec("")
     self.assertEqual(None, fc)
     fc = FlashCard.from_card_spec("front : back # abc")
     self.assertEqual(None, fc)
     fc = FlashCard.from_card_spec("front : back # 3 @ ab")
     self.assertEqual(None, fc)
     fc = FlashCard.from_card_spec("front : back # 123@")
     self.assertEqual(None, fc)
Example #2
0
 def test_next_expiry(self):
     expiries = [0, 500, 1000, 5000, 8000, 10000]
     deck = Deck(expiries, time_fun=lambda: 2001)
     deck.insert_card(FlashCard("front", "back", box=5, timestamp=2000))
     # Cards in last box never expire.
     self.assertEqual(None, deck.next_expiry())
     deck.insert_card(FlashCard("front", "back", box=4, timestamp=1000))
     self.assertEqual(1000 + 8000 - 2001, deck.next_expiry())
     deck.insert_card(FlashCard("front", "back", box=2, timestamp=1500))
     self.assertEqual(1500 + 1000 - 2001, deck.next_expiry())
     deck.insert_card(FlashCard("front", "back", box=2, timestamp=1000))
     self.assertEqual(0, deck.next_expiry())
Example #3
0
 def test_get_statistics(self):
     expiries = [0, 500, 1000, 5000, 8000, 10000]
     deck = Deck(expiries, time_fun=lambda: 2001)
     self.assertEqual([[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], deck.get_statistics())
     deck.insert_card(FlashCard("front", "back", box=0, timestamp=0))
     deck.insert_card(FlashCard("front", "back", box=0, timestamp=1000))
     deck.insert_card(FlashCard("front", "back", box=0, timestamp=1700))
     deck.insert_card(FlashCard("front", "back", box=1, timestamp=1000))
     deck.insert_card(FlashCard("front", "back", box=1, timestamp=1800))
     deck.insert_card(FlashCard("front", "back", box=1, timestamp=1900))
     deck.insert_card(FlashCard("front", "back", box=2, timestamp=1000))
     deck.insert_card(FlashCard("front", "back", box=2, timestamp=1500))
     deck.insert_card(FlashCard("front", "back", box=5, timestamp=1000))
     deck.insert_card(FlashCard("front", "back", box=5, timestamp=2000))
     self.assertEqual([[3, 3], [3, 1], [2, 1], [0, 0], [0, 0], [2, 0]], deck.get_statistics())
Example #4
0
def new_card():
    card_json = request.get_json()
    front = card_json['front']
    back = card_json['back']
    card = FlashCard(front=front, back=back)
    CurrentDeck.add_card(card)
    return '', 200
Example #5
0
 def shuffleCards(self):
     for card in self.card:
         card.destroy()
     self.card.clear()
     for i in range(self.amountCards):
         self.card.append(FlashCard(self.cardFrame))
     self.inputField.delete(0, END)
     self.inputField.focus()
Example #6
0
 def test_from_card_spec_corner_cases(self):
     fc = FlashCard.from_card_spec("front:")
     self.assertEqual("front:", fc.front)
     self.assertEqual("", fc.back)
     fc = FlashCard.from_card_spec("front :")
     self.assertEqual("front :", fc.front)
     self.assertEqual("", fc.back)
     fc = FlashCard.from_card_spec("front : ")
     self.assertEqual("front", fc.front)
     self.assertEqual("", fc.back)
     fc = FlashCard.from_card_spec("front : back @ 123")
     self.assertEqual("front", fc.front)
     self.assertEqual("back @ 123", fc.back)
     fc = FlashCard.from_card_spec("front : back #")
     self.assertEqual("front", fc.front)
     self.assertEqual("back #", fc.back)
     self.assertEqual(0, fc.box)
     self.assertEqual(0, fc.timestamp)
     fc = FlashCard.from_card_spec("front : back #123")
     self.assertEqual("front", fc.front)
     self.assertEqual("back #123", fc.back)
     self.assertEqual(0, fc.box)
     self.assertEqual(0, fc.timestamp)
     fc = FlashCard.from_card_spec("front :  # 7 @ 4711")
     self.assertEqual(fc.front, "front")
     self.assertEqual(fc.back, "")
     self.assertEqual(fc.box, 7)
     self.assertEqual(fc.timestamp, 4711)
Example #7
0
 def test_get_next_card_with_timestamp(self):
     expiries = [0, 1, 2, 3, 4, 100]
     deck = Deck(expiries)
     deck.insert_card(FlashCard("q4", "a4", timestamp=2000), 1)
     deck.insert_card(FlashCard("q5", "a5", timestamp=1000), 1)
     deck.insert_card(FlashCard("q1", "a1", timestamp=9000), 0)
     deck.insert_card(FlashCard("q2", "a2", timestamp=8000), 0)
     deck.insert_card(FlashCard("q3", "a3", timestamp=7000), 0)
     deck.insert_card(FlashCard("q6", "a6", timestamp=10000), 2)
     deck.time_fun = lambda: 8001
     self.assertEqual("q2", deck.get_next_card(consume=True).front)
     self.assertEqual("q3", deck.get_next_card(consume=True).front)
     self.assertEqual("q4", deck.get_next_card(consume=True).front)
     self.assertEqual("q5", deck.get_next_card(consume=True).front)
     self.assertEqual(None, deck.get_next_card(consume=True))
     deck.time_fun = lambda: 10000
     self.assertEqual("q1", deck.get_next_card(consume=True).front)
     self.assertEqual(None, deck.get_next_card(consume=True))
     self.assertEqual(None, deck.get_next_card(consume=True))
     self.assertEqual(None, deck.get_next_card(consume=True))
     deck.time_fun = lambda: 10001
     self.assertEqual(None, deck.get_next_card(consume=True))
     deck.time_fun = lambda: 10002
     self.assertEqual("q6", deck.get_next_card(consume=True).front)
     self.assertEqual(None, deck.get_next_card(consume=True))
     deck.insert_card(FlashCard("q7", "a7", timestamp=10002), 0)
     self.assertEqual("q7", deck.get_next_card(consume=True).front)
     self.assertEqual(None, deck.get_next_card(consume=True))
Example #8
0
 def __init__(self, master):
     
     master.title('"24" Game')
     
     Grid.rowconfigure(master, 0, weight=1)
     Grid.columnconfigure(master, 0, weight=1)
     
     self.mainNumber = IntVar()
     self.mainNumber.set(24)
 
     self.amountCards = 4
     
     self.menu = Menu(master)
     self.menu.add_command(label="Settings", command=lambda: self.openSettings(master))
     self.menu.add_command(label="About", command=lambda: self.openAbout(master))
     master.config(menu=self.menu)
     
     self.title = Label(master, textvariable=self.mainNumber, font=("", 72, "bold"))
     self.title.grid(row=0, pady=10)
     
     self.description = Label(master, text=self.createDescription())
     self.description.grid(row=1, padx=100, pady=10)
     
     self.cardFrame = Frame(master)
     self.cardFrame.grid(row=2, pady=10)
     
     self.card = []
     for i in range(self.amountCards):
         self.card.append(FlashCard(self.cardFrame))
         
     self.shuffleButton = ttk.Button(master, text="New Deal", command=lambda: self.shuffleCards())  
     self.shuffleButton.grid(row=3, pady=10)
     
     self.inputFrame = Frame(master)
     self.inputFrame.grid(row=4, pady=10)
     
     self.inputLabel = Label(self.inputFrame, text="Answer")
     self.inputLabel.pack(side=LEFT)
     
     self.equation = StringVar()
     self.equation.trace('w', self.updateCardColors)
     
     self.inputField = ttk.Entry(self.inputFrame, textvariable = self.equation)
     self.inputField.pack(side=LEFT, padx=5)
     self.inputField.bind('<Return>', lambda event: self.openSubmit(master))
     self.inputField.focus()
     
     self.inputButton = ttk.Button(self.inputFrame, text="Submit", command=lambda: self.openSubmit(master))
     self.inputButton.pack(side=LEFT)
     
     self.credits = Label(master, text = 'v0.1 | Amos Yu | 2019', font=("", 8))
     self.credits.grid(row=6, sticky=E)
Example #9
0
 def test_wrong(self):
     deck = Deck()
     deck.insert_card(FlashCard("q3", "a3"), 3)
     card = deck.get_next_card(consume=True)
     self.assertEqual(3, card.box)
     deck.wrong(card)
     self.assertEqual(0, card.box)
     card.timestamp = 0
     deck.restart()
     card = deck.get_next_card(consume=True)
     card.timestamp = 0
     self.assertEqual(0, card.box)
     self.assertEqual("q3", card.front)
Example #10
0
 def test_card_expiry(self):
     expiries = [0, 500, 1000, 5000, 8000, 10000]
     deck = Deck(expiries)
     c0 = FlashCard("front", "back", box=0, timestamp=1000)
     self.assertTrue(deck.card_expired(c0, time_fun=lambda: 1000))
     c1 = FlashCard("front", "back", box=1, timestamp=1000)
     self.assertFalse(deck.card_expired(c1, time_fun=lambda: 1000 + 100))
     self.assertFalse(deck.card_expired(c1, time_fun=lambda: 1000 + 499))
     self.assertTrue(deck.card_expired(c1, time_fun=lambda: 1000 + 500))
     self.assertTrue(deck.card_expired(c1, time_fun=lambda: 1000 + 501))
     c2 = FlashCard("front", "back", box=2, timestamp=1000)
     self.assertFalse(deck.card_expired(c2, time_fun=lambda: 1000))
     self.assertFalse(deck.card_expired(c2, time_fun=lambda: 1000 + 100))
     self.assertFalse(deck.card_expired(c2, time_fun=lambda: 1000 + 999))
     self.assertTrue(deck.card_expired(c2, time_fun=lambda: 1000 + 1000))
     self.assertTrue(deck.card_expired(c2, time_fun=lambda: 1000 + 1001))
     # Card in last box never expires.
     c3 = FlashCard("front", "back", box=5, timestamp=1000)
     self.assertFalse(deck.card_expired(c3, time_fun=lambda: 1000))
     self.assertFalse(deck.card_expired(c3, time_fun=lambda: 1000 + 9999))
     self.assertFalse(deck.card_expired(c3, time_fun=lambda: 1000 + 10000))
     self.assertFalse(deck.card_expired(c3, time_fun=lambda: 1000 + 100000))
Example #11
0
    def test_simple_session(self):
        expiries = [0, 1, 2, 3, 4, 100]
        deck = Deck(expiries)
        deck.time_fun = lambda: 1000
        self.assertEqual(None, deck.get_next_card(consume=True))
        deck.insert_card(FlashCard("q1", "a1", timestamp=1000), 0)

        # Card in box 0, right answer, move to 1
        deck.time_fun = lambda: 1001
        card = deck.get_next_card(consume=True)
        self.assertEqual("q1", card.front)
        deck.wrong(card)
        self.assertEqual(1001, card.timestamp)
        card = deck.get_next_card(consume=True)
        self.assertEqual("q1", card.front)
        self.assertEqual(1001, card.timestamp)
        deck.time_fun = lambda: 1002
        deck.right(card)
        self.assertEqual(1, card.box)
        self.assertEqual(1002, card.timestamp)

        # Card now in box 1, right answer, move to 2
        card = deck.get_next_card(consume=True)
        self.assertEqual(None, deck.get_next_card(consume=True))
        # .. Expire
        deck.time_fun = lambda: 1003
        card = deck.get_next_card(consume=True)
        self.assertEqual("q1", card.front)
        deck.right(card)
        self.assertEqual(2, card.box)
        self.assertEqual(1003, card.timestamp)

        # Card now in box 2, wrong answer, move to 0
        card = deck.get_next_card(consume=True)
        self.assertEqual(None, deck.get_next_card(consume=True))
        deck.time_fun = lambda: 1004
        card = deck.get_next_card(consume=True)
        self.assertEqual(None, deck.get_next_card(consume=True))
        deck.time_fun = lambda: 1005
        card = deck.get_next_card(consume=True)
        self.assertEqual("q1", card.front)
        deck.wrong(card)
        self.assertEqual(0, card.box)
        self.assertEqual(1005, card.timestamp)

        # Card now in box 0, wrong answer, stay in box 0
        card = deck.get_next_card(consume=True)
        self.assertEqual("q1", card.front)
        deck.wrong(card)
        self.assertEqual(0, card.box)
        self.assertEqual(1005, card.timestamp)
Example #12
0
    def test_from_card_spec_happy_path(self):
        fc = FlashCard.from_card_spec("front")
        self.assertEqual("front", fc.front)
        self.assertEqual("", fc.back)
        self.assertEqual(0, fc.box)
        self.assertEqual(0, fc.timestamp)

        fc = FlashCard.from_card_spec("front : back")
        self.assertEqual("front", fc.front)
        self.assertEqual("back", fc.back)
        self.assertEqual(0, fc.box)
        self.assertEqual(0, fc.timestamp)

        fc = FlashCard.from_card_spec("front : back # 3")
        self.assertEqual("front", fc.front)
        self.assertEqual("back", fc.back)
        self.assertEqual(3, fc.box)
        self.assertEqual(0, fc.timestamp)

        fc = FlashCard.from_card_spec("front : back # 3 @ 1000")
        self.assertEqual("front", fc.front)
        self.assertEqual("back", fc.back)
        self.assertEqual(3, fc.box)
        self.assertEqual(1000, fc.timestamp)
Example #13
0
 def test_right(self):
     deck = Deck()
     deck.insert_card(FlashCard("q3", "a3"), 3)
     card = deck.get_next_card(consume=True)
     self.assertEqual(3, card.box)
     deck.right(card)
     card.timestamp = 0
     card = deck.get_next_card(consume=True)
     self.assertEqual(4, card.box)
     self.assertEqual("q3", card.front)
     # Cannot promote card beyond last box.
     deck.right(card)
     card.timestamp = 0
     self.assertEqual(5, card.box)
     self.assertEqual("q3", card.front)
Example #14
0
 def load_from_specs(self, card_specs):
     for line in card_specs:
         card_spec = line.rstrip()
         # Handle empty lines and comments.
         if not card_spec or card_spec.startswith(Deck.comment_leader):
             self.deckfile_lines.append(card_spec)
         else:
             card = FlashCard.from_card_spec(card_spec)
             if card:
                 if 0 <= card.box <= self.max_box_num:
                     self.boxes[card.box].append(card)
                 else:
                     raise Deck.CardSpecError("Box number out of range: " +
                                              card_spec)
                 self.deckfile_lines.append(card)
             else:
                 raise Deck.CardSpecError("Malformed card spec: " +
                                          card_spec)
Example #15
0
    def test_dont_move_beyond_last_box(self):
        expiries = [0, 1, 2, 3, 4, 100]
        deck = Deck(expiries)
        deck.time_fun = lambda: 1000
        self.assertEqual(None, deck.get_next_card(consume=True))
        deck.insert_card(FlashCard("q1", "a1", timestamp=1000), 4)

        # Card in box 4, right answer, move to 5
        deck.time_fun = lambda: 1004
        card = deck.get_next_card(consume=True)
        self.assertEqual("q1", card.front)
        deck.right(card)
        self.assertEqual(5, card.box)
        self.assertEqual(1004, card.timestamp)

        # Card in last box. Never expires, never presented.
        deck.time_fun = lambda: 1500
        card = deck.get_next_card(consume=True)
        self.assertEqual(None, card)
Example #16
0
 def test_get_next_card_simple(self):
     deck = Deck()
     deck.insert_card(FlashCard("q4", "a4"), 1)
     deck.insert_card(FlashCard("q5", "a5"), 1)
     deck.insert_card(FlashCard("q1", "a1"), 0)
     deck.insert_card(FlashCard("q2", "a2"), 0)
     deck.insert_card(FlashCard("q3", "a3"), 0)
     deck.insert_card(FlashCard("q6", "a6"), 2)
     self.assertEqual("q1", deck.get_next_card(consume=True).front)
     self.assertEqual("q2", deck.get_next_card(consume=True).front)
     self.assertEqual("q3", deck.get_next_card(consume=True).front)
     self.assertEqual("q4", deck.get_next_card(consume=True).front)
     self.assertEqual("q5", deck.get_next_card(consume=True).front)
     self.assertEqual("q6", deck.get_next_card(consume=True).front)
     self.assertEqual(None, deck.get_next_card(consume=True))
Example #17
0
 def test_to_card_spec(self):
     fc = FlashCard("front", "back", box=4, timestamp=100)
     self.assertEqual("front : back # 4 @ 100", fc.to_card_spec())
     fc = FlashCard("front1", box=7)
     self.assertEqual("front1 :  # 7 @ 0", fc.to_card_spec())
Example #18
0
 def test_flash_card(self):
     fc = FlashCard("the front", "the back")
     self.assertEqual(fc.front, "the front")
     self.assertEqual(fc.back, "the back")