Skip to content

Commit

Permalink
feat: break long strings in _PUZZLE
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Nov 20, 2024
1 parent e97b9b0 commit 017d90a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 15 additions & 3 deletions puzzle_generator/create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,26 @@ def _advertisement() -> str:
"""


def _split_long_str(in_str: str, max_len: int) -> typing.List[str]:
if len(in_str) < max_len:
return [in_str]
return [in_str[:max_len]] + _split_long_str(in_str[max_len:], max_len)


def _str_to_code(in_str: str, max_len: int, quotes: str) -> str:
return "\n".join(quotes + _ + quotes for _ in _split_long_str(in_str, max_len))


def _create_str(in_encrypted_puzzle, configurator) -> str:
modules: str = "\n".join("import " + _ for _ in configurator.get_modules()) + "\n"
objects: str = "\n".join(
inspect.getsource(_) for _ in configurator.get_needed_objects()
)
question = in_encrypted_puzzle[0]
rest_str = bytestr_utils.bytes_to_bytestr(in_encrypted_puzzle[1])
puzzle_data: str = f'_PUZZLE = ("""{question}""", bytestr_to_bytes("{rest_str}"))'
question = _str_to_code(in_encrypted_puzzle[0], 78, '"""')
rest_str = _str_to_code(
bytestr_utils.bytes_to_bytestr(in_encrypted_puzzle[1]), 78, '"'
)
puzzle_data: str = f"_PUZZLE = ({question}, bytestr_to_bytes({rest_str}))"
call: str = "run_puzzle(_PUZZLE, _DECRYPT, input)"

return (
Expand Down
16 changes: 11 additions & 5 deletions tests/test_create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ def _positive_puzzle_tc(qa_list: typing.List[str]) -> _PuzzleTestCase:
"Congratulations!",
]

_MULTILINE_QUESTION = (
"Question 1❓\n"
"-very-long-line----------------------------------------------------"
"-------------------------------------------------------------------"
"------------------------------------------------end-of-long-line-\n"
"With several lines!\n"
)

_QA_LIST_2 = [
"Question 1❓\n-------\nWith several lines!\n",
_MULTILINE_QUESTION,
"1",
"Q2?",
"A2",
Expand All @@ -72,10 +79,8 @@ def _positive_puzzle_tc(qa_list: typing.List[str]) -> _PuzzleTestCase:
_PuzzleTestCase(
qa_list=_QA_LIST_2,
input=["Wrong!"],
output="""Question 1❓
-------
With several lines!
output=_MULTILINE_QUESTION
+ """
This is a wrong answer. Try again!
""",
),
Expand Down Expand Up @@ -111,6 +116,7 @@ def _run_puzzle_str(
f"puzzle-generator {importlib.metadata.version('puzzle-generator')}"
in in_puzzle
)
assert all(len(_) <= 88 for _ in in_puzzle.splitlines())
with open(in_puzzle_path, "w", encoding="utf-8") as puzzle_file:
puzzle_file.write(in_puzzle)
return _run_puzzle_file(in_puzzle_path, answers)
Expand Down

0 comments on commit 017d90a

Please sign in to comment.