-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_col_ops.py
302 lines (247 loc) · 8.98 KB
/
generate_col_ops.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from __future__ import annotations
import os
from collections.abc import Iterable
from types import NoneType
from pydiverse.transform._internal.ops import ops
from pydiverse.transform._internal.ops.op import Operator
from pydiverse.transform._internal.ops.signature import Signature
from pydiverse.transform._internal.tree.types import (
Dtype,
Tvar,
pdt_type_to_python,
)
COL_EXPR_PATH = "./src/pydiverse/transform/_internal/tree/col_expr.py"
FNS_PATH = "./src/pydiverse/transform/_internal/pipe/functions.py"
API_DOCS_PATH = "./docs/source/reference/operators/index.rst"
NAMESPACES = ["str", "dt", "dur"]
RVERSIONS = {
"__add__",
"__sub__",
"__mul__",
"__truediv__",
"__floordiv__",
"__pow__",
"__mod__",
"__and__",
"__or__",
"__xor__",
}
def add_vararg_star(formatted_args: str) -> str:
last_arg = "*" + formatted_args.split(", ")[-1]
return ", ".join(formatted_args.split(", ")[:-1] + [last_arg])
def type_annotation(dtype: Dtype, specialize_generic: bool) -> str:
if (not specialize_generic and not dtype.const) or isinstance(dtype, Tvar):
return "ColExpr"
if dtype.const:
python_type = pdt_type_to_python(dtype)
return python_type.__name__ if python_type is not NoneType else "None"
return f"ColExpr[{dtype.__class__.__name__}]"
def generate_fn_decl(
op: Operator, sig: Signature, *, name=None, specialize_generic: bool = True
) -> str:
if name is None:
name = op.name
defaults: Iterable = (
op.default_values
if op.default_values is not None
else (... for _ in op.param_names)
)
annotated_args = ", ".join(
name
+ ": "
+ type_annotation(dtype, specialize_generic)
+ (f" = {default_val}" if default_val is not ... else "")
for dtype, name, default_val in zip(
sig.types, op.param_names, defaults, strict=True
)
)
if sig.is_vararg:
annotated_args = add_vararg_star(annotated_args)
if len(op.context_kwargs) > 0:
context_kwarg_annotation = {
"partition_by": "Col | ColName | Iterable[Col | ColName]",
"arrange": "ColExpr | Iterable[ColExpr]",
"filter": "ColExpr[Bool] | Iterable[ColExpr[Bool]]",
}
annotated_kwargs = "".join(
f", {kwarg.name}: {context_kwarg_annotation[kwarg.name]}"
+ f"{'' if kwarg.required else ' | None = None'}"
for kwarg in op.context_kwargs
)
if len(sig.types) == 0 or not sig.is_vararg:
annotated_kwargs = "*" + annotated_kwargs
if len(sig.types) > 0:
annotated_kwargs = ", " + annotated_kwargs
else:
annotated_kwargs = ""
return (
f"def {name}({annotated_args}{annotated_kwargs}) "
f"-> {type_annotation(sig.return_type, specialize_generic)}:\n"
)
def generate_fn_body(
op: Operator,
sig: Signature,
param_names: list[str] | None = None,
*,
op_var_name: str,
rversion: bool = False,
):
if param_names is None:
param_names = op.param_names
if rversion:
assert len(param_names) == 2
assert not sig.is_vararg
param_names = list(reversed(param_names))
args = "".join(f", {name}" for name in param_names)
if sig.is_vararg:
args = add_vararg_star(args)
if op.context_kwargs is not None:
kwargs = "".join(f", {kwarg.name}={kwarg.name}" for kwarg in op.context_kwargs)
else:
kwargs = ""
return f" return ColFn(ops.{op_var_name}{args}{kwargs})\n\n"
def generate_overloads(
op: Operator, *, name: str | None = None, rversion: bool = False, op_var_name: str
):
res = ""
in_namespace = "." in op.name
if name is None:
name = op.name if not in_namespace else op.name.split(".")[1]
has_overloads = len(op.signatures) > 1
if has_overloads:
for sig in op.signatures:
res += "@overload\n" + generate_fn_decl(op, sig, name=name) + " ...\n\n"
res += (
generate_fn_decl(
op, op.signatures[0], name=name, specialize_generic=not has_overloads
)
+ f' """{op.doc}"""\n\n'
+ generate_fn_body(
op,
op.signatures[0],
["self.arg"] + op.param_names[1:] if in_namespace else None,
rversion=rversion,
op_var_name=op_var_name,
)
)
return res
def indent(s: str, by: int) -> str:
return "".join(" " * by + line + "\n" for line in s.split("\n"))
with open(COL_EXPR_PATH, "r+") as file:
new_file_contents = ""
in_generated_section = False
namespace_contents: dict[str, str] = {
name: (
f'@register_accessor("{name}")\n'
"@dataclasses.dataclass(slots=True)\n"
f"class {name.title()}Namespace(FnNamespace):\n"
)
for name in NAMESPACES
}
for line in file:
if not in_generated_section and line.startswith(
" # --- generated code starts here, do not delete this comment ---"
):
in_generated_section = True
new_file_contents += (
" # --- generated code starts here, do not delete this "
"comment ---\n\n"
)
elif in_generated_section and line.startswith(
"# --- generated code ends here, do not delete this comment ---"
):
for op_var_name in sorted(ops.__dict__):
op = ops.__dict__[op_var_name]
if not isinstance(op, Operator) or not op.generate_expr_method:
continue
op_overloads = generate_overloads(op, op_var_name=op_var_name)
if op.name in RVERSIONS:
op_overloads += generate_overloads(
op,
name=f"__r{op.name[2:]}",
rversion=True,
op_var_name=op_var_name,
)
op_overloads = indent(op_overloads, 4)
if "." in op.name:
namespace_contents[op.name.split(".")[0]] += op_overloads
else:
new_file_contents += op_overloads
for name in NAMESPACES:
new_file_contents += f" {name} : {name.title()}Namespace\n"
new_file_contents += (
"@dataclasses.dataclass(slots=True)\n"
"class FnNamespace:\n"
" arg: ColExpr\n"
)
for name in NAMESPACES:
new_file_contents += namespace_contents[name]
in_generated_section = False
if not in_generated_section:
new_file_contents += line
file.seek(0)
file.write(new_file_contents)
file.truncate()
os.system(f"ruff format {COL_EXPR_PATH}")
with open(FNS_PATH, "r+") as file:
new_file_contents = ""
for line in file:
new_file_contents += line
if line.startswith(
"# --- from here the code is generated, do not delete this comment ---"
):
new_file_contents += "\n"
for op_var_name in sorted(ops.__dict__):
op = ops.__dict__[op_var_name]
if isinstance(op, Operator) and not op.generate_expr_method:
new_file_contents += generate_overloads(op, op_var_name=op_var_name)
break
file.seek(0)
file.write(new_file_contents)
file.truncate()
os.system(f"ruff format {FNS_PATH}")
with open(API_DOCS_PATH, "r+") as file:
new_file_contents = ""
for line in file:
new_file_contents += line
if line.startswith("Expression methods"):
new_file_contents += (
"------------------\n\n"
".. currentmodule:: pydiverse.transform.ColExpr\n\n"
".. autosummary::\n"
" :nosignatures:\n\n "
)
new_file_contents += "\n ".join(
sorted(
[
op.name
for op in ops.__dict__.values()
if isinstance(op, Operator) and op.generate_expr_method
]
+ ["rank", "dense_rank", "map", "cast"]
)
)
new_file_contents += (
"\n\nGlobal functions\n"
"----------------\n\n"
".. currentmodule:: pydiverse.transform\n\n"
".. autosummary::\n"
" :nosignatures:\n\n "
)
new_file_contents += (
"\n ".join(
sorted(
[
op.name
for op in ops.__dict__.values()
if isinstance(op, Operator) and not op.generate_expr_method
]
+ ["when", "lit"]
)
)
+ "\n"
)
break
file.seek(0)
file.write(new_file_contents)
file.truncate()