-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathcode_builder.py
More file actions
36 lines (24 loc) · 885 Bytes
/
code_builder.py
File metadata and controls
36 lines (24 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing_extensions import Self
class CodeBuilder:
def __init__(self):
self._code = ""
def append(self, *args: str) -> "Self":
for arg in args:
self._code += arg
return self
def indent(self):
return self.append(" ")
def newline(self) -> "Self":
return self.append("\n")
def append_list(self, args: list[str], sep: str = ",") -> "Self":
return self.append(sep.join(args))
def append_dict(self, args: dict[str, str], sep: str = ",") -> "Self":
return self.append_list([f"{k}={v}" for k, v in args.items()], sep)
def append_triple_quote(self) -> "Self":
return self.append('"""')
def append_repr(self, value) -> "Self":
return self.append(repr(value))
def build(self):
return self._code