Spaces:
Sleeping
Sleeping
| 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)}") | |