From 04d682582da7de8d38390798b2a368fb64c8564c Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Sat, 27 Jul 2024 23:31:29 +0200 Subject: [PATCH] style: add some type hints --- puzzle_generator/create_puzzle.py | 36 +++++++++++----------- puzzle_generator/puzzle_data_encryption.py | 13 +++++--- tests/test_create_puzzle.py | 12 ++++---- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/puzzle_generator/create_puzzle.py b/puzzle_generator/create_puzzle.py index 902f1b6..bfb611b 100644 --- a/puzzle_generator/create_puzzle.py +++ b/puzzle_generator/create_puzzle.py @@ -23,26 +23,26 @@ def run_puzzle(in_puzzle, in_decrypt, get_answer): run_puzzle(new_puzzle, in_decrypt, get_answer) -def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants_str: str) -> str: +def _create_str(in_modules, in_objects, in_encrypted_puzzle, constants: str) -> str: advertisement = """# generated with puzzle-generator # # https://pypi.org/project/puzzle-generator/ # https://github.com/vil02/puzzle_generator/ """ - modules_str = "\n".join("import " + _ for _ in in_modules) + "\n" - objects_str = "\n".join(inspect.getsource(_) for _ in in_objects) - puzzle_data_str = f"_PUZZLE = {in_encrypted_puzzle}" - call_str = "run_puzzle(_PUZZLE, _DECRYPT, input)" + modules: str = "\n".join("import " + _ for _ in in_modules) + "\n" + objects: str = "\n".join(inspect.getsource(_) for _ in in_objects) + puzzle_data: str = f"_PUZZLE = {in_encrypted_puzzle}" + call: str = "run_puzzle(_PUZZLE, _DECRYPT, input)" return ( "\n".join( [ advertisement, - modules_str, - objects_str, - puzzle_data_str, - constants_str, - call_str, + modules, + objects, + puzzle_data, + constants, + call, ] ) + "\n" @@ -95,8 +95,8 @@ def _list_of_bytes_to_str(in_list: typing.List[bytes]) -> str: def _list_of_bytes_to_codestr(in_list: typing.List[bytes]) -> str: - raw_str = _list_of_bytes_to_str(in_list) - return f"[bytestr_to_bytes(_) for _ in {raw_str}]" + raw: str = _list_of_bytes_to_str(in_list) + return f"[bytestr_to_bytes(_) for _ in {raw}]" class SpicedSimpleEncryptionConfigurator: @@ -129,20 +129,20 @@ def get_needed_objects(self): def get_constants_str( self, ) -> str: - proc_spices_str = ( + proc_spices: str = ( f"_PROC_SPICES = {_list_of_bytes_to_codestr(self._proc_spices)}" ) - signature_spices_str = ( + signature_spices: str = ( f"_SIGNATURE_SPICES = {_list_of_bytes_to_codestr(self._signature_spices)}" ) - decrypt_str = ( + decrypt: str = ( "_DECRYPT = get_decrypt(" f"{_get_hasher_name(self._proc_hasher)}, " f"{_get_hasher_name(self._signature_hasher)}, " "_PROC_SPICES, " "_SIGNATURE_SPICES)" ) - return "\n".join([proc_spices_str, signature_spices_str, decrypt_str]) + return "\n".join([proc_spices, signature_spices, decrypt]) def _get_configurator(**kwargs): @@ -161,6 +161,6 @@ def create(in_puzzle, **kwargs) -> str: needed_modules = ["hashlib", "itertools", "base64", "json", "sys", "typing"] needed_objects = configurator.get_needed_objects() - constants_str = configurator.get_constants_str() + constants: str = configurator.get_constants_str() - return _create_str(needed_modules, needed_objects, encrypted_puzzle, constants_str) + return _create_str(needed_modules, needed_objects, encrypted_puzzle, constants) diff --git a/puzzle_generator/puzzle_data_encryption.py b/puzzle_generator/puzzle_data_encryption.py index 2222b6d..e82fbe6 100644 --- a/puzzle_generator/puzzle_data_encryption.py +++ b/puzzle_generator/puzzle_data_encryption.py @@ -1,15 +1,18 @@ import json +import typing -def encrypt_data(in_data, in_encrypt): +def encrypt_data(in_data, in_encrypt: typing.Callable[[str, str], str]): if list(in_data.keys()) == ["str"]: return {"str": in_data["str"]} - rest_str = json.dumps(encrypt_data(in_data["rest"], in_encrypt)) - encrypted_str = in_encrypt(rest_str, in_data["pass"]) - return {"str": in_data["str"], "rest": encrypted_str} + rest: str = json.dumps(encrypt_data(in_data["rest"], in_encrypt)) + encrypted: str = in_encrypt(rest, in_data["pass"]) + return {"str": in_data["str"], "rest": encrypted} -def decrypt_data(in_rest: str, in_pass: str, in_decrypt): +def decrypt_data( + in_rest: str, in_pass: str, in_decrypt: typing.Callable[[str, str], str | None] +): rest_str = in_decrypt(in_rest, in_pass) if rest_str is None: return None diff --git a/tests/test_create_puzzle.py b/tests/test_create_puzzle.py index adf5830..13e53c9 100644 --- a/tests/test_create_puzzle.py +++ b/tests/test_create_puzzle.py @@ -74,10 +74,10 @@ def _run_puzzle_file( def _run_puzzle_str( - in_puzzle_str: str, answers: list[str], in_puzzle_path: pathlib.Path + in_puzzle: str, answers: list[str], in_puzzle_path: pathlib.Path ) -> subprocess.CompletedProcess[str]: with open(in_puzzle_path, "w", encoding="utf-8") as puzzle_file: - puzzle_file.write(in_puzzle_str) + puzzle_file.write(in_puzzle) return _run_puzzle_file(in_puzzle_path, answers) @@ -109,8 +109,8 @@ def test_all_good_answers( puzzle_path: pathlib.Path, configuration, ) -> None: - puzzle_str = cp.create(puzzle_tc.puzzle, **configuration) - res = _run_puzzle_str(puzzle_str, puzzle_tc.correct.input, puzzle_path) + puzzle: str = cp.create(puzzle_tc.puzzle, **configuration) + res = _run_puzzle_str(puzzle, puzzle_tc.correct.input, puzzle_path) assert res.returncode == 0 assert res.stdout == puzzle_tc.correct.output @@ -124,8 +124,8 @@ def test_wrong_answers( configuration, ) -> None: for cur_wrong in puzzle_tc.wrong: - puzzle_str = cp.create(puzzle_tc.puzzle, **configuration) - res = _run_puzzle_str(puzzle_str, cur_wrong.input, puzzle_path) + puzzle: str = cp.create(puzzle_tc.puzzle, **configuration) + res = _run_puzzle_str(puzzle, cur_wrong.input, puzzle_path) assert res.returncode == 1 assert res.stdout == cur_wrong.output assert not res.stderr