From eb2c66aa3fbba81af37fd769f0b81fedfb048eb2 Mon Sep 17 00:00:00 2001 From: berryman Date: Sat, 8 Dec 2012 15:11:47 -0500 Subject: [PATCH] count the number of answers needed for a question card --- plugin.py | 19 +++++++++++++++---- test.py | 2 ++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/plugin.py b/plugin.py index 83781e8..41f7112 100644 --- a/plugin.py +++ b/plugin.py @@ -25,7 +25,6 @@ class Deck(object): if os.path.exists(path): with open(path) as file_handle: file_data = file_handle.readlines() - card_text_list.extend(file_data) if len(card_text_list) is 0: raise IOError @@ -33,13 +32,25 @@ class Deck(object): # Deduplicate the text from the cards card_text_list = list(set(card_text_list)) + + # Turn the strings of text into a Card object card_object_list = [] for index, card in enumerate(card_text_list): - card_object_list.append(Card(index, card_type, card)) + # Figure out how many answers are required for a question card + if card_type == 'question': + answers = self.count_answers(card) + card_object_list.append(Card(index, card_type, card, answers=answers)) + else: + card_object_list.append(Card(index, card_type, card)) return card_object_list - + def count_answers(self, text, blank_format = '__________'): + blanks = text.count(blank_format) + if blanks is 0: + return 1 + else: + return blanks def drawCard(self, typeOfCard): typeMap = {'answer': self.answerDb, 'question': self.questionDb} @@ -49,7 +60,7 @@ class Deck(object): return card class Card(object): - def __init__(self, id, type, text): + def __init__(self, id, type, text, answers=None, author=None): self.id = id self.type = type self.text = text diff --git a/test.py b/test.py index a0400c6..99e5727 100644 --- a/test.py +++ b/test.py @@ -19,3 +19,5 @@ def test_card_parsing(): assert type(card.type) is str assert card.type in ['answer', 'question'] assert type(card.text) is str + if card.type is 'question': + assert type(card.answers) is int