expr-codegen 0.5.2__tar.gz → 0.6.0__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.
Files changed (28) hide show
  1. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/PKG-INFO +1 -1
  2. expr_codegen-0.6.0/expr_codegen/_version.py +1 -0
  3. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/codes.py +14 -0
  4. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/pandas/code.py +8 -6
  5. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/pandas/template.py.j2 +1 -1
  6. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/polars/code.py +8 -6
  7. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/polars/template.py.j2 +1 -1
  8. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/tool.py +6 -2
  9. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen.egg-info/PKG-INFO +1 -1
  10. expr_codegen-0.5.2/expr_codegen/_version.py +0 -1
  11. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/LICENSE +0 -0
  12. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/README.md +0 -0
  13. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/__init__.py +0 -0
  14. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/dag.py +0 -0
  15. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/expr.py +0 -0
  16. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/latex/__init__.py +0 -0
  17. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/latex/printer.py +0 -0
  18. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/model.py +0 -0
  19. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/pandas/__init__.py +0 -0
  20. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/pandas/printer.py +0 -0
  21. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/polars/__init__.py +0 -0
  22. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen/polars/printer.py +0 -0
  23. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen.egg-info/SOURCES.txt +0 -0
  24. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen.egg-info/dependency_links.txt +0 -0
  25. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen.egg-info/requires.txt +0 -0
  26. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/expr_codegen.egg-info/top_level.txt +0 -0
  27. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/pyproject.toml +0 -0
  28. {expr_codegen-0.5.2 → expr_codegen-0.6.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: expr_codegen
3
- Version: 0.5.2
3
+ Version: 0.6.0
4
4
  Summary: symbol expression to polars expression tool
5
5
  Author-email: wukan <wu-kan@163.com>
6
6
  License: BSD 3-Clause License
@@ -0,0 +1 @@
1
+ __version__ = "0.6.0"
@@ -1,6 +1,8 @@
1
1
  import ast
2
2
  import re
3
3
 
4
+ from sympy import Add, Mul, Pow, Eq
5
+
4
6
  from expr_codegen.expr import register_symbols, dict_to_exprs
5
7
 
6
8
 
@@ -191,8 +193,20 @@ def raw_to_code(raw):
191
193
  return '\n'.join([ast.unparse(a) for a in raw])
192
194
 
193
195
 
196
+ def _add_default_type(globals_):
197
+ # 这种写法可以省去由用户导入Eq一类的工作
198
+ globals_['Add'] = Add
199
+ globals_['Mul'] = Mul
200
+ globals_['Pow'] = Pow
201
+ globals_['Eq'] = Eq
202
+ return globals_
203
+
204
+
194
205
  def sources_to_exprs(globals_, *sources, safe: bool = True):
195
206
  """将源代码转换成表达式"""
207
+
208
+ globals_ = _add_default_type(globals_)
209
+
196
210
  raw, assigns, funcs_new, args_new, targets_new = sources_to_asts(*sources)
197
211
  register_symbols(funcs_new, globals_, is_function=True)
198
212
  register_symbols(args_new, globals_, is_function=False)
@@ -1,5 +1,5 @@
1
1
  import os
2
- from typing import Sequence
2
+ from typing import Sequence, Dict
3
3
 
4
4
  import jinja2
5
5
  from jinja2 import FileSystemLoader, TemplateNotFound
@@ -27,9 +27,9 @@ def get_groupby_from_tuple(tup, func_name):
27
27
  return f'df = {func_name}(df)'
28
28
 
29
29
 
30
- def symbols_to_code(syms):
30
+ def symbols_to_code(syms, alias):
31
31
  a = [f"{s}" for s in syms]
32
- b = [f"'{s}'" for s in syms]
32
+ b = [f"r'{alias.get(s, s)}'" for s in syms]
33
33
  return f"""_ = ({','.join(b)},)
34
34
  ({','.join(a)},) = _"""
35
35
 
@@ -37,6 +37,7 @@ def symbols_to_code(syms):
37
37
  def codegen(exprs_ldl: ListDictList, exprs_src, syms_dst,
38
38
  filename='template.py.j2',
39
39
  date='date', asset='asset',
40
+ alias: Dict[str, str] = {},
40
41
  extra_codes: Sequence[str] = ()):
41
42
  """基于模板的代码生成"""
42
43
  # 打印Pandas风格代码
@@ -65,7 +66,8 @@ def codegen(exprs_ldl: ListDictList, exprs_src, syms_dst,
65
66
  va, ex = kv
66
67
  func_code.append(f" # {va} = {ex}\n df[{va}] = {p.doprint(ex)}")
67
68
  exprs_dst.append(f"{va} = {ex}")
68
- syms_out.append(va)
69
+ if va not in syms_dst:
70
+ syms_out.append(va)
69
71
 
70
72
  if k[0] == TS:
71
73
  groupbys['sort'] = f'df = df.sort_values(by=[_DATE_, _ASSET_]).reset_index(drop=True)'
@@ -77,8 +79,8 @@ def codegen(exprs_ldl: ListDictList, exprs_src, syms_dst,
77
79
  # 分组应用代码
78
80
  groupbys[func_name] = get_groupby_from_tuple(k, func_name)
79
81
 
80
- syms1 = symbols_to_code(syms_dst)
81
- syms2 = symbols_to_code(syms_out)
82
+ syms1 = symbols_to_code(syms_dst, alias)
83
+ syms2 = symbols_to_code(syms_out, alias)
82
84
 
83
85
  try:
84
86
  env = jinja2.Environment(loader=FileSystemLoader(os.path.dirname(__file__)))
@@ -49,7 +49,7 @@ def {{ key }}(df: pd.DataFrame) -> pd.DataFrame:
49
49
  """
50
50
 
51
51
 
52
- def main(df: pd.DataFrame):
52
+ def main(df: pd.DataFrame) -> pd.DataFrame:
53
53
  # logger.info("start...")
54
54
  {% for key, value in groupbys.items() %}
55
55
  {{ value-}}
@@ -1,5 +1,5 @@
1
1
  import os
2
- from typing import Sequence
2
+ from typing import Sequence, Dict
3
3
 
4
4
  import jinja2
5
5
  from jinja2 import FileSystemLoader, TemplateNotFound
@@ -27,9 +27,9 @@ def get_groupby_from_tuple(tup, func_name):
27
27
  return f'df = {func_name}(df)'
28
28
 
29
29
 
30
- def symbols_to_code(syms):
30
+ def symbols_to_code(syms, alias):
31
31
  a = [f"{s}" for s in syms]
32
- b = [f"'{s}'" for s in syms]
32
+ b = [f"r'{alias.get(s, s)}'" for s in syms]
33
33
  return f"""_ = ({','.join(b)},)
34
34
  ({','.join(a)},) = (pl.col(i) for i in _)"""
35
35
 
@@ -37,6 +37,7 @@ def symbols_to_code(syms):
37
37
  def codegen(exprs_ldl: ListDictList, exprs_src, syms_dst,
38
38
  filename='template.py.j2',
39
39
  date='date', asset='asset',
40
+ alias: Dict[str, str] = {},
40
41
  extra_codes: Sequence[str] = ()):
41
42
  """基于模板的代码生成"""
42
43
  # 打印Polars风格代码
@@ -73,7 +74,8 @@ def codegen(exprs_ldl: ListDictList, exprs_src, syms_dst,
73
74
 
74
75
  func_code.append(f"{va}={s2},")
75
76
  exprs_dst.append(f"{va} = {s1}")
76
- syms_out.append(va)
77
+ if va not in syms_dst:
78
+ syms_out.append(va)
77
79
  func_code.append(f" )")
78
80
  func_code = func_code[1:]
79
81
 
@@ -87,8 +89,8 @@ def codegen(exprs_ldl: ListDictList, exprs_src, syms_dst,
87
89
  # 分组应用代码
88
90
  groupbys[func_name] = get_groupby_from_tuple(k, func_name)
89
91
 
90
- syms1 = symbols_to_code(syms_dst)
91
- syms2 = symbols_to_code(syms_out)
92
+ syms1 = symbols_to_code(syms_dst, alias)
93
+ syms2 = symbols_to_code(syms_out, alias)
92
94
 
93
95
  try:
94
96
  env = jinja2.Environment(loader=FileSystemLoader(os.path.dirname(__file__)))
@@ -51,7 +51,7 @@ def {{ key }}(df: pl.DataFrame) -> pl.DataFrame:
51
51
  """
52
52
 
53
53
 
54
- def main(df: pl.DataFrame):
54
+ def main(df: pl.DataFrame) -> pl.DataFrame:
55
55
  # logger.info("start...")
56
56
  {% for key, value in groupbys.items() %}
57
57
  {{ value-}}
@@ -1,5 +1,5 @@
1
1
  import inspect
2
- from typing import Sequence
2
+ from typing import Sequence, Dict
3
3
 
4
4
  from black import Mode, format_str
5
5
  from sympy import simplify, cse, symbols, numbered_symbols
@@ -73,7 +73,7 @@ class ExprTool:
73
73
  exprs = exprs + list(kwargs.values())
74
74
 
75
75
  # print(exprs)
76
-
76
+ syms = [str(s) for s in syms]
77
77
  return exprs, syms
78
78
 
79
79
  def reduce(self, repl, redu):
@@ -141,6 +141,7 @@ class ExprTool:
141
141
  def all(self, exprs_src, style: str = 'polars', template_file: str = 'template.py.j2',
142
142
  replace: bool = True, regroup: bool = False, format: bool = True,
143
143
  date='date', asset='asset',
144
+ alias: Dict[str, str] = {},
144
145
  extra_codes: Sequence[object] = ()):
145
146
  """功能集成版,将几个功能写到一起方便使用
146
147
 
@@ -162,6 +163,8 @@ class ExprTool:
162
163
  日期字段名
163
164
  asset:str
164
165
  资产字段名
166
+ alias: Dict[str,str]
167
+ 符号别名。可以变通的传入正则符号名
165
168
  extra_codes: Sequence[object]
166
169
  需要复制到模板中的额外代码
167
170
 
@@ -195,6 +198,7 @@ class ExprTool:
195
198
 
196
199
  codes = codegen(exprs_ldl, exprs_src, syms_dst,
197
200
  filename=template_file, date=date, asset=asset,
201
+ alias=alias,
198
202
  extra_codes=extra_codes)
199
203
 
200
204
  if format:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: expr_codegen
3
- Version: 0.5.2
3
+ Version: 0.6.0
4
4
  Summary: symbol expression to polars expression tool
5
5
  Author-email: wukan <wu-kan@163.com>
6
6
  License: BSD 3-Clause License
@@ -1 +0,0 @@
1
- __version__ = "0.5.2"
File without changes
File without changes
File without changes