Spaces:
Sleeping
Sleeping
File size: 1,997 Bytes
53f9c43 a709932 53f9c43 2b130d1 53f9c43 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import csv
import os
import random
class QuestionLoaderLocal:
def __init__(self, file_path, question_count):
"""
Initializes the QuestionLoader with a base path for local files.
:param base_path: The base path where the question files are located.
"""
self.base_path = "candidate_assesment/data"
self.question_count = question_count
self.file_path = file_path
def fetch_questions(self):
"""
Fetches the questions for the given technology from the local file system.
:param technology: The technology (e.g., Python, Django) to fetch questions for.
:return: A list of dictionaries, where each dictionary represents a question.
:raises: Exception if the file cannot be fetched or read.
"""
# file_path = os.path.join(BASE_DIR, "questions", technology, "questions.csv")
if not os.path.exists(self.file_path):
return []
# raise FileNotFoundError(f"No questions found for technology")
try:
questions = []
# Read and parse the CSV file
with open(self.file_path, mode="r", encoding="utf-8") as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
questions.append({
"question": row["question"],
"option1": row["option1"],
"option2": row["option2"],
"option3": row["option3"],
"option4": row["option4"],
"answer": row["answer"],
"difficulty": row["difficulty"].lower()
})
# Randomly select 20 questions
sampled_questions = random.sample(questions, min(self.question_count, len(questions)))
return sampled_questions
except Exception as e:
raise RuntimeError(f"Failed to fetch questions: {str(e)}")
|