expr-codegen 0.6.2__tar.gz → 0.6.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/PKG-INFO +1 -1
- expr_codegen-0.6.4/expr_codegen/_version.py +1 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/codes.py +4 -1
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/tool.py +59 -1
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen.egg-info/PKG-INFO +1 -1
- expr_codegen-0.6.2/expr_codegen/_version.py +0 -1
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/LICENSE +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/README.md +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/__init__.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/dag.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/expr.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/latex/__init__.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/latex/printer.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/model.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/pandas/__init__.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/pandas/code.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/pandas/printer.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/pandas/template.py.j2 +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/polars/__init__.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/polars/code.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/polars/printer.py +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen/polars/template.py.j2 +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen.egg-info/SOURCES.txt +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen.egg-info/dependency_links.txt +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen.egg-info/requires.txt +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/expr_codegen.egg-info/top_level.txt +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/pyproject.toml +0 -0
- {expr_codegen-0.6.2 → expr_codegen-0.6.4}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.6.4"
|
|
@@ -60,6 +60,10 @@ class SympyTransformer(ast.NodeTransformer):
|
|
|
60
60
|
self.args_map[old_target_id] = new_target_id
|
|
61
61
|
|
|
62
62
|
def visit_Assign(self, node):
|
|
63
|
+
# 调整位置,支持循环赋值
|
|
64
|
+
# _A = _A+1 调整成 _A_001 = _A_000 + 1
|
|
65
|
+
self.generic_visit(node)
|
|
66
|
+
|
|
63
67
|
# 提取输出变量名
|
|
64
68
|
for target in node.targets:
|
|
65
69
|
if isinstance(target, ast.Tuple):
|
|
@@ -74,7 +78,6 @@ class SympyTransformer(ast.NodeTransformer):
|
|
|
74
78
|
node.value.id = self.args_map.get(node.value.id, node.value.id)
|
|
75
79
|
self.args_new.add(node.value.id)
|
|
76
80
|
|
|
77
|
-
self.generic_visit(node)
|
|
78
81
|
return node
|
|
79
82
|
|
|
80
83
|
def visit_Compare(self, node):
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import inspect
|
|
2
|
-
from
|
|
2
|
+
from functools import lru_cache
|
|
3
|
+
from typing import Sequence, Dict, Optional
|
|
3
4
|
|
|
4
5
|
from black import Mode, format_str
|
|
5
6
|
from sympy import simplify, cse, symbols, numbered_symbols
|
|
6
7
|
|
|
8
|
+
from expr_codegen.codes import sources_to_exprs
|
|
7
9
|
from expr_codegen.expr import get_current_by_prefix, get_children, replace_exprs
|
|
8
10
|
from expr_codegen.model import dag_start, dag_end, dag_middle
|
|
9
11
|
|
|
@@ -15,6 +17,7 @@ class ExprTool:
|
|
|
15
17
|
self.get_current_func_kwargs = {}
|
|
16
18
|
self.exprs_dict = {}
|
|
17
19
|
self.exprs_names = []
|
|
20
|
+
self.globals_ = {}
|
|
18
21
|
|
|
19
22
|
def set_current(self, func, **kwargs):
|
|
20
23
|
self.get_current_func = func
|
|
@@ -206,3 +209,58 @@ class ExprTool:
|
|
|
206
209
|
codes = format_str(codes, mode=Mode(line_length=600, magic_trailing_comma=True))
|
|
207
210
|
|
|
208
211
|
return codes, G
|
|
212
|
+
|
|
213
|
+
def exec(self, codes: str, df_input):
|
|
214
|
+
"""执行代码
|
|
215
|
+
|
|
216
|
+
Notes
|
|
217
|
+
-----
|
|
218
|
+
注意生成的代码已经约定输入用df_input,输出用df_output
|
|
219
|
+
|
|
220
|
+
"""
|
|
221
|
+
globals_ = {'df_input': df_input}
|
|
222
|
+
exec(codes, globals_)
|
|
223
|
+
return globals_['df_output']
|
|
224
|
+
|
|
225
|
+
@lru_cache(maxsize=64)
|
|
226
|
+
def _get_codes(self, source: str, extra_codes: str, output_file: str) -> str:
|
|
227
|
+
"""通过字符串生成代码, 加了缓存,多次调用不重复生成"""
|
|
228
|
+
raw, exprs_dict = sources_to_exprs(self.globals_, source, safe=False)
|
|
229
|
+
|
|
230
|
+
# 生成代码
|
|
231
|
+
codes, G = _TOOL_.all(exprs_dict, style='polars', template_file='template.py.j2',
|
|
232
|
+
replace=True, regroup=True, format=True,
|
|
233
|
+
date='date', asset='asset',
|
|
234
|
+
# 复制了需要使用的函数,还复制了最原始的表达式
|
|
235
|
+
extra_codes=(raw,
|
|
236
|
+
# 传入多个列的方法
|
|
237
|
+
extra_codes,
|
|
238
|
+
))
|
|
239
|
+
|
|
240
|
+
if output_file is not None:
|
|
241
|
+
with open(output_file, 'w', encoding='utf-8') as f:
|
|
242
|
+
f.write(codes)
|
|
243
|
+
|
|
244
|
+
return codes
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
_TOOL_ = ExprTool()
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def codegen_exec(code_block, df_input,
|
|
251
|
+
extra_codes: str = r'CS_SW_L1 = pl.col(r"^sw_l1_\d+$")',
|
|
252
|
+
output_file: Optional[str] = None):
|
|
253
|
+
"""快速转换源代码并执行"""
|
|
254
|
+
# 此代码来自于sympy.var
|
|
255
|
+
frame = inspect.currentframe().f_back
|
|
256
|
+
_TOOL_.globals_ = frame.f_globals.copy()
|
|
257
|
+
del frame
|
|
258
|
+
|
|
259
|
+
if isinstance(code_block, str):
|
|
260
|
+
source = code_block
|
|
261
|
+
else:
|
|
262
|
+
source = inspect.getsource(code_block)
|
|
263
|
+
|
|
264
|
+
codes = _TOOL_._get_codes(source, extra_codes, output_file)
|
|
265
|
+
|
|
266
|
+
return _TOOL_.exec(codes, df_input)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.6.2"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|