count the number of answers needed for a question card

This commit is contained in:
berryman 2012-12-08 15:11:47 -05:00
parent 6006fc0dd0
commit eb2c66aa3f
2 changed files with 17 additions and 4 deletions

View File

@ -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

View File

@ -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