Skip to content

Commit 72ccbd7

Browse files
committed
refactoring and now allows setting name and description for the set
1 parent 14c8958 commit 72ccbd7

File tree

3 files changed

+54
-141
lines changed

3 files changed

+54
-141
lines changed

in2lambda/api/module.py

Lines changed: 0 additions & 125 deletions
This file was deleted.

in2lambda/api/set.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import panflute as pf
77

88
from in2lambda.api.question import Question
9-
from in2lambda.json_convert import json_convert
109

1110

1211
@dataclass
1312
class Set:
1413
"""Represents a list of questions."""
1514

15+
_name: str = field(default="set")
16+
_description: str = field(default="")
17+
1618
questions: list[Question] = field(default_factory=list)
1719
_current_question_index = -1
1820

@@ -122,4 +124,36 @@ def to_json(self, output_dir: str) -> None:
122124
Question 1's title: Question 1
123125
124126
"""
125-
json_convert.main(self.questions, output_dir)
127+
128+
from in2lambda.json_convert import json_convert
129+
json_convert.main(self, output_dir)
130+
131+
def set_name(self, name: str) -> None:
132+
"""Sets the name of the set.
133+
134+
Args:
135+
name: The name to set for the set.
136+
137+
Examples:
138+
>>> from in2lambda.api.set import Set
139+
>>> s = Set()
140+
>>> s.set_name("My Question Set")
141+
>>> s._name
142+
'My Question Set'
143+
"""
144+
self._name = name
145+
146+
def set_description(self, description: str) -> None:
147+
"""Sets the description of the set.
148+
149+
Args:
150+
description: The description to set for the set.
151+
152+
Examples:
153+
>>> from in2lambda.api.set import Set
154+
>>> s = Set()
155+
>>> s.set_description("This is my question set.")
156+
>>> s._description
157+
'This is my question set.'
158+
"""
159+
self._description = description

in2lambda/json_convert/json_convert.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,53 @@
88
from pathlib import Path
99
from typing import Any
1010

11+
from in2lambda.api.set import Set
1112
from in2lambda.api.question import Question
1213

1314
MINIMAL_QUESTION_TEMPLATE = "minimal_template_question.json"
1415
MINIMAL_SET_TEMPLATE = "minimal_template_set.json"
1516

1617

17-
def zip_sorted_folder(folder_path, zip_path):
18+
def _zip_sorted_folder(folder_path, zip_path):
1819
"""Zips the contents of a folder, preserving the directory structure.
1920
Args:
2021
folder_path: The path to the folder to zip.
2122
zip_path: The path where the zip file will be created.
2223
"""
2324
with zipfile.ZipFile(zip_path, 'w') as zf:
2425
for root, dirs, files in os.walk(folder_path):
25-
# Sort files for deterministic order
26+
# Sort files for deterministic, alphabetical order
2627
for file in sorted(files):
2728
abs_path = os.path.join(root, file)
2829
rel_path = os.path.relpath(abs_path, folder_path)
2930
zf.write(abs_path, arcname=rel_path)
3031

3132
def converter(
32-
question_template: dict[str, Any], set_template: dict[str, Any], ListQuestions: list[Question], output_dir: str
33+
question_template: dict[str, Any], set_template: dict[str, Any], SetQuestions: Set, output_dir: str
3334
) -> None:
34-
"""Turns a list of question objects into Lambda Feedback JSON.
35+
"""Turns a set of question objects into Lambda Feedback JSON.
3536
3637
Args:
3738
question_template: The loaded JSON from the minimal question template (it needs to be in sync).
3839
set_template: The loaded JSON from the minimal set template (it needs to be in sync).
39-
ListQuestions: A list of question objects.
40+
SetQuestions: A Set object containing questions.
4041
output_dir: The absolute path for where to produced the final JSON/zip files.
4142
"""
42-
# Create output by copying template
43+
ListQuestions = SetQuestions.questions
44+
set_name = SetQuestions._name
45+
set_description = SetQuestions._description
4346

4447
# create directory to put the questions
4548
os.makedirs(output_dir, exist_ok=True)
46-
output_question = os.path.join(output_dir, "set_a_simple_example")
49+
output_question = os.path.join(output_dir, set_name)
4750
os.makedirs(output_question, exist_ok=True)
4851

52+
53+
set_template["name"] = set_name
54+
set_template["description"] = set_description
4955
# create the set file
50-
with open(f"{output_question}/set_a_simple_example.json", "w") as file:
56+
with open(f"{output_question}/set_{set_name}.json", "w") as file:
5157
json.dump(set_template, file)
52-
shutil.make_archive(output_question, "zip", output_question)
53-
5458

5559
for i in range(len(ListQuestions)):
5660
output = deepcopy(question_template)
@@ -97,16 +101,16 @@ def converter(
97101
shutil.copy(image_path, output_image) # copies image into the directory
98102

99103
# output zip file in destination folder
100-
zip_sorted_folder(output_question, output_question + ".zip")
104+
_zip_sorted_folder(output_question, output_question + ".zip")
101105

102106

103-
def main(questions: list[Question], output_dir: str) -> None:
107+
def main(set_questions: Set, output_dir: str) -> None:
104108
"""Preliminary defensive programming before calling the main converter function.
105109
106110
This ultimately then produces the Lambda Feedback JSON/ZIP files.
107111
108112
Args:
109-
questions: A list of question objects.
113+
set_questions: A Set object containing questions.
110114
output_dir: Where to output the final Lambda Feedback JSON/ZIP files.
111115
"""
112116
# Use path so minimal template can be found regardless of where the user is running python from.
@@ -122,4 +126,4 @@ def main(questions: list[Question], output_dir: str) -> None:
122126
shutil.rmtree(output_dir)
123127
except OSError as e:
124128
print("Error: %s : %s" % (output_dir, e.strerror))
125-
converter(question_template, set_template, questions, output_dir)
129+
converter(question_template, set_template, set_questions, output_dir)

0 commit comments

Comments
 (0)