Skip to content

Commit 70354e7

Browse files
committed
included minimal json template for set in json converter
1 parent 85fdc12 commit 70354e7

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

in2lambda/json_convert/json_convert.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99

1010
from in2lambda.api.question import Question
1111

12-
MINIMAL_TEMPLATE = "minimal_template_question.json"
12+
MINIMAL_QUESTION_TEMPLATE = "minimal_template_question.json"
13+
MINIMAL_SET_TEMPLATE = "minimal_template_set.json"
1314

1415

1516
def converter(
16-
template: dict[str, Any], ListQuestions: list[Question], output_dir: str
17+
question_template: dict[str, Any], set_template: dict[str, Any], ListQuestions: list[Question], output_dir: str
1718
) -> None:
1819
"""Turns a list of question objects into Lambda Feedback JSON.
1920
2021
Args:
21-
template: The loaded JSON from the minimal template (it needs to be in sync).
22+
question_template: The loaded JSON from the minimal question template (it needs to be in sync).
23+
set_template: The loaded JSON from the minimal set template (it needs to be in sync).
2224
ListQuestions: A list of question objects.
2325
output_dir: The absolute path for where to produced the final JSON/zip files.
2426
"""
@@ -33,8 +35,14 @@ def converter(
3335
output_image = os.path.join(output_question, "media")
3436
os.makedirs(output_image, exist_ok=True)
3537

38+
# create the set file
39+
with open(f"{output_question}/set_a_simple_example.json", "w") as file:
40+
json.dump(set_template, file)
41+
shutil.make_archive(output_question, "zip", output_question)
42+
43+
3644
for i in range(len(ListQuestions)):
37-
output = deepcopy(template)
45+
output = deepcopy(question_template)
3846

3947
output["orderNumber"] = i # order number starts at 0
4048
# add title to the question file
@@ -53,15 +61,15 @@ def converter(
5361
ListQuestions[i].parts[0].worked_solution
5462
)
5563
for j in range(1, len(ListQuestions[i].parts)):
56-
output["parts"].append(deepcopy(template["parts"][0]))
64+
output["parts"].append(deepcopy(question_template["parts"][0]))
5765
output["parts"][j]["content"] = ListQuestions[i].parts[j].text
5866
output["parts"][j]["orderNumber"] = j
5967
output["parts"][j]["workedSolution"]["content"] = (
6068
ListQuestions[i].parts[j].worked_solution
6169
)
6270

6371
# Output file
64-
filename = "question_" + str(i + 1)
72+
filename = "question_" + str(i).zfill(3) + "_" + output['title'].replace(" ", "_")
6573

6674
# write questions into directory
6775
with open(f"{output_question}/{filename}.json", "w") as file:
@@ -74,8 +82,8 @@ def converter(
7482
) # converts computer path into python path
7583
shutil.copy(image_path, output_image) # copies image into the directory
7684

77-
# output zip file in destination folder
78-
shutil.make_archive(output_question, "zip", output_question)
85+
# output zip file in destination folder
86+
shutil.make_archive(output_question, "zip", output_question)
7987

8088

8189
def main(questions: list[Question], output_dir: str) -> None:
@@ -88,13 +96,16 @@ def main(questions: list[Question], output_dir: str) -> None:
8896
output_dir: Where to output the final Lambda Feedback JSON/ZIP files.
8997
"""
9098
# Use path so minimal template can be found regardless of where the user is running python from.
91-
with open(Path(__file__).with_name(MINIMAL_TEMPLATE), "r") as file:
92-
template = json.load(file)
99+
with open(Path(__file__).with_name(MINIMAL_QUESTION_TEMPLATE), "r") as file:
100+
question_template = json.load(file)
101+
102+
with open(Path(__file__).with_name(MINIMAL_SET_TEMPLATE), "r") as file:
103+
set_template = json.load(file)
93104

94105
# check if directory exists in file
95106
if os.path.isdir(output_dir):
96107
try:
97108
shutil.rmtree(output_dir)
98109
except OSError as e:
99110
print("Error: %s : %s" % (output_dir, e.strerror))
100-
converter(template, questions, output_dir)
111+
converter(question_template, set_template, questions, output_dir)
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
{
22
"orderNumber": 0,
3+
"title": "Question title here",
4+
"masterContent": "Top level question here",
5+
"publish": false,
36
"displayFinalAnswer": true,
47
"displayStructuredTutorial": true,
58
"displayWorkedSolution": true,
6-
"masterContent": "Top level question here",
9+
"displayChatbot": false,
710
"parts": [
811
{
9-
"answer": "",
10-
"content": "Part text here",
1112
"orderNumber": 0,
13+
"content": "Part text here",
1214
"responseAreas": [],
13-
"tutorial": [],
14-
"universalPartId": "N/A",
1515
"workedSolution": {
16+
"title": "",
1617
"content": "Part worked solution here",
17-
"id": "N/A",
18-
"title": ""
18+
"children": []
1919
}
2020
}
21-
],
22-
"publish": false,
23-
"title": "Question title here"
21+
]
2422
}

0 commit comments

Comments
 (0)