diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2017-01-13 11:08:33 +0100 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2017-01-13 11:08:33 +0100 |
commit | d31f1337c252a65f3b33e7d74cdbda14547ae39f (patch) | |
tree | 2687c1cdec381c61277798a15ed558fbb8b5d1a4 /flashcards |
Initial commit
Diffstat (limited to 'flashcards')
-rwxr-xr-x | flashcards | 68 |
1 files changed, 68 insertions, 0 deletions
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() |