Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flashcards #818

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added flashcards/dict.pickle
Binary file not shown.
111 changes: 111 additions & 0 deletions flashcards/flashcards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pickle
import random

class Flashcards:
# initializes with an empty dictionary
# key: category/subject name
# value: dictionary of cards for the given subject
# key: card's front
# value: card's back
def __init__(self):
self.categories = {}

# adds a category/subject
# returns True if subject does not exist and can be added
# returns False and does not add otherwise
def add_category(self, category_name):
if category_name not in self.categories:
self.categories[category_name] = {}
return True
else:
print("Category already exists.")
return False

# deletes a category/subject
# returns True if subject exists and is able to be deleted
# returns False otherwise
def delete_category(self, category_name):
if category_name in self.categories:
self.categories.pop(category_name)
return True
else:
print("Category does not exist.")
return False

# adds a card to a given category
# if the card exists already, the user is prompted whether or not to override
# returns True if card is added or overridden
# returns False otherwise
def add_card(self, category, front, back):
if category in self.categories:
if front in self.categories[category]:
ans = input("Card exists. Override(y/n)? ")
if ans == 'y':
self.categories[category][front] = back
return True
elif ans == 'n':
print("Ok. Exiting.")
return False
else:
print("Invalid input. Exiting.")
return False
else:
self.categories[category][front] = back
return True
else:
print("Category does not exist.")
return False

# deletes a given card from a given category
# returns True if category and card both exist and deletion completes
# returns False otherwise
def delete_card(self, category, front):
if category in self.categories:
if front in self.categories[category]:
self.categories[category].pop(front)
return True
else:
print("Card does not exist.")
return False
else:
print("Category does not exist.")
return False

# saves self.categories to a pickle file
def save(self):
with open('dict.pickle', 'wb') as f:
pickle.dump({
'categories': self.categories
}, f)

# loads self.categories from a pickle file
def load(self):
with open('dict.pickle', 'rb') as f:
params = pickle.load(f)
self.categories = params['categories']

# shuffles cards in a given category and goes through them
# after input, the correct answer is shown
# returns True when review is finished
# returns False if category does not exist or if the category has no cards
def review(self, category):
if category in self.categories and len(self.categories[category]) > 0:
cards = self.categories[category]
key_list = list(cards.keys())
random.shuffle(key_list)
for card in key_list:
input(card + ": ")
print(cards[card])
print()
return True
else:
print("Category does not exist or is empty.")
return False

if __name__ == '__main__':
flashcards = Flashcards()
flashcards.add_category("Japanese")
flashcards.add_card("Japanese", "ありがとう", "thank you")
flashcards.add_card("Japanese", "あめ", "rain")
flashcards.add_card("Japanese", "あまい", "sweet")
flashcards.review("Japanese")
63 changes: 63 additions & 0 deletions flashcards/flashcards_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest
from flashcards import Flashcards

class TestGuesser(unittest.TestCase):
# the test cards have two categories
# Pacman: {Pacman: "the player character in the game PacMan"}
# Spanish: {}
def setUp(self):
self.flashLib = Flashcards()
self.flashLib.add_category("Pacman")
self.flashLib.add_card("Pacman","Pacman","the player character in the game PacMan")
self.flashLib.add_category("Spanish")

# tests that new categories can be added and that existing categories cannot be added
def testAddCategories(self):
self.assertTrue(self.flashLib.add_category("Japanese")) # the category Japanese does not exist, and can be added
self.assertFalse(self.flashLib.add_category("Pacman")) # the category Pacman exists, so cannot be added again

# tests that new cards can be added to an existing category, but not to one that does not exist
def testAddCards(self):
self.assertTrue(self.flashLib.add_card("Pacman","Inky","the blue ghost in the game PacMan"))
self.assertFalse(self.flashLib.add_card("Nonexistant","Blinky","the red ghost in the game PacMan"))

# self.assertTrue(self.flashLib.add_card("Pacman","Clyde","the orange ghost in the game PacMan"))
# self.assertTrue(self.flashLib.add_card("Pacman","Clyde","the loser ghost in the game PacMan")) input: 'y'
# self.assertFalse(self.flashLib.add_card("Pacman","Clyde","the orange ghost in the game PacMan")) input: 'n'
# self.assertFalse(self.flashLib.add_card("Pacman","Clyde","the orange ghost in the game PacMan")) input: anything else

# tests that existing categories can be deleted and that nonexisting categories cannot
def testDeleteCategories(self):
self.assertTrue(self.flashLib.add_category("Test"))
self.assertTrue(self.flashLib.add_card("Test","front","back"))
self.assertTrue(self.flashLib.delete_category("Test"))
self.assertFalse(self.flashLib.delete_category("Test"))

# tests that existing cards can be deleted and that nonexisting cards and cards from nonexisting categories cannot
def testDeleteCards(self):
self.assertTrue(self.flashLib.add_category("Test"))
self.assertTrue(self.flashLib.add_card("Test","front","back"))
self.assertFalse(self.flashLib.delete_card("Test","back"))
self.assertTrue(self.flashLib.delete_card("Test","front"))
self.assertFalse(self.flashLib.delete_card("Test","front"))
self.assertFalse(self.flashLib.delete_card("Nonexistant", "front"))

# tests that save and load works
def testSaveAndLoad(self):
testCards = Flashcards()
testCards.add_category("Test")
testCards.add_card("Test","front","back")
#testCards.save()

newCards = Flashcards()
newCards.load()

self.assertFalse(newCards.add_category("Test"))

# tests that review works correctly
def testReview(self):
self.assertFalse(self.flashLib.review("Spanish"))
# self.assertTrue(self.flashLib.review("Pacman"))

if __name__ == '__main__':
unittest.main()
121 changes: 121 additions & 0 deletions flashcards/ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from tkinter import *
from flashcards import Flashcards

# the class for the Flashcards Application, using tkinter
# note: unfinished, also my first time using tkinter
# things to add:
# back button
# delete categories/cards feature
# review/quiz features
# use click event functions
# finish add categories/cards
# when a category is added or selected, should take user to add cards scene
class FlashcardsUI:
def __init__(self):
self.root = Tk()
self.root.title("Flashcard App")

self.frame = Frame(self.root, width=300, height=250)
#self.frame.bind("<Button-1>", self.leftClick)
#self.frame.bind("<Button-2>", self.middleClick)
#self.frame.bind("<Button-3>", self.rightClick)
self.frame.pack()

self.flashcards = Flashcards()
self.flashcards.load()

self.home()

self.root.mainloop()

# homescreen, with the options to add cards, review, or quiz
def home(self):
label = Label(self.root, text="Flashcards", font=("Arial Bold", 30))
label.place(x=50,y=30)

add_cards = Button(self.root, text="Add Cards", width=10)
add_cards.place(x=60,y=100)
add_cards.config(command=self.addCards)

review_cards = Button(self.root, text="Review", width=10)
review_cards.place(x=160,y=100)
review_cards.config(command=self.reviewCards)

quiz = Button(self.root, text="Quiz", width=10)
quiz.place(x=110,y=150)
quiz.config(command=self.quiz)

# left click event
def leftClick(self, event):
print("left")

# middle click event
def middleClick(self, event):
print("middle")

# right click event
def rightClick(self, event):
print("right")

# clears the screne for a different part of the application
# ex: home -> review
def changeScene(self):
for widget in self.root.winfo_children():
widget.destroy()

self.frame = Frame(self.root, width=300, height=250)
self.frame.pack()

# a screen to choose a category, which will then move to adding cards
# currently only the screen to choose a category is done, and buttons are not functional
# has a type in bar to create a new category and a scrollbar with existing categories
def addCards(self):
self.changeScene()

categories = list(self.flashcards.categories.keys())

label = Label(self.root, text="Create/Choose a Subject", font=("Arial Bold", 15))
label.place(x=35,y=30)

txt = Entry(self.root, width=20, textvariable=StringVar(self.root, "Enter new subject"))
txt.place(x=90,y=70)

add = Button(self.root, text="Add Subject", width=20)
add.place(x=80,y=120)
add.config(command=self.addCategory(txt.get()))

add_cards = Button(self.root, text="Choose Subject", width=20)
add_cards.place(x=80,y=170)

scrollbar = Scrollbar(self.root)
scrollbar.pack( side = RIGHT, fill = Y )

mylist = Listbox(self.root, yscrollcommand = scrollbar.set )
for subject in categories:
mylist.insert(END, str(subject))

mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )

# update flashcard library categories
def addCategory(self, category):
self.flashcards.add_category(category)
#self.flashcards.save()
#self.flashcards = self.flashcards.load()

# scene where card review takes place
# should display menu of categories
# when category is chosen, review occurs (use click events)
def reviewCards(self):
self.changeScene()
category_menu = OptionMenu(self.root, StringVar(self.root).set("Select a Category"), list(self.flashcards.categories.keys()))
category_menu.place(x=50,y=30)

# scene where multiple choice or matching quiz takes place
# should display menu of categories
# when category is chosen, quiz occurs
def quiz(self):
self.changeScene()

if __name__ == '__main__':
ui = FlashcardsUI()