iQuentin commited on
Commit
0339155
·
verified ·
1 Parent(s): 8a758b0

Create answer_data_manager.py

Browse files
Files changed (1) hide show
  1. answer_data_manager.py +152 -0
answer_data_manager.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from typing import List, Dict, Any, Optional
4
+
5
+
6
+ class AnswerDataManager:
7
+ """
8
+ A class to handle saving and reading already answered task data to/from a JSON file to avoid calling costly agent twice.
9
+ """
10
+
11
+ def __init__(self, filename: str = "already_answered.json"):
12
+ """
13
+ Initialize the AnswerDataManager.
14
+
15
+ Args:
16
+ filename (str): The name of the JSON file to use for storage.
17
+ """
18
+ self.filename = filename
19
+ self.data: List[Dict[str, Any]] = []
20
+
21
+ def load_data(self) -> List[Dict[str, Any]]:
22
+ """
23
+ Load data from the JSON file.
24
+
25
+ Returns:
26
+ List[Dict[str, Any]]: The loaded data, or empty list if file doesn't exist.
27
+ """
28
+ try:
29
+ if os.path.exists(self.filename):
30
+ with open(self.filename, 'r', encoding='utf-8') as f:
31
+ self.data = json.load(f)
32
+ else:
33
+ self.data = []
34
+ except (json.JSONDecodeError, IOError) as e:
35
+ print(f"Error loading data from {self.filename}: {e}")
36
+ self.data = []
37
+
38
+ return self.data
39
+
40
+ def save_data(self, data: Optional[List[Dict[str, Any]]] = None) -> bool:
41
+ """
42
+ Save data to the JSON file.
43
+
44
+ Args:
45
+ data (Optional[List[Dict[str, Any]]]): Data to save. If None, saves current self.data.
46
+
47
+ Returns:
48
+ bool: True if successful, False otherwise.
49
+ """
50
+ if data is not None:
51
+ self.data = data
52
+
53
+ try:
54
+ with open(self.filename, 'w', encoding='utf-8') as f:
55
+ json.dump(self.data, f, indent=4, ensure_ascii=False)
56
+ return True
57
+ except IOError as e:
58
+ print(f"Error saving data to {self.filename}: {e}")
59
+ return False
60
+
61
+ def add_answer(self, task_id: str, question: str, submitted_answer: str) -> bool:
62
+ """
63
+ Add a new answer to the data structure.
64
+
65
+ Args:
66
+ task_id (str): The task identifier.
67
+ question (str): The question text.
68
+ submitted_answer (str): The submitted answer.
69
+
70
+ Returns:
71
+ bool: True if successful, False otherwise.
72
+ """
73
+ new_answer = {
74
+ "task_id": task_id,
75
+ "question": question,
76
+ "submitted_answer": submitted_answer
77
+ }
78
+
79
+ self.data.append(new_answer)
80
+ return self.save_data()
81
+
82
+ def get_answer_by_task_id(self, task_id: str) -> Optional[Dict[str, Any]]:
83
+ """
84
+ Retrieve an answer by task ID.
85
+
86
+ Args:
87
+ task_id (str): The task identifier to search for.
88
+
89
+ Returns:
90
+ Optional[Dict[str, Any]]: The answer data if found, None otherwise.
91
+ """
92
+ for answer in self.data:
93
+ if answer.get("task_id") == task_id:
94
+ return answer
95
+ return None
96
+
97
+ def update_answer(self, task_id: str, question: str = None, submitted_answer: str = None) -> bool:
98
+ """
99
+ Update an existing answer by task ID.
100
+
101
+ Args:
102
+ task_id (str): The task identifier.
103
+ question (str, optional): New question text.
104
+ submitted_answer (str, optional): New submitted answer.
105
+
106
+ Returns:
107
+ bool: True if successful, False if task_id not found.
108
+ """
109
+ for answer in self.data:
110
+ if answer.get("task_id") == task_id:
111
+ if question is not None:
112
+ answer["question"] = question
113
+ if submitted_answer is not None:
114
+ answer["submitted_answer"] = submitted_answer
115
+ return self.save_data()
116
+ return False
117
+
118
+ def remove_answer(self, task_id: str) -> bool:
119
+ """
120
+ Remove an answer by task ID.
121
+
122
+ Args:
123
+ task_id (str): The task identifier to remove.
124
+
125
+ Returns:
126
+ bool: True if successful, False if task_id not found.
127
+ """
128
+ original_length = len(self.data)
129
+ self.data = [answer for answer in self.data if answer.get("task_id") != task_id]
130
+
131
+ if len(self.data) < original_length:
132
+ return self.save_data()
133
+ return False
134
+
135
+ def get_all_answers(self) -> List[Dict[str, Any]]:
136
+ """
137
+ Get all answers in the current data structure.
138
+
139
+ Returns:
140
+ List[Dict[str, Any]]: All answer data.
141
+ """
142
+ return self.data.copy()
143
+
144
+ def clear_all_data(self) -> bool:
145
+ """
146
+ Clear all data from memory and file.
147
+
148
+ Returns:
149
+ bool: True if successful, False otherwise.
150
+ """
151
+ self.data = []
152
+ return self.save_data()