import csv
class Question(object):
def __init__(self, question, answer):
self.question = question.strip()
self.answer = answer.strip().lower()
def __eq__(self, other):
if not isinstance(other, Question):
return False
return self.question == other.question and self.answer == other.answer
def __neq__(self, other):
return not self == other
def validate_questions(questions):
all_qs = [q.question for q in questions]
seen = set()
duplicates = []
for q in all_qs:
if q in seen:
duplicates.append(q)
seen.add(q)
if duplicates:
raise ValueError("duplicate questions found: {}".format(duplicates))
def load_io(f):
reader = csv.reader(f)
questions = []
for row in reader:
if len(row) == 0 or (len(row) == 1 and not row[0].strip()):
continue
if len(row) != 2:
raise ValueError("row is invalid length of {}: {}".format(
len(row), row))
questions.append(Question(*row))
return questions
def load_path(path):
with open(path, 'rb') as f:
return load_io(f)