Skip to content

Commit 41d8be2

Browse files
committed
feat(api): add to_json method to module
Also includes formatting changes from black’s version bump
1 parent 7f2efbf commit 41d8be2

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

in2lambda/api/module.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import panflute as pf
77

88
from in2lambda.api.question import Question
9+
from in2lambda.json_convert import json_convert
910

1011

1112
@dataclass
@@ -89,3 +90,33 @@ def increment_current_question(self) -> None:
8990
Question(title='Question 2', parts=[Part(text='', worked_solution='Question 2 answer')], images=[], _main_text='')]
9091
"""
9192
self._current_question_index += 1
93+
94+
def to_json(self, output_dir: str) -> None:
95+
"""Turns this module into Lambda Feedback JSON/ZIP files.
96+
97+
WARNING: This will overwrite any existing files in the directory.
98+
99+
Args:
100+
output_dir: Where to output the final Lambda Feedback JSON/ZIP files.
101+
102+
Examples:
103+
>>> import tempfile
104+
>>> import os
105+
>>> import json
106+
>>> # Create a module with two questions
107+
>>> module = Module()
108+
>>> module.add_question("Question 1")
109+
>>> module.add_question("Question 2")
110+
>>> with tempfile.TemporaryDirectory() as temp_dir:
111+
... # Write the JSON files to the temporary directory
112+
... module.to_json(temp_dir)
113+
... # Check the contents of the directory
114+
... sorted(os.listdir(temp_dir))
115+
... # Check the title of the first question
116+
... with open(f"{temp_dir}/Question 1/Question 1.json") as file:
117+
... print(f"Question 1's title: {json.load(file)['title']}")
118+
['Question 1', 'Question 1.zip', 'Question 2', 'Question 2.zip']
119+
Question 1's title: Question 1
120+
121+
"""
122+
json_convert.main(self.questions, output_dir)

in2lambda/filters/PartsSepSol/filter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def pandoc_filter(
7272
# Add each part solution/text
7373
# For the solution, prepend any top level answer text to each part answer
7474
for part in lettered_parts:
75-
module.current_question.add_solution(
76-
spaced_blurb + part
77-
) if parsing_answers else module.current_question.add_part_text(part)
75+
(
76+
module.current_question.add_solution(spaced_blurb + part)
77+
if parsing_answers
78+
else module.current_question.add_part_text(part)
79+
)
7880
return None

in2lambda/filters/markdown.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def filter(
120120
[pf.Element, pf.elements.Doc, Module, bool],
121121
Optional[pf.Str],
122122
]
123-
) -> Callable[[pf.Element, pf.elements.Doc, Module, str, bool], Optional[pf.Str],]:
123+
) -> Callable[
124+
[pf.Element, pf.elements.Doc, Module, str, bool],
125+
Optional[pf.Str],
126+
]:
124127
"""Python decorator to make generic LaTeX elements markdown readable.
125128
126129
As an example, part of the process involves putting dollar signs around maths

in2lambda/main.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import in2lambda.filters
1111
from in2lambda.api.module import Module
12-
from in2lambda.json_convert import json_convert
1312

1413

1514
def file_type(file: str) -> str:
@@ -40,7 +39,17 @@ def file_type(file: str) -> str:
4039
match (extension := file.split(".")[-1].lower()):
4140
case "tex" | "latex" | "ltx":
4241
return "latex"
43-
case "md" | "rmd" | "markdown" | "mdown" | "mdwn" | "mkd" | "mkdn" | "text" | "txt":
42+
case (
43+
"md"
44+
| "rmd"
45+
| "markdown"
46+
| "mdown"
47+
| "mdwn"
48+
| "mkd"
49+
| "mkdn"
50+
| "text"
51+
| "txt"
52+
):
4453
return "markdown"
4554
case "docx":
4655
return "docx" # Pandoc doesn't seem to support doc
@@ -112,7 +121,7 @@ def runner(
112121

113122
# Read the Python API format and convert to JSON.
114123
if output_dir is not None:
115-
json_convert.main(module.questions, output_dir)
124+
module.to_json(output_dir)
116125

117126
return module
118127

0 commit comments

Comments
 (0)