|
2 | 2 |
|
3 | 3 | import re |
4 | 4 |
|
5 | | -from .base import BaseLanguageInterface |
| 5 | +from .python import PythonLanguageInterface |
6 | 6 |
|
7 | 7 | FUNCTION_SIGNATURE_PATTERN = re.compile( |
8 | | - r"^(class Solution:\n)?\s*def (?P<name>\w+)\((?P<params>[^)]*)\) -> (?P<returnType>[^:]+):$", |
| 8 | + r"^\s*def (?P<name>\w+)\((?P<params>[^)]*)\) -> (?P<returnType>[^:]+):$", |
9 | 9 | flags=re.MULTILINE, |
10 | 10 | ) |
11 | 11 |
|
| 12 | + |
12 | 13 | # LeetCode uses Typing types, not Python 3.9+ types |
13 | 14 | TYPING_IMPORT_TEMPLATE = "from typing import *\n\n" |
14 | 15 |
|
15 | | -TEST_FILE_TEMPLATE = """\ |
16 | | -from solution import Solution |
17 | | -
|
18 | | -
|
19 | | -{supplemental_code}if __name__ == "__main__": |
20 | | - {params_setup} |
21 | | - result = Solution().{name}({params_call}) |
22 | | - print("{OUTPUT_RESULT_PREFIX}", result) |
23 | | -""" |
24 | 16 |
|
25 | | - |
26 | | -class Python3LanguageInterface(BaseLanguageInterface): |
| 17 | +class Python3LanguageInterface(PythonLanguageInterface): |
27 | 18 | """Implementation of the Python 3 language project template interface.""" |
28 | 19 |
|
29 | 20 | function_signature_pattern = FUNCTION_SIGNATURE_PATTERN |
30 | | - compile_command = None |
31 | | - test_command = ["python", "test.py"] |
32 | | - default_output = "None" |
33 | | - |
34 | | - def prepare_project_files(self, template: str): |
35 | | - params = ( |
36 | | - [ |
37 | | - param |
38 | | - for param in self.groups["params"].split(", ") |
39 | | - if param and param != "self" |
40 | | - ] |
41 | | - if self.groups["params"] |
42 | | - else [] |
43 | | - ) |
44 | | - self.groups["params_setup"] = "\n ".join( |
45 | | - param if "=" in param else f"{param} = None" for param in params |
46 | | - ) |
47 | | - self.groups["params_call"] = ", ".join( |
48 | | - param.split("=")[0].split(":")[0].strip() for param in params |
49 | | - ) |
50 | | - |
51 | | - supplemental_code = self.get_supplemental_code(template) |
52 | | - supplemental_code = ( |
53 | | - "" if supplemental_code is None else f"{supplemental_code}\n\n\n" |
54 | | - ) |
55 | | - self.groups["supplemental_code"] = supplemental_code |
| 21 | + |
| 22 | + def prepare_project_files(self, template): |
| 23 | + project_files = super().prepare_project_files(template) |
56 | 24 | return { |
57 | | - "solution.py": f"{TYPING_IMPORT_TEMPLATE}\n{supplemental_code}{template}pass\n", |
58 | | - "test.py": f"{TYPING_IMPORT_TEMPLATE}{TEST_FILE_TEMPLATE.format(**self.groups)}", |
| 25 | + "solution.py": f"{TYPING_IMPORT_TEMPLATE}\n{project_files['solution.py']}", |
| 26 | + "test.py": f"{TYPING_IMPORT_TEMPLATE}{project_files['test.py']}", |
59 | 27 | } |
0 commit comments