expr-codegen 0.13.3__tar.gz → 0.13.5__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 (29) hide show
  1. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/PKG-INFO +1 -1
  2. expr_codegen-0.13.5/expr_codegen/_version.py +1 -0
  3. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/tool.py +19 -8
  4. expr_codegen-0.13.3/expr_codegen/_version.py +0 -1
  5. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/.gitignore +0 -0
  6. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/LICENSE +0 -0
  7. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/README.md +0 -0
  8. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/__init__.py +0 -0
  9. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/codes.py +0 -0
  10. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/dag.py +0 -0
  11. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/expr.py +0 -0
  12. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/latex/__init__.py +0 -0
  13. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/latex/printer.py +0 -0
  14. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/model.py +0 -0
  15. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/pandas/__init__.py +0 -0
  16. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/pandas/code.py +0 -0
  17. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/pandas/helper.py +0 -0
  18. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/pandas/printer.py +0 -0
  19. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/pandas/ta.py +0 -0
  20. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/pandas/template.py.j2 +0 -0
  21. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/polars/__init__.py +0 -0
  22. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/polars/code.py +0 -0
  23. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/polars/printer.py +0 -0
  24. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/polars/template.py.j2 +0 -0
  25. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/sql/__init__.py +0 -0
  26. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/sql/code.py +0 -0
  27. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/sql/printer.py +0 -0
  28. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/expr_codegen/sql/template.sql.j2 +0 -0
  29. {expr_codegen-0.13.3 → expr_codegen-0.13.5}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: expr_codegen
3
- Version: 0.13.3
3
+ Version: 0.13.5
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.13.5"
@@ -10,6 +10,7 @@ from loguru import logger
10
10
  from sympy import simplify, cse, symbols, numbered_symbols
11
11
  from sympy.core.expr import Expr
12
12
  from sympy.logic import boolalg
13
+ from sympy.simplify import cse_opts
13
14
 
14
15
  from expr_codegen.codes import sources_to_exprs
15
16
  from expr_codegen.expr import get_current_by_prefix, get_children, replace_exprs
@@ -48,12 +49,14 @@ Expr.diff = _diff
48
49
 
49
50
  # ===============================
50
51
 
51
- def simplify2(expr):
52
- # return simplify(expr)
52
+ def simplify2(expr, skip_simplify: bool):
53
+ # OPEN/OPEN会被简化成1,遗传算法中常出现,可以跳过简化
54
+ if skip_simplify:
55
+ return expr
53
56
  try:
54
57
  expr = simplify(expr)
55
- except AttributeError as e:
56
- print(f'{expr} ,表达式无法简化, {e}')
58
+ except (AttributeError, TypeError) as e:
59
+ logger.warning('{}, 表达式无法简化, {}', expr, repr(e))
57
60
  return expr
58
61
 
59
62
 
@@ -93,7 +96,7 @@ class ExprTool:
93
96
  # print(exprs)
94
97
  return exprs, syms
95
98
 
96
- def merge(self, date, asset, args):
99
+ def merge(self, date, asset, args, skip_simplify):
97
100
  """合并多个表达式
98
101
 
99
102
  1. 先抽取分割子公式
@@ -109,7 +112,7 @@ class ExprTool:
109
112
  表达式列表
110
113
  """
111
114
  # 抽取前先化简
112
- args = [(k, simplify2(v), c) for k, v, c in args]
115
+ args = [(k, simplify2(v, skip_simplify), c) for k, v, c in args]
113
116
 
114
117
  # 保留了注释信息
115
118
  exprs_syms = [(self.extract(v, date, asset), c) for k, v, c in args]
@@ -172,7 +175,7 @@ class ExprTool:
172
175
  _exprs = [k for k, v in exprs]
173
176
 
174
177
  # 注意:对于表达式右边相同,左边不同的情况,会当成一个处理
175
- repl, redu = cse(_exprs, symbols_repl, optimizations="basic")
178
+ repl, redu = cse(_exprs, symbols_repl, optimizations=[(cse_opts.sub_pre, cse_opts.sub_post), ])
176
179
  outputs_len = len(exprs_src)
177
180
 
178
181
  new_redu = []
@@ -205,6 +208,7 @@ class ExprTool:
205
208
  over_null: Literal['order_by', 'partition_by', None] = 'partition_by',
206
209
  table_name: str = 'self',
207
210
  filter_last: bool = False,
211
+ skip_simplify: bool = False,
208
212
  **kwargs):
209
213
  """功能集成版,将几个功能写到一起方便使用
210
214
 
@@ -230,6 +234,7 @@ class ExprTool:
230
234
  需要复制到模板中的额外代码
231
235
  table_name
232
236
  filter_last
237
+ skip_simplify
233
238
 
234
239
  Returns
235
240
  -------
@@ -242,7 +247,7 @@ class ExprTool:
242
247
  exprs_src = replace_exprs(exprs_src)
243
248
 
244
249
  # 子表达式在前,原表式在最后
245
- exprs_dst, syms_dst = self.merge(date, asset, exprs_src)
250
+ exprs_dst, syms_dst = self.merge(date, asset, exprs_src, skip_simplify)
246
251
  syms_dst = list(set(syms_dst) - _RESERVED_WORD_)
247
252
 
248
253
  # 提取公共表达式
@@ -293,6 +298,7 @@ class ExprTool:
293
298
  over_null: Literal['order_by', 'partition_by', None] = 'partition_by',
294
299
  table_name: str = 'self',
295
300
  filter_last: bool = False,
301
+ skip_simplify: bool = False,
296
302
  **kwargs) -> str:
297
303
  """通过字符串生成代码, 加了缓存,多次调用不重复生成"""
298
304
  raw, exprs_list = sources_to_exprs(self.globals_, source, *more_sources, convert_xor=convert_xor)
@@ -309,6 +315,7 @@ class ExprTool:
309
315
  over_null=over_null,
310
316
  table_name=table_name,
311
317
  filter_last=filter_last,
318
+ skip_simplify=skip_simplify,
312
319
  **kwargs)
313
320
 
314
321
  # 移回到cache,防止多次调用多次保存
@@ -372,6 +379,7 @@ def codegen_exec(df: Union[DataFrame, None],
372
379
  date: str = 'date', asset: str = 'asset',
373
380
  table_name: str = 'self',
374
381
  filter_last: bool = False,
382
+ skip_simplify: bool = False,
375
383
  **kwargs) -> Union[DataFrame, str]:
376
384
  """快速转换源代码并执行
377
385
 
@@ -413,6 +421,8 @@ def codegen_exec(df: Union[DataFrame, None],
413
421
  表名。只在style参数为sql时有效
414
422
  filter_last:bool
415
423
  在实盘时,只需要最后一天日期的数据,可以在最后一个`ts`之后过滤数据。目前只在style参数为'polars', 'pandas'时有效
424
+ skip_simplify:bool
425
+ 遗传算法时很有可能出现OPEN/OPEN,可以跳过化简步骤
416
426
 
417
427
 
418
428
  Returns
@@ -467,6 +477,7 @@ def codegen_exec(df: Union[DataFrame, None],
467
477
  over_null=over_null,
468
478
  table_name=table_name,
469
479
  filter_last=filter_last,
480
+ skip_simplify=skip_simplify,
470
481
  **kwargs
471
482
  )
472
483
 
@@ -1 +0,0 @@
1
- __version__ = "0.13.3"
File without changes
File without changes
File without changes