Skip to content

Commit 0e60b62

Browse files
committed
add python2 interface
1 parent 8f5bc2d commit 0e60b62

File tree

5 files changed

+69
-43
lines changed

5 files changed

+69
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ python -m lpg --help
7070

7171
Planned available languages:
7272

73-
- [ ] python
73+
- [x] python
7474
- [x] python3 (thanks [@dancikmad](https://github.com/dancikmad))
7575
- [ ] javascript
7676
- [ ] typescript

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "leetcode-project-generator"
9-
version = "1.3.0"
9+
version = "1.3.1"
1010
authors = [
1111
{ name = "Konrad Guzek", email = "[email protected]" },
1212
]

src/lpg/interfaces/file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
from .lang.base import BaseLanguageInterface
88
from .lang.c import CLanguageInterface
9+
from .lang.python import PythonLanguageInterface
910
from .lang.python3 import Python3LanguageInterface
1011

1112
LANGUAGE_INTERFACES: dict[str, BaseLanguageInterface] = {
1213
"c": CLanguageInterface(),
14+
"python": PythonLanguageInterface(),
1315
"python3": Python3LanguageInterface(),
1416
}
1517

src/lpg/interfaces/lang/python.py

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

src/lpg/interfaces/lang/python3.py

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,26 @@
22

33
import re
44

5-
from .base import BaseLanguageInterface
5+
from .python import PythonLanguageInterface
66

77
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>[^:]+):$",
99
flags=re.MULTILINE,
1010
)
1111

12+
1213
# LeetCode uses Typing types, not Python 3.9+ types
1314
TYPING_IMPORT_TEMPLATE = "from typing import *\n\n"
1415

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-
"""
2416

25-
26-
class Python3LanguageInterface(BaseLanguageInterface):
17+
class Python3LanguageInterface(PythonLanguageInterface):
2718
"""Implementation of the Python 3 language project template interface."""
2819

2920
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)
5624
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']}",
5927
}

0 commit comments

Comments
 (0)