From d31f1337c252a65f3b33e7d74cdbda14547ae39f Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 13 Jan 2017 11:08:33 +0100 Subject: Initial commit --- flashcards | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 flashcards diff --git a/flashcards b/flashcards new file mode 100755 index 0000000..caf51d8 --- /dev/null +++ b/flashcards @@ -0,0 +1,68 @@ +#!/usr/bin/python3 + +import re +import sys +from pathlib import Path +from random import randint + +cardRegex = "CARD: " +prefixLength = 6 + +# Returns a list of Path objects, containing the path to each valid file provided +def getFileList(): + fileList = [] + if len(sys.argv) > 1: + for f in sys.argv[1:]: + path = Path(f) + if path.exists() and path.is_file(): + fileList.append(f) + return fileList + else: + print("Missing arguments") + sys.exit() + +# Returns cards in the form [(front, back)] +def createCardList(files): + cards = [] + for f in files: + cards = cards + extractCards(f) + return cards + +# Extracts cards from a single file into a list of the form [(front, back)] +def extractCards(f): + front = "" + back = "" + cards = [] + with open(f) as cardFile: + for l in cardFile: + match = re.match(cardRegex, l) + if match: + if front != "": + cards.append([front, back]) + back = "" + front = match.string[prefixLength:] + else: + back += l + return cards + +# Loops serving cards to the user until the program is exited +def serveCards(cards): + while True: + card = cards[randint(0, len(cards) - 1)] + print(card[0]) + input() + print(card[1]) + input() + +def debugCards(cardList): + for c in cardList: + print("Front:", c[0]) + print("Back:", c[1]) + +def main(): + files = getFileList() + cards = createCardList(files) + serveCards(cards) + +if __name__ == "__main__": + main() -- cgit v1.2.3