onetick-py 1.162.2__py3-none-any.whl

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 (152) hide show
  1. locator_parser/__init__.py +0 -0
  2. locator_parser/acl.py +73 -0
  3. locator_parser/actions.py +266 -0
  4. locator_parser/common.py +365 -0
  5. locator_parser/io.py +41 -0
  6. locator_parser/locator.py +150 -0
  7. onetick/__init__.py +101 -0
  8. onetick/doc_utilities/__init__.py +3 -0
  9. onetick/doc_utilities/napoleon.py +40 -0
  10. onetick/doc_utilities/ot_doctest.py +140 -0
  11. onetick/doc_utilities/snippets.py +280 -0
  12. onetick/lib/__init__.py +4 -0
  13. onetick/lib/instance.py +138 -0
  14. onetick/py/__init__.py +290 -0
  15. onetick/py/_stack_info.py +89 -0
  16. onetick/py/_version.py +2 -0
  17. onetick/py/aggregations/__init__.py +11 -0
  18. onetick/py/aggregations/_base.py +645 -0
  19. onetick/py/aggregations/_docs.py +912 -0
  20. onetick/py/aggregations/compute.py +286 -0
  21. onetick/py/aggregations/functions.py +2216 -0
  22. onetick/py/aggregations/generic.py +104 -0
  23. onetick/py/aggregations/high_low.py +80 -0
  24. onetick/py/aggregations/num_distinct.py +83 -0
  25. onetick/py/aggregations/order_book.py +427 -0
  26. onetick/py/aggregations/other.py +1014 -0
  27. onetick/py/backports.py +26 -0
  28. onetick/py/cache.py +373 -0
  29. onetick/py/callback/__init__.py +5 -0
  30. onetick/py/callback/callback.py +275 -0
  31. onetick/py/callback/callbacks.py +131 -0
  32. onetick/py/compatibility.py +752 -0
  33. onetick/py/configuration.py +736 -0
  34. onetick/py/core/__init__.py +0 -0
  35. onetick/py/core/_csv_inspector.py +93 -0
  36. onetick/py/core/_internal/__init__.py +0 -0
  37. onetick/py/core/_internal/_manually_bound_value.py +6 -0
  38. onetick/py/core/_internal/_nodes_history.py +250 -0
  39. onetick/py/core/_internal/_op_utils/__init__.py +0 -0
  40. onetick/py/core/_internal/_op_utils/every_operand.py +9 -0
  41. onetick/py/core/_internal/_op_utils/is_const.py +10 -0
  42. onetick/py/core/_internal/_per_tick_scripts/tick_list_sort_template.script +121 -0
  43. onetick/py/core/_internal/_proxy_node.py +140 -0
  44. onetick/py/core/_internal/_state_objects.py +2307 -0
  45. onetick/py/core/_internal/_state_vars.py +87 -0
  46. onetick/py/core/_source/__init__.py +0 -0
  47. onetick/py/core/_source/_symbol_param.py +95 -0
  48. onetick/py/core/_source/schema.py +97 -0
  49. onetick/py/core/_source/source_methods/__init__.py +0 -0
  50. onetick/py/core/_source/source_methods/aggregations.py +810 -0
  51. onetick/py/core/_source/source_methods/applyers.py +296 -0
  52. onetick/py/core/_source/source_methods/columns.py +141 -0
  53. onetick/py/core/_source/source_methods/data_quality.py +301 -0
  54. onetick/py/core/_source/source_methods/debugs.py +270 -0
  55. onetick/py/core/_source/source_methods/drops.py +120 -0
  56. onetick/py/core/_source/source_methods/fields.py +619 -0
  57. onetick/py/core/_source/source_methods/filters.py +1001 -0
  58. onetick/py/core/_source/source_methods/joins.py +1393 -0
  59. onetick/py/core/_source/source_methods/merges.py +566 -0
  60. onetick/py/core/_source/source_methods/misc.py +1325 -0
  61. onetick/py/core/_source/source_methods/pandases.py +155 -0
  62. onetick/py/core/_source/source_methods/renames.py +356 -0
  63. onetick/py/core/_source/source_methods/sorts.py +183 -0
  64. onetick/py/core/_source/source_methods/switches.py +142 -0
  65. onetick/py/core/_source/source_methods/symbols.py +117 -0
  66. onetick/py/core/_source/source_methods/times.py +627 -0
  67. onetick/py/core/_source/source_methods/writes.py +702 -0
  68. onetick/py/core/_source/symbol.py +202 -0
  69. onetick/py/core/_source/tmp_otq.py +222 -0
  70. onetick/py/core/column.py +209 -0
  71. onetick/py/core/column_operations/__init__.py +0 -0
  72. onetick/py/core/column_operations/_methods/__init__.py +4 -0
  73. onetick/py/core/column_operations/_methods/_internal.py +28 -0
  74. onetick/py/core/column_operations/_methods/conversions.py +215 -0
  75. onetick/py/core/column_operations/_methods/methods.py +294 -0
  76. onetick/py/core/column_operations/_methods/op_types.py +150 -0
  77. onetick/py/core/column_operations/accessors/__init__.py +0 -0
  78. onetick/py/core/column_operations/accessors/_accessor.py +30 -0
  79. onetick/py/core/column_operations/accessors/decimal_accessor.py +92 -0
  80. onetick/py/core/column_operations/accessors/dt_accessor.py +464 -0
  81. onetick/py/core/column_operations/accessors/float_accessor.py +160 -0
  82. onetick/py/core/column_operations/accessors/str_accessor.py +1374 -0
  83. onetick/py/core/column_operations/base.py +1061 -0
  84. onetick/py/core/cut_builder.py +149 -0
  85. onetick/py/core/db_constants.py +20 -0
  86. onetick/py/core/eval_query.py +244 -0
  87. onetick/py/core/lambda_object.py +442 -0
  88. onetick/py/core/multi_output_source.py +193 -0
  89. onetick/py/core/per_tick_script.py +2253 -0
  90. onetick/py/core/query_inspector.py +465 -0
  91. onetick/py/core/source.py +1663 -0
  92. onetick/py/db/__init__.py +2 -0
  93. onetick/py/db/_inspection.py +1042 -0
  94. onetick/py/db/db.py +1423 -0
  95. onetick/py/db/utils.py +64 -0
  96. onetick/py/docs/__init__.py +0 -0
  97. onetick/py/docs/docstring_parser.py +112 -0
  98. onetick/py/docs/utils.py +81 -0
  99. onetick/py/functions.py +2354 -0
  100. onetick/py/license.py +188 -0
  101. onetick/py/log.py +88 -0
  102. onetick/py/math.py +947 -0
  103. onetick/py/misc.py +437 -0
  104. onetick/py/oqd/__init__.py +22 -0
  105. onetick/py/oqd/eps.py +1195 -0
  106. onetick/py/oqd/sources.py +325 -0
  107. onetick/py/otq.py +211 -0
  108. onetick/py/pyomd_mock.py +47 -0
  109. onetick/py/run.py +841 -0
  110. onetick/py/servers.py +173 -0
  111. onetick/py/session.py +1342 -0
  112. onetick/py/sources/__init__.py +19 -0
  113. onetick/py/sources/cache.py +167 -0
  114. onetick/py/sources/common.py +126 -0
  115. onetick/py/sources/csv.py +642 -0
  116. onetick/py/sources/custom.py +85 -0
  117. onetick/py/sources/data_file.py +305 -0
  118. onetick/py/sources/data_source.py +1049 -0
  119. onetick/py/sources/empty.py +94 -0
  120. onetick/py/sources/odbc.py +337 -0
  121. onetick/py/sources/order_book.py +238 -0
  122. onetick/py/sources/parquet.py +168 -0
  123. onetick/py/sources/pit.py +191 -0
  124. onetick/py/sources/query.py +495 -0
  125. onetick/py/sources/snapshots.py +419 -0
  126. onetick/py/sources/split_query_output_by_symbol.py +198 -0
  127. onetick/py/sources/symbology_mapping.py +123 -0
  128. onetick/py/sources/symbols.py +357 -0
  129. onetick/py/sources/ticks.py +825 -0
  130. onetick/py/sql.py +70 -0
  131. onetick/py/state.py +256 -0
  132. onetick/py/types.py +2056 -0
  133. onetick/py/utils/__init__.py +70 -0
  134. onetick/py/utils/acl.py +93 -0
  135. onetick/py/utils/config.py +186 -0
  136. onetick/py/utils/default.py +49 -0
  137. onetick/py/utils/file.py +38 -0
  138. onetick/py/utils/helpers.py +76 -0
  139. onetick/py/utils/locator.py +94 -0
  140. onetick/py/utils/perf.py +499 -0
  141. onetick/py/utils/query.py +49 -0
  142. onetick/py/utils/render.py +1139 -0
  143. onetick/py/utils/script.py +244 -0
  144. onetick/py/utils/temp.py +471 -0
  145. onetick/py/utils/types.py +118 -0
  146. onetick/py/utils/tz.py +82 -0
  147. onetick_py-1.162.2.dist-info/METADATA +148 -0
  148. onetick_py-1.162.2.dist-info/RECORD +152 -0
  149. onetick_py-1.162.2.dist-info/WHEEL +5 -0
  150. onetick_py-1.162.2.dist-info/entry_points.txt +2 -0
  151. onetick_py-1.162.2.dist-info/licenses/LICENSE +21 -0
  152. onetick_py-1.162.2.dist-info/top_level.txt +2 -0
@@ -0,0 +1,2253 @@
1
+ import ast
2
+ import inspect
3
+ import textwrap
4
+ import types
5
+ import operator
6
+ import tokenize
7
+ import warnings
8
+ from typing import Callable, Union, Any, Optional, Iterable, Type, Tuple, Dict, List, TypeVar
9
+ from copy import deepcopy
10
+ from functools import wraps
11
+
12
+ from onetick.py.backports import astunparse, cached_property
13
+
14
+ from collections import deque
15
+ from contextlib import contextmanager
16
+
17
+ from .. import types as ott
18
+ from .column_operations.base import _Operation
19
+ from .column import _Column
20
+ from .lambda_object import _EmulateObject, _LambdaIfElse, _EmulateStateVars, _CompareTrackScope
21
+ from ._internal._state_objects import (
22
+ _TickSequence, _TickSequenceTickBase, _TickListTick, _TickSetTick, _TickDequeTick, _DynamicTick
23
+ )
24
+
25
+
26
+ class Static:
27
+ """
28
+ Class for declaring static local variable in per-tick script.
29
+ Static variables are defined once and save their values between
30
+ arrival of the input ticks.
31
+ """
32
+ def __init__(self, value):
33
+ self.value = value
34
+
35
+ # these functions needed mostly for linters
36
+ def __getattr__(self, item):
37
+ return self.value.__getattr__(item)
38
+
39
+ def __getitem__(self, item):
40
+ return operator.getitem(self.value, item)
41
+
42
+ def __setitem__(self, key, value):
43
+ return operator.setitem(self.value, key, value)
44
+
45
+
46
+ class LocalVariable(_Operation):
47
+ """
48
+ Class for inner representation of local variable in per-tick script.
49
+ Only simple values are supported, tick sequences are represented by another class.
50
+ """
51
+ def __init__(self, name, dtype=None):
52
+ super().__init__(op_str=f'LOCAL::{name}', dtype=dtype)
53
+ self.name = name
54
+
55
+
56
+ class TickDescriptorFields(_TickSequence):
57
+ """
58
+ Class for declaring tick descriptor fields in per-tick script.
59
+ Can only be iterated, doesn't have methods and parameters.
60
+
61
+ See also
62
+ --------
63
+ :py:class:`TickDescriptorField <onetick.py.core.per_tick_script.TickDescriptorField>`
64
+
65
+ Examples
66
+ --------
67
+ >>> t = otp.Tick(A=1)
68
+ >>> def fun(tick):
69
+ ... for field in otp.tick_descriptor_fields():
70
+ ... tick['NAME'] = field.get_name()
71
+ >>> t = t.script(fun)
72
+ >>> otp.run(t)
73
+ Time A NAME
74
+ 0 2003-12-01 1 A
75
+ """
76
+ def __init__(self):
77
+ # we don't want to inherit parent class init
78
+ pass
79
+
80
+ def __str__(self):
81
+ return 'LOCAL::INPUT_TICK_DESCRIPTOR_FIELDS'
82
+
83
+ @property
84
+ def _tick_class(self):
85
+ return TickDescriptorField
86
+
87
+
88
+ def tick_list_tick():
89
+ """
90
+ Can be used only in per-tick script function
91
+ to define a tick list tick local variable.
92
+
93
+ Tick list ticks can be used with some methods
94
+ of tick lists :py:class:`onetick.py.state.tick_list`.
95
+
96
+ See also
97
+ --------
98
+ :py:class:`onetick.py.state.tick_list`.
99
+
100
+ Returns
101
+ -------
102
+ :py:class:`onetick.py.static` value with tick object.
103
+
104
+ Examples
105
+ --------
106
+ >>> def fun(tick):
107
+ ... t = otp.tick_list_tick()
108
+ ... tick.state_vars['LIST'].push_back(t)
109
+ """
110
+ return Static(_TickListTick(None))
111
+
112
+
113
+ def tick_set_tick():
114
+ """
115
+ Can be used only in per-tick script function
116
+ to define a tick set tick local variable.
117
+
118
+ Tick set ticks can be used with some methods
119
+ of tick sets :py:class:`onetick.py.state.tick_set`.
120
+
121
+ See also
122
+ --------
123
+ :py:class:`onetick.py.state.tick_set`.
124
+
125
+ Returns
126
+ -------
127
+ :py:class:`onetick.py.static` value with tick object.
128
+
129
+ Examples
130
+ --------
131
+ >>> def fun(tick):
132
+ ... t = otp.tick_set_tick()
133
+ ... if tick.state_vars['SET'].find(t, -1):
134
+ ... tick['RES'] = '-1'
135
+ """
136
+ return Static(_TickSetTick(None))
137
+
138
+
139
+ def tick_deque_tick():
140
+ """
141
+ Can be used only in per-tick script function
142
+ to define a tick deque tick local variable.
143
+
144
+ Tick deque ticks can be used with some methods
145
+ of tick deques :py:class:`onetick.py.state.tick_deque`.
146
+
147
+ See also
148
+ --------
149
+ :py:class:`onetick.py.state.tick_deque`.
150
+
151
+ Returns
152
+ -------
153
+ :py:class:`onetick.py.static` value with tick object.
154
+
155
+ Examples
156
+ --------
157
+ >>> def fun(tick):
158
+ ... t = otp.tick_deque_tick()
159
+ ... tick.state_vars['DEQUE'].get_tick(0, t)
160
+ """
161
+ return Static(_TickDequeTick(None))
162
+
163
+
164
+ def dynamic_tick():
165
+ """
166
+ Can be used only in per-tick script function
167
+ to define a dynamic tick local variable.
168
+
169
+ Dynamic ticks can be used with some methods
170
+ of all tick sequences.
171
+
172
+ See also
173
+ --------
174
+ :py:class:`onetick.py.state.tick_list`
175
+ :py:class:`onetick.py.state.tick_set`
176
+ :py:class:`onetick.py.state.tick_deque`
177
+
178
+ Returns
179
+ -------
180
+ :py:class:`onetick.py.static` value with tick object.
181
+
182
+ Examples
183
+ --------
184
+ >>> def fun(tick):
185
+ ... t = otp.dynamic_tick()
186
+ ... t['X'] = tick['SUM']
187
+ """
188
+ return Static(_DynamicTick(None))
189
+
190
+
191
+ class TickDescriptorField(_TickSequenceTickBase):
192
+ """
193
+ Tick descriptor field object.
194
+ Can be accessed only while iterating over
195
+ :py:class:`otp.tick_descriptor_fields <onetick.py.core.per_tick_script.TickDescriptorFields>`
196
+ in per-tick script.
197
+
198
+ Examples
199
+ --------
200
+ >>> t = otp.Tick(A=2, B='B', C=1.2345)
201
+ >>> def fun(tick):
202
+ ... tick['NAMES'] = ''
203
+ ... tick['TYPES'] = ''
204
+ ... tick['SIZES'] = ''
205
+ ... for field in otp.tick_descriptor_fields():
206
+ ... tick['NAMES'] += field.get_name() + ','
207
+ ... tick['TYPES'] += field.get_type() + ','
208
+ ... tick['SIZES'] += field.get_size().apply(str) + ','
209
+ >>> t = t.script(fun)
210
+ >>> otp.run(t)
211
+ Time A B C NAMES TYPES SIZES
212
+ 0 2003-12-01 2 B 1.2345 A,B,C, long,string,double, 8,64,8,
213
+ """
214
+
215
+ _definition = 'TICK_DESCRIPTOR_FIELD'
216
+
217
+ def get_field_name(self):
218
+ """
219
+ Get the name of the field.
220
+
221
+ Returns
222
+ -------
223
+ onetick.py.Operation
224
+ """
225
+ return _Operation(op_str=f'{self}.GET_FIELD_NAME()', dtype=str)
226
+
227
+ def get_name(self):
228
+ """
229
+ Get the name of the field.
230
+
231
+ Returns
232
+ -------
233
+ onetick.py.Operation
234
+ """
235
+ return self.get_field_name()
236
+
237
+ def get_size(self):
238
+ """
239
+ Get the size of the type of the field.
240
+
241
+ Returns
242
+ -------
243
+ onetick.py.Operation
244
+ """
245
+ return _Operation(op_str=f'{self}.GET_SIZE()', dtype=int)
246
+
247
+ def get_type(self):
248
+ """
249
+ Get the name of the type of the field.
250
+
251
+ Returns
252
+ -------
253
+ onetick.py.Operation
254
+ """
255
+ return _Operation(op_str=f'{self}.GET_TYPE()', dtype=str)
256
+
257
+
258
+ class Expression:
259
+ """
260
+ Class to save per-tick-script expressions along with their possible values.
261
+
262
+ Parameters
263
+ ----------
264
+ expr
265
+ string expression that will be saved to per tick script
266
+ values:
267
+ values that this expression can take.
268
+ For example, bool operation can take many values.
269
+ lhs:
270
+ True if expression is left hand expression.
271
+ In this case value of expression must be callable.
272
+ Calling it with right hand expression value as an argument
273
+ should be the same as execute the whole expression.
274
+ """
275
+ def __init__(self, *values: Any, expr: Optional[str] = None, lhs: bool = False):
276
+ self.values = values
277
+ self._expr = expr
278
+ self.lhs = lhs
279
+ if self.lhs:
280
+ assert callable(self.value)
281
+ assert self.expr
282
+
283
+ @property
284
+ def expr(self):
285
+ if self._expr:
286
+ return self._expr
287
+ if self.is_emulator:
288
+ self._expr = 'LOCAL::OUTPUT_TICK'
289
+ elif self.is_column:
290
+ self._expr = str(self.value)
291
+ elif self.values:
292
+ self._expr = self.value_to_onetick(self.value)
293
+ return self._expr
294
+
295
+ @property
296
+ def value(self):
297
+ length = len(self.values)
298
+ if length == 0:
299
+ raise ValueError(f"Expression '{self}' doesn't have values.")
300
+ if length > 1:
301
+ raise ValueError(f"Expression '{self}' have more than one value.")
302
+ return self.values[0]
303
+
304
+ @cached_property
305
+ def dtype(self):
306
+ return ott.get_type_by_objects(self.values)
307
+
308
+ @property
309
+ def is_emulator(self) -> bool:
310
+ try:
311
+ return isinstance(self.value, _EmulateObject)
312
+ except ValueError:
313
+ return False
314
+
315
+ @property
316
+ def is_state_vars(self) -> bool:
317
+ try:
318
+ return isinstance(self.value, _EmulateStateVars)
319
+ except ValueError:
320
+ return False
321
+
322
+ @property
323
+ def is_static(self) -> bool:
324
+ try:
325
+ return isinstance(self.value, Static)
326
+ except ValueError:
327
+ return False
328
+
329
+ @property
330
+ def is_dynamic_tick(self) -> bool:
331
+ try:
332
+ return type(self.value) is _DynamicTick
333
+ except ValueError:
334
+ return False
335
+
336
+ @property
337
+ def is_tick(self) -> bool:
338
+ try:
339
+ return isinstance(self.value, _TickSequenceTickBase)
340
+ except ValueError:
341
+ return False
342
+
343
+ @property
344
+ def is_column(self) -> bool:
345
+ try:
346
+ return isinstance(self.value, _Column)
347
+ except ValueError:
348
+ return False
349
+
350
+ @property
351
+ def is_local_variable(self) -> bool:
352
+ try:
353
+ return isinstance(self.value, LocalVariable)
354
+ except ValueError:
355
+ return False
356
+
357
+ @property
358
+ def is_operation(self) -> bool:
359
+ try:
360
+ return isinstance(self.value, _Operation)
361
+ except ValueError:
362
+ return False
363
+
364
+ @property
365
+ def predefined(self) -> bool:
366
+ """Check if the value of expression is known before the execution of query"""
367
+ return not self.is_operation
368
+
369
+ @property
370
+ def expressible(self) -> bool:
371
+ return bool(self.expr)
372
+
373
+ def __str__(self):
374
+ if not self.expressible:
375
+ raise ValueError("This Expression can't be expressed in OneTick or is undefined yet")
376
+ return self.expr
377
+
378
+ def convert_to_operation(self):
379
+ """
380
+ Convert otp.Column to otp.Operation.
381
+ Needed to convert expressions like:
382
+ if tick['X']:
383
+ to
384
+ if (X != 0) {
385
+ """
386
+ if self.is_column:
387
+ self.values = [self.value._make_python_way_bool_expression()]
388
+ self._expr = str(self.value)
389
+
390
+ @staticmethod
391
+ def value_to_onetick(value: Union[str, int, float, bool, None, _Operation]) -> str:
392
+ """
393
+ Python value will be converted accordingly to OneTick syntax
394
+ (lowercase boolean values, string in quotes, etc.)
395
+ """
396
+ if value is None:
397
+ return str(ott.nan)
398
+ if isinstance(value, bool):
399
+ return str(value).lower()
400
+ return ott.value2str(value)
401
+
402
+
403
+ class CaseOperatorParser:
404
+ """
405
+ Class with methods to convert ast operators to their string or python representations.
406
+ Only ast operators that can be used in OneTick's CASE function are accepted.
407
+ """
408
+
409
+ @staticmethod
410
+ def py_operator(op: Union[ast.operator, ast.cmpop, ast.unaryop, ast.boolop]) -> Callable:
411
+ """
412
+ Convert ast operator to python function for this operator.
413
+
414
+ Parameters
415
+ ----------
416
+ op
417
+ ast operator object
418
+ """
419
+ return {
420
+ # binary
421
+ ast.Add: operator.add,
422
+ ast.Sub: operator.sub,
423
+ ast.Mult: operator.mul,
424
+ ast.Div: operator.truediv,
425
+ ast.BitAnd: operator.and_,
426
+ ast.BitOr: operator.or_,
427
+ ast.Mod: operator.mod,
428
+ # unary
429
+ ast.UAdd: operator.pos,
430
+ ast.USub: operator.neg,
431
+ ast.Not: operator.not_,
432
+ ast.Invert: operator.invert,
433
+ # compare
434
+ ast.Lt: operator.lt,
435
+ ast.LtE: operator.le,
436
+ ast.Gt: operator.gt,
437
+ ast.GtE: operator.ge,
438
+ ast.Eq: operator.eq,
439
+ ast.NotEq: operator.ne,
440
+ # bool
441
+ ast.And: lambda x, y: x and y,
442
+ ast.Or: lambda x, y: x or y,
443
+ }[type(op)] # type: ignore[return-value]
444
+
445
+ @staticmethod
446
+ def operator(op: Union[ast.operator, ast.cmpop, ast.unaryop, ast.boolop]) -> str:
447
+ """
448
+ Convert ast operator to OneTick's string representation.
449
+
450
+ Parameters
451
+ ----------
452
+ op
453
+ ast operator object
454
+ """
455
+ return {
456
+ # binary
457
+ ast.Add: '+',
458
+ ast.Sub: '-',
459
+ ast.Mult: '*',
460
+ ast.Div: '/',
461
+ ast.Mod: '%',
462
+ # unary
463
+ ast.UAdd: '+',
464
+ ast.USub: '-',
465
+ # compare
466
+ ast.Lt: '<',
467
+ ast.LtE: '<=',
468
+ ast.Gt: '>',
469
+ ast.GtE: '>=',
470
+ ast.Eq: '=',
471
+ ast.NotEq: '!=',
472
+ # bool
473
+ ast.And: 'AND',
474
+ ast.Or: 'OR',
475
+ }[type(op)]
476
+
477
+
478
+ class OperatorParser(CaseOperatorParser):
479
+ """
480
+ Class with methods to convert ast operators to their string or python representations.
481
+ Only ast operators that can be used in OneTick's per tick script are accepted.
482
+ """
483
+
484
+ @staticmethod
485
+ def py_operator(op: Union[ast.operator, ast.cmpop, ast.unaryop, ast.boolop],
486
+ aug: bool = False, **kwargs) -> Callable:
487
+ """
488
+ Convert ast operator to python function for this operator.
489
+
490
+ Parameters
491
+ ----------
492
+ op
493
+ ast operator object
494
+ aug
495
+ ast don't have separate inplace operators (+=, -=, etc.)
496
+ If this parameter is True then operator is inplace and otherwise if False.
497
+ """
498
+ if aug:
499
+ assert isinstance(op, ast.operator)
500
+ return {
501
+ ast.Add: operator.iadd,
502
+ ast.Sub: operator.isub,
503
+ ast.Mult: operator.imul,
504
+ ast.Div: operator.itruediv,
505
+ }[type(op)]
506
+ return CaseOperatorParser.py_operator(op, **kwargs)
507
+
508
+ @staticmethod
509
+ def operator(op: Union[ast.operator, ast.cmpop, ast.unaryop, ast.boolop],
510
+ aug: bool = False) -> str:
511
+ """
512
+ Convert ast operator to its string representation.
513
+
514
+ Parameters
515
+ ----------
516
+ op
517
+ ast operator object
518
+ aug
519
+ ast don't have separate inplace binary operators (+=, -=, etc.)
520
+ If this parameter is True then parameter is inplace and otherwise if False.
521
+ """
522
+ if aug:
523
+ assert isinstance(op, ast.operator)
524
+ return {
525
+ ast.Add: '+=',
526
+ ast.Sub: '-=',
527
+ ast.Mult: '*=',
528
+ ast.Div: '/=',
529
+ }[type(op)]
530
+ try:
531
+ return {
532
+ ast.Eq: '==',
533
+ ast.And: '&&',
534
+ ast.Or: '||',
535
+ }[type(op)]
536
+ except KeyError:
537
+ return CaseOperatorParser.operator(op)
538
+
539
+
540
+ class ExpressionParser:
541
+ """
542
+ Class with methods to convert ast expressions to OneTick's script or function syntax.
543
+ """
544
+ def __init__(self, fun: 'FunctionParser'):
545
+ self.fun = fun
546
+ self.operator_parser = OperatorParser()
547
+
548
+ @contextmanager
549
+ def _replace_context(self, closure_vars: inspect.ClosureVars):
550
+ """
551
+ Temporarily change closure variables in self.fun.
552
+ Variables will be replaced with those from closure_vars parameter.
553
+ """
554
+ nonlocals, globals_, *_ = closure_vars
555
+ assert isinstance(self.fun.closure_vars.globals, dict)
556
+ assert isinstance(self.fun.closure_vars.nonlocals, dict)
557
+ saved_globals = self.fun.closure_vars.globals.copy()
558
+ saved_nonlocals = self.fun.closure_vars.nonlocals.copy()
559
+ self.fun.closure_vars.globals.update(globals_)
560
+ self.fun.closure_vars.nonlocals.update(nonlocals)
561
+ yield
562
+ self.fun.closure_vars.globals.update(saved_globals)
563
+ self.fun.closure_vars.nonlocals.update(saved_nonlocals)
564
+
565
+ def constant(self, expr: ast.Constant) -> Expression:
566
+ """Some basic constant value: string, integer, float."""
567
+ return Expression(expr.value)
568
+
569
+ def string(self, expr: "ast.Str") -> Expression:
570
+ """String (for backward compatibility with Python 3.7)."""
571
+ return Expression(expr.s)
572
+
573
+ def number(self, expr: "ast.Str") -> Expression:
574
+ """Number (for backward compatibility with Python 3.7)."""
575
+ return Expression(expr.n)
576
+
577
+ def name(self, expr: ast.Name) -> Expression:
578
+ """
579
+ Name of the variable.
580
+ Every variable in per-tick script function, if defined correctly,
581
+ is considered to be local per-tick script variable.
582
+ If variable with this name is not found it will be captured from function context.
583
+ """
584
+ if self.fun.arg_name and expr.id == self.fun.arg_name:
585
+ value = self.fun.emulator if self.fun.emulator is not None else expr.id
586
+ return Expression(value)
587
+
588
+ if type(expr.ctx) is not ast.Load:
589
+ # local variable, left-hand side
590
+ return Expression(LocalVariable(expr.id))
591
+
592
+ for dict_name in ('LOCAL_VARS', 'STATIC_VARS'):
593
+ # local or static variable, right-hand side
594
+ variables = getattr(self.fun.emulator, dict_name, {})
595
+ if expr.id in variables:
596
+ dtype = ott.get_type_by_objects([variables[expr.id]])
597
+ if issubclass(dtype, _TickSequenceTickBase):
598
+ # ticks have schema, owner and methods, so using saved value
599
+ return Expression(variables[expr.id])
600
+ return Expression(LocalVariable(expr.id, dtype))
601
+
602
+ if not self.fun._from_args_annotations and expr.id in self.fun.args_annotations:
603
+ # parameter of the per-tick script function
604
+ return Expression(_Operation(op_str=expr.id, dtype=self.fun.args_annotations[expr.id]))
605
+
606
+ value = eval(expr.id, self.fun.closure_vars.globals, self.fun.closure_vars.nonlocals) # type: ignore[arg-type]
607
+ return Expression(value)
608
+
609
+ def index(self, expr: ast.Index) -> Expression:
610
+ """Proxy object in ast.Subscript in python < 3.9"""
611
+ return self.expression(expr.value) # type: ignore[attr-defined]
612
+
613
+ def slice(self, expr: ast.Slice) -> Expression:
614
+ """
615
+ Slice of the list.
616
+ For example:
617
+ a = [1, 2, 3, 4]
618
+ a[2:4]
619
+ Here, 2:4 is the slice.
620
+ """
621
+ lower = self.expression(expr.lower).value if expr.lower else None
622
+ upper = self.expression(expr.upper).value if expr.upper else None
623
+ step = self.expression(expr.step).value if expr.step else None
624
+ return Expression(slice(lower, upper, step))
625
+
626
+ def subscript(self, expr: ast.Subscript) -> Expression:
627
+ """
628
+ Expression like: tick['X'].
629
+ Setting items of ticks and state variables is supported.
630
+ Getting items supported for any captured variable.
631
+ """
632
+ val = self.expression(expr.value)
633
+ item = self.expression(expr.slice)
634
+
635
+ if type(expr.ctx) is ast.Load:
636
+ v = val.value[item.value]
637
+ return Expression(v)
638
+
639
+ # index of per tick script function parameter or tick sequence tick is column name
640
+ if not (val.is_emulator or val.is_tick or val.is_state_vars):
641
+ raise ValueError(f"Setting items supported only for "
642
+ f"'{self.fun.arg_name}' function argument, "
643
+ f"tick sequences' ticks and state variables object")
644
+
645
+ return Expression(
646
+ lambda rhs: val.value.__setitem__(item.value, rhs),
647
+ expr=item.value,
648
+ lhs=True,
649
+ )
650
+
651
+ def attribute(self, expr: ast.Attribute) -> Expression:
652
+ """
653
+ Expression like: tick.X
654
+ For now we only support setting attributes of first function parameter.
655
+ Getting attributes supported for any captured variable.
656
+ """
657
+ val = self.expression(expr.value)
658
+ attr = expr.attr
659
+
660
+ if type(expr.ctx) is ast.Load:
661
+ v = getattr(val.value, attr)
662
+ return Expression(v)
663
+
664
+ # attribute of per tick script function parameter or tick sequence tick is column name
665
+ if not (val.is_emulator or val.is_tick):
666
+ raise ValueError(f"Setting attributes supported only for "
667
+ f"'{self.fun.arg_name}' function argument and tick sequences' ticks")
668
+
669
+ return Expression(
670
+ lambda rhs: val.value.__setattr__(attr, rhs),
671
+ expr=attr,
672
+ lhs=True,
673
+ )
674
+
675
+ def bin_op(self, expr: ast.BinOp) -> Expression:
676
+ """
677
+ Binary operation expression: 2 + 2, tick['X'] * 2, etc.
678
+ """
679
+ left = self.expression(expr.left)
680
+ py_op = self.operator_parser.py_operator(expr.op)
681
+ right = self.expression(expr.right)
682
+ value = py_op(left.value, right.value)
683
+ return Expression(value)
684
+
685
+ def unary_op(self, expr: ast.UnaryOp) -> Expression:
686
+ """
687
+ Unary operation expression: -1, -tick['X'], not tick['X'], ~tick['X'], etc.
688
+ """
689
+ py_op = self.operator_parser.py_operator(expr.op)
690
+ operand = self.expression(expr.operand)
691
+ if operand.is_operation:
692
+ # special case for negative otp.Columns and otp.Operations
693
+ if isinstance(expr.op, (ast.Not, ast.Invert)):
694
+ operand.convert_to_operation()
695
+ if isinstance(expr.op, ast.Not):
696
+ py_op = self.operator_parser.py_operator(ast.Invert())
697
+ value = py_op(operand.value)
698
+ return Expression(value)
699
+
700
+ def bool_op(self, expr: ast.BoolOp) -> Expression:
701
+ """
702
+ Bool operation expression: True and tick['X'], etc.
703
+ Note that
704
+ * all python values will be checked inplace and will not be written to the script
705
+ * short-circuit logic will work for python values
706
+ For example:
707
+ True and 0 and tick['X'] == 1 -------> false
708
+ 'true' or False or tick['X'] == 1 -------> true
709
+ True and True and tick['X'] == 1 -------> X == 1
710
+ """
711
+ value = None
712
+ for e in expr.values:
713
+ expression = self.expression(e)
714
+ expression.convert_to_operation()
715
+ v = expression.value
716
+
717
+ if not expression.is_operation:
718
+ # short-circuit logic, return as early as possible
719
+ if isinstance(expr.op, ast.And) and not v:
720
+ # TODO: return v, not True or False
721
+ # TODO: there can be many values if operations are present
722
+ value = False
723
+ break
724
+ if isinstance(expr.op, ast.Or) and v:
725
+ value = True
726
+ break
727
+ continue
728
+
729
+ if value is None:
730
+ value = v
731
+ continue
732
+
733
+ if isinstance(value, _Operation) or expression.is_operation:
734
+ # change operator for operations
735
+ py_op = self.operator_parser.py_operator({
736
+ ast.And: ast.BitAnd(),
737
+ ast.Or: ast.BitOr(),
738
+ }[type(expr.op)])
739
+ else:
740
+ py_op = self.operator_parser.py_operator(expr.op)
741
+
742
+ value = py_op(value, v)
743
+ return Expression(value)
744
+
745
+ def _convert_in_to_bool_op(self, expr: ast.Compare) -> Union[ast.Compare, ast.BoolOp]:
746
+ """
747
+ Convert expressions like:
748
+ tick['X'] in [1, 2] -----> tick['X'] == 1 or tick['X'] == 2
749
+ tick['X'] not in [1, 2] -----> tick['X'] != 1 and tick['X'] != 2
750
+ """
751
+ left, op, right = expr.left, expr.ops[0], expr.comparators[0]
752
+ if not isinstance(op, (ast.In, ast.NotIn)):
753
+ return expr
754
+
755
+ assert len(expr.ops) == 1
756
+ assert len(expr.comparators) == 1
757
+
758
+ right_value = self.expression(right).value
759
+ if isinstance(right_value, range) and right_value.step == 1:
760
+ # replace tick['X'] in range(5, 10) to X >=5 AND X < 10
761
+ return ast.BoolOp(
762
+ op=ast.And(),
763
+ values=[
764
+ ast.Compare(left=left, ops=[ast.GtE()], comparators=[ast.Constant(right_value.start)]),
765
+ ast.Compare(left=left, ops=[ast.Lt()], comparators=[ast.Constant(right_value.stop)]),
766
+ ]
767
+ )
768
+
769
+ right_values = [ast.Constant(r) for r in right_value]
770
+
771
+ bool_op: ast.boolop
772
+ compare_op: ast.cmpop
773
+ if isinstance(op, ast.In):
774
+ bool_op, compare_op = ast.Or(), ast.Eq()
775
+ else:
776
+ bool_op, compare_op = ast.And(), ast.NotEq()
777
+
778
+ values: List[ast.expr] = [
779
+ ast.Compare(left=left, ops=[compare_op], comparators=[r])
780
+ for r in right_values
781
+ ]
782
+ return ast.BoolOp(op=bool_op, values=values)
783
+
784
+ def _convert_many_comparators_to_bool_op(self, expr: ast.Compare) -> Union[ast.Compare, ast.BoolOp]:
785
+ """
786
+ OneTick don't support compare expressions with many comparators
787
+ so replacing them with several simple expressions.
788
+ For example:
789
+ 1 < tick['X'] < 3, -----> tick['X'] > 1 AND tick['X'] < 3
790
+ """
791
+ if len(expr.comparators) == 1 and len(expr.ops) == 1:
792
+ return expr
793
+
794
+ comparators = []
795
+ comparators.append(expr.left)
796
+ ops = []
797
+ for op, right in zip(expr.ops, expr.comparators):
798
+ ops.append(op)
799
+ comparators.append(right)
800
+
801
+ bool_operands: List[ast.expr] = []
802
+ for i in range(len(comparators) - 1):
803
+ left, op, right = comparators[i], ops[i], comparators[i + 1]
804
+ bool_operands.append(
805
+ ast.Compare(left=left, ops=[op], comparators=[right])
806
+ )
807
+ return ast.BoolOp(op=ast.And(), values=bool_operands)
808
+
809
+ def compare(self, expr: ast.Compare) -> Expression:
810
+ """
811
+ Compare operation expression: tick['X'] > 1, 1 < 2 < 3, tick['X'] in [1, 2] etc.
812
+ """
813
+ if len(expr.ops) > 1:
814
+ return self.expression(
815
+ self._convert_many_comparators_to_bool_op(expr)
816
+ )
817
+
818
+ op = expr.ops[0]
819
+ if isinstance(op, (ast.In, ast.NotIn)):
820
+ return self.expression(
821
+ self._convert_in_to_bool_op(expr)
822
+ )
823
+
824
+ left = self.expression(expr.left)
825
+ right = self.expression(expr.comparators[0])
826
+
827
+ py_op = self.operator_parser.py_operator(op)
828
+ value = py_op(left.value, right.value)
829
+ return Expression(value)
830
+
831
+ def keyword(self, expr: ast.keyword) -> Tuple[str, Any]:
832
+ """
833
+ Keyword argument expression from function call: func(key=value).
834
+ Not converted to per tick script in any way, needed only in self.call() function.
835
+ """
836
+ arg = expr.arg
837
+ val = self.expression(expr.value)
838
+ assert arg is not None
839
+ return arg, val.value
840
+
841
+ def call(self, expr: ast.Call) -> Expression:
842
+ """
843
+ Any call expression, like otp.nsectime().
844
+ The returned value of the call will be inserted in script.
845
+ """
846
+ func = self.expression(expr.func)
847
+
848
+ # TODO: refactor, this code is copy-pasted from CaseExpressionParser.call()
849
+ need_to_parse = False
850
+ if not isinstance(func.value, types.BuiltinMethodType) and expr.args:
851
+ node = expr.args[0]
852
+ if isinstance(node, ast.Name) and node.id == self.fun.arg_name:
853
+ # we will parse inner function call to OneTick per-tick script function
854
+ # only if one of the function call arguments is 'tick' parameter of the original function
855
+ need_to_parse = True
856
+
857
+ if need_to_parse:
858
+ err = None
859
+ func_name = func.value.__name__
860
+ if str(func.value) not in self.fun.emulator.FUNCTIONS:
861
+ try:
862
+ fp = FunctionParser(func.value, emulator=self.fun.emulator,
863
+ check_arg_name=self.fun.arg_name, inner_function=True)
864
+ except Exception as e:
865
+ err = e
866
+ else:
867
+ value = fp.per_tick_script()
868
+ self.fun.emulator.FUNCTIONS[str(func.value)] = (value, fp.args_annotations, fp.return_annotation)
869
+ if err is None:
870
+ _, args_annotations, return_type = self.fun.emulator.FUNCTIONS[str(func.value)]
871
+ if expr.keywords:
872
+ raise ValueError(
873
+ f"Passing keyword parameters to per-tick script function '{func_name}' is not supported"
874
+ )
875
+ args = []
876
+ for arg, (name, dtype) in zip(expr.args[1:], args_annotations.items()):
877
+ if isinstance(arg, ast.Starred):
878
+ raise ValueError(
879
+ f"Passing starred parameter to per-tick script function '{func_name}' is not supported"
880
+ )
881
+ arg_expr = self.expression(arg)
882
+ msg = (f"In function '{func_name}' parameter '{name}'"
883
+ f" has type annotation '{dtype.__name__}',"
884
+ f" but the type of passed argument is '{arg_expr.dtype.__name__}'")
885
+ try:
886
+ widest_type = ott.get_type_by_objects([arg_expr.dtype, dtype])
887
+ except TypeError as e:
888
+ raise TypeError(msg) from e
889
+ if widest_type is not arg_expr.dtype:
890
+ raise TypeError(msg)
891
+ args.append(arg_expr)
892
+ str_args = ', '.join(map(str, args))
893
+ return Expression(_Operation(op_str=f'{func_name}({str_args})', dtype=return_type))
894
+
895
+ args = []
896
+ for arg in expr.args:
897
+ # TODO: support starred in CaseExpressionParser.call()
898
+ if isinstance(arg, ast.Starred):
899
+ args.extend(self.expression(arg.value).value)
900
+ else:
901
+ args.append(self.expression(arg).value)
902
+ keywords = dict(self.keyword(keyword) for keyword in expr.keywords)
903
+ value = func.value(*args, **keywords)
904
+ return Expression(value)
905
+
906
+ def formatted_value(self, expr: ast.FormattedValue) -> Expression:
907
+ """
908
+ Block from the f-string in curly brackets, e.g.
909
+ {tick['A']} and {123} in f"{tick['A']} {123}"
910
+ """
911
+ return self.expression(expr.value)
912
+
913
+ def joined_str(self, expr: ast.JoinedStr) -> Expression:
914
+ """
915
+ F-string expression, like: f"{tick['A']} {123}"
916
+ """
917
+ expressions = [self.expression(value) for value in expr.values]
918
+ value = None
919
+ for expression in expressions:
920
+ v = expression.value
921
+ if expression.is_operation:
922
+ v = v.apply(str)
923
+ else:
924
+ v = str(v)
925
+ if value is None:
926
+ value = v
927
+ else:
928
+ value = value + v
929
+ return Expression(value)
930
+
931
+ def list(self, expr: ast.List) -> Expression:
932
+ """
933
+ List expression, like: [1, 2, 3, 4, 5]
934
+ """
935
+ value = []
936
+ for e in expr.elts:
937
+ if isinstance(e, ast.Starred):
938
+ value.extend(self.expression(e.value).value)
939
+ else:
940
+ value.append(self.expression(e).value)
941
+ return Expression(value, expr=None)
942
+
943
+ def tuple(self, expr: ast.Tuple) -> Expression:
944
+ """
945
+ Tuple expression, like: (1, 2, 3, 4, 5)
946
+ """
947
+ expression = self.list(expr) # type: ignore[arg-type]
948
+ expression.values = (tuple(expression.value),)
949
+ return expression
950
+
951
+ @property
952
+ def _expression(self) -> dict:
953
+ """Mapping from ast expression to parser functions"""
954
+ mapping = {
955
+ ast.Constant: self.constant,
956
+ ast.Name: self.name,
957
+ ast.Attribute: self.attribute,
958
+ ast.Index: self.index,
959
+ ast.Subscript: self.subscript,
960
+ ast.BinOp: self.bin_op,
961
+ ast.UnaryOp: self.unary_op,
962
+ ast.BoolOp: self.bool_op,
963
+ ast.Compare: self.compare,
964
+ ast.Call: self.call,
965
+ ast.FormattedValue: self.formatted_value,
966
+ ast.JoinedStr: self.joined_str,
967
+ ast.List: self.list,
968
+ ast.Tuple: self.tuple,
969
+ ast.Slice: self.slice,
970
+ }
971
+ deprecated = {
972
+ 'NameConstant': self.constant,
973
+ 'Str': self.string,
974
+ 'Num': self.number,
975
+ }
976
+ with warnings.catch_warnings():
977
+ warnings.simplefilter('ignore', DeprecationWarning)
978
+ for name, callback in deprecated.items():
979
+ if hasattr(ast, name):
980
+ mapping[getattr(ast, name)] = callback
981
+ return mapping
982
+
983
+ def expression(self, expr: ast.expr) -> Expression:
984
+ """Return parsed expression according to its type."""
985
+ return self._expression[type(expr)](expr)
986
+
987
+
988
+ class CaseExpressionParser(ExpressionParser):
989
+ """
990
+ Class with methods to convert ast expressions to CASE function.
991
+ """
992
+ def __init__(self, fun: 'FunctionParser'):
993
+ super().__init__(fun)
994
+ self.operator_parser = CaseOperatorParser() # type: ignore[assignment]
995
+
996
+ def _convert_bool_op_to_if_expr(self, expr: ast.expr) -> ast.expr:
997
+ """
998
+ Special case to convert bool operation to if expression.
999
+ For example:
1000
+ lambda row: row['A'] or -1
1001
+ will be converted to:
1002
+ CASE(A != 0, 1, A, -1)
1003
+ """
1004
+ if not isinstance(expr, ast.BoolOp):
1005
+ return expr
1006
+
1007
+ def get_if_expr(first, second):
1008
+ if isinstance(expr.op, ast.Or):
1009
+ return ast.IfExp(test=first, body=first, orelse=second)
1010
+ if isinstance(expr.op, ast.And):
1011
+ return ast.IfExp(test=first, body=second, orelse=first)
1012
+
1013
+ first = None
1014
+ for i in range(len(expr.values) - 1):
1015
+ if first is None:
1016
+ first = expr.values[i]
1017
+ first = self._convert_bool_op_to_if_expr(first)
1018
+ second = expr.values[i + 1]
1019
+ second = self._convert_bool_op_to_if_expr(second)
1020
+ first = get_if_expr(first, second)
1021
+ assert first is not None
1022
+ return first
1023
+
1024
+ def if_expr(self, expr: ast.IfExp) -> Expression:
1025
+ """
1026
+ If expression: 'A' if tick['X'] > 0 else 'B'.
1027
+ Do not confuse with if statement.
1028
+ Will be converted to OneTick case function: CASE(X > 0, 1, 'A', 'B').
1029
+ If condition value can be deduced before execution of script,
1030
+ then if or else value will be returned without using CASE() function.
1031
+ For example:
1032
+ tick['A'] if False else 3 -----------> 3
1033
+ """
1034
+ test = self.expression(expr.test)
1035
+ if test.predefined:
1036
+ # we can remove unnecessary branch if condition value is already known
1037
+ if test.value:
1038
+ return self.expression(expr.body)
1039
+ return self.expression(expr.orelse)
1040
+ body = self.expression(expr.body)
1041
+ orelse = self.expression(expr.orelse)
1042
+ test.convert_to_operation()
1043
+
1044
+ str_expr = f'CASE({test}, 1, {body}, {orelse})'
1045
+ value = _LambdaIfElse(str_expr, ott.get_type_by_objects([*body.values, *orelse.values]))
1046
+ return Expression(value, expr=str_expr)
1047
+
1048
+ def call(self, expr: ast.Call) -> Expression:
1049
+ """
1050
+ For CASE() function we support using inner functions that return valid case expression.
1051
+ """
1052
+ func = self.expression(expr.func)
1053
+
1054
+ need_to_parse = False
1055
+ if not isinstance(func.value, types.BuiltinMethodType):
1056
+ for node in expr.args + [kw.value for kw in expr.keywords]:
1057
+ if isinstance(node, ast.Name) and node.id == self.fun.arg_name:
1058
+ # we will parse inner function call to OneTick expression
1059
+ # only if one of the function call arguments is
1060
+ # 'tick' or 'row' parameter of the original function
1061
+ need_to_parse = True
1062
+ break
1063
+ orig_err = None
1064
+ if not need_to_parse:
1065
+ with _CompareTrackScope(emulation_enabled=False):
1066
+ try:
1067
+ return super().call(expr)
1068
+ except Exception as err:
1069
+ orig_err = err
1070
+ uw = UserWarning(
1071
+ f"Function '{astunparse(expr)}' can't be called in python, "
1072
+ "will try to parse it to OneTick expression. "
1073
+ f"Use '{self.fun.arg_name}' in function's signature to indicate "
1074
+ "that this function can be parsed to OneTick expression."
1075
+ )
1076
+ uw.__cause__ = err
1077
+ warnings.warn(uw)
1078
+
1079
+ fp = FunctionParser(func.value, check_arg_name=False)
1080
+
1081
+ kwargs = {}
1082
+ args = fp.ast_node.args.args
1083
+ if fp.is_method:
1084
+ args = args[1:]
1085
+ for arg, default in zip(reversed(args), reversed(fp.ast_node.args.defaults)):
1086
+ kwargs[arg.arg] = default
1087
+ kwargs.update({keyword.arg: keyword.value for keyword in expr.keywords}) # type: ignore[misc]
1088
+ for arg, arg_value in zip(args, expr.args):
1089
+ kwargs[arg.arg] = arg_value
1090
+
1091
+ try:
1092
+ value = fp.compress()
1093
+ except Exception as err:
1094
+ try:
1095
+ return super().call(expr)
1096
+ except Exception:
1097
+ raise ValueError(
1098
+ f"Can't convert function '{astunparse(expr)}' to CASE() expression."
1099
+ ) from err
1100
+ try:
1101
+ with self._replace_context(fp.closure_vars):
1102
+ # replace function parameters with calculated values
1103
+ value = fp.case_statement_parser._replace_nodes(value, replace_name=kwargs)
1104
+ return self.expression(value)
1105
+ except Exception as err:
1106
+ if orig_err is not None:
1107
+ raise err from orig_err
1108
+ raise err
1109
+
1110
+ @property
1111
+ def _expression(self) -> dict:
1112
+ return dict(super()._expression.items() | {
1113
+ ast.IfExp: self.if_expr,
1114
+ }.items())
1115
+
1116
+
1117
+ class CaseStatementParser:
1118
+ """
1119
+ Class with methods to convert ast statements to CASE function.
1120
+ """
1121
+ def __init__(self, fun: 'FunctionParser'):
1122
+ self.fun = fun
1123
+ self.expression_parser = CaseExpressionParser(fun=fun)
1124
+ self.operator_parser = CaseOperatorParser()
1125
+
1126
+ T = TypeVar('T', bound=ast.AST)
1127
+
1128
+ @staticmethod
1129
+ def _replace_nodes(node: T,
1130
+ replace_name: Optional[Dict[str, ast.expr]] = None,
1131
+ replace_break: Union[ast.stmt, Exception, Type[Exception], None] = None,
1132
+ inplace: bool = False) -> T:
1133
+ """
1134
+ Function to replace expressions and statements inside ast.For node.
1135
+
1136
+ Parameters
1137
+ ----------
1138
+ node
1139
+ ast node in which expressions and statements will be replaced
1140
+ inplace
1141
+ if True `node` object will be modified else it will be copied and the copy will be returned
1142
+ replace_name
1143
+ mapping from ast.Name ids to ast expressions.
1144
+ ast.Name nodes with these ids will be replaced with corresponding expressions.
1145
+ replace_break
1146
+ replace break statement with another statement.
1147
+ We can't execute for loop on real data here so we can't allow break statements at all.
1148
+ So we will replace them with statements from code after the for loop.
1149
+ If replace_break is Exception then exception will be raised when visiting ast.Break nodes.
1150
+ """
1151
+ class RewriteName(ast.NodeTransformer):
1152
+ def visit_Name(self, n: ast.Name):
1153
+ return (replace_name or {}).get(n.id) or n
1154
+
1155
+ def visit_Continue(self, n: ast.Continue):
1156
+ # TODO: pass is not continue, we must allow only bodies with one statement in this case
1157
+ return ast.Pass()
1158
+
1159
+ def visit_Break(self, n: ast.Break):
1160
+ if replace_break is None:
1161
+ return n
1162
+ if inspect.isclass(replace_break) and issubclass(replace_break, Exception):
1163
+ raise replace_break("Break is found in for loop and replacer is not provided")
1164
+ if isinstance(replace_break, Exception):
1165
+ raise replace_break
1166
+ assert isinstance(replace_break, ast.stmt)
1167
+ return CaseStatementParser._replace_nodes(replace_break, replace_name=replace_name)
1168
+
1169
+ if not inplace:
1170
+ node = deepcopy(node)
1171
+ RewriteName().visit(node)
1172
+ return node
1173
+
1174
+ def _flatten_for_stmt(self,
1175
+ stmt: ast.For,
1176
+ replace_break: Union[ast.stmt, Exception, Type[Exception], None] = None,
1177
+ stmt_after_for: Optional[ast.stmt] = None) -> List[ast.stmt]:
1178
+ """
1179
+ Convert for statement to list of copy-pasted statements from the body for each iteration.
1180
+ """
1181
+ stmts = []
1182
+ target = stmt.target
1183
+ assert isinstance(target, (ast.Name, ast.Tuple)), (
1184
+ f"Unsupported expression '{astunparse(target)}' is used in for statement."
1185
+ " Please, use variable or tuple of variables instead."
1186
+ )
1187
+ if isinstance(target, ast.Tuple):
1188
+ for t in target.elts:
1189
+ assert isinstance(t, ast.Name)
1190
+ targets = target.elts
1191
+ else:
1192
+ targets = [target]
1193
+ replace_name = {}
1194
+ iter_ = self.expression_parser.expression(stmt.iter)
1195
+ for iter_value in iter_.value:
1196
+ if not isinstance(iter_value, Iterable) or isinstance(iter_value, str):
1197
+ iter_value = [iter_value]
1198
+ replace_name = {
1199
+ target.id: ast.Constant(value) # type: ignore[attr-defined]
1200
+ for target, value in zip(targets, iter_value)
1201
+ }
1202
+ for s in stmt.body:
1203
+ stmts.append(
1204
+ self._replace_nodes(s,
1205
+ replace_name=replace_name, # type: ignore[arg-type]
1206
+ replace_break=replace_break)
1207
+ )
1208
+ if stmt_after_for and replace_name:
1209
+ stmts.append(self._replace_nodes(stmt_after_for, replace_name=replace_name)) # type: ignore[arg-type]
1210
+ return stmts
1211
+
1212
+ def _flatten_for_stmts(self, stmts: List[ast.stmt]) -> List[Union[ast.If, ast.Return, ast.Pass]]:
1213
+ """
1214
+ Find ast.For statements in list of statements and flatten them.
1215
+ Return list of statements without ast.For.
1216
+ Additionally raise exception if unsupported statement is found.
1217
+ """
1218
+ # TODO: support ast.For statements on deeper levels
1219
+ res_stmts = []
1220
+ for i, stmt in enumerate(stmts):
1221
+ if not isinstance(stmt, (ast.If, ast.Return, ast.For, ast.Pass)):
1222
+ raise ValueError(
1223
+ "this function can't be converted to CASE function, "
1224
+ "only for, if, return and pass statements are allowed"
1225
+ )
1226
+ if isinstance(stmt, ast.For):
1227
+ try:
1228
+ res_stmts.extend(self._flatten_for_stmt(stmt, replace_break=ValueError))
1229
+ except ValueError:
1230
+ stmts_left = len(stmts[i + 1:])
1231
+ assert stmts_left in (0, 1), "Can't be more than one statement after break"
1232
+ replace_break: Union[ast.Return, ast.Pass]
1233
+ if stmts_left == 0:
1234
+ stmt_after_for = None
1235
+ replace_break = ast.Pass()
1236
+ else:
1237
+ stmt_after_for = stmts[i + 1]
1238
+ assert isinstance(stmt_after_for, (ast.Return, ast.Pass)), (
1239
+ 'Can only use pass and return statements after for loop with break'
1240
+ )
1241
+ replace_break = stmt_after_for
1242
+ res_stmts.extend(self._flatten_for_stmt(stmt,
1243
+ replace_break=replace_break,
1244
+ stmt_after_for=stmt_after_for))
1245
+ break
1246
+ else:
1247
+ res_stmts.append(stmt)
1248
+ return res_stmts # type: ignore[return-value]
1249
+
1250
+ def _compress_stmts_to_one_stmt(self, stmts: List[ast.stmt], filler=None) -> Union[ast.If, ast.Return]:
1251
+ """
1252
+ List of if statements will be converted to one if statement.
1253
+ For example:
1254
+ if tick['X'] <= 1:
1255
+ if tick['X'] > 0:
1256
+ return 1
1257
+ else:
1258
+ pass
1259
+ else:
1260
+ if tick['X'] < 3:
1261
+ return 2
1262
+ if tick['X'] <= 3:
1263
+ return 3
1264
+ return 4
1265
+ will be converted to:
1266
+ if tick['X'] <= 1:
1267
+ if tick['X'] > 0:
1268
+ return 1
1269
+ else:
1270
+ if tick['X'] <= 3:
1271
+ return 3
1272
+ else:
1273
+ return 4
1274
+ else:
1275
+ if tick['X'] < 3:
1276
+ return 2
1277
+ else:
1278
+ if tick['X'] <= 3:
1279
+ return 3
1280
+ else:
1281
+ return 4
1282
+ """
1283
+ filler = filler or ast.Pass()
1284
+ if not stmts:
1285
+ return filler
1286
+ stmt, *others = stmts
1287
+ if isinstance(stmt, ast.Return):
1288
+ return stmt
1289
+ if isinstance(stmt, ast.Pass):
1290
+ return filler
1291
+ filler = self._compress_stmts_to_one_stmt(others, filler=filler)
1292
+ if isinstance(stmt, ast.If):
1293
+ stmt.body = [self._compress_stmts_to_one_stmt(stmt.body, filler=filler)]
1294
+ if stmt.orelse:
1295
+ stmt.orelse = [self._compress_stmts_to_one_stmt(stmt.orelse, filler=filler)]
1296
+ elif filler:
1297
+ stmt.orelse = [filler]
1298
+ assert stmt.orelse
1299
+ return stmt
1300
+ raise ValueError(
1301
+ "this function can't be converted to CASE function, "
1302
+ "only for, if, return and pass statements are allowed"
1303
+ )
1304
+
1305
+ def _replace_local_variables(self, stmts: List[ast.stmt]) -> List[ast.stmt]:
1306
+ """
1307
+ We support local variables only by calculating their value and
1308
+ replacing all it's occurrences in the code after variable definition.
1309
+ For example:
1310
+ a = 12345
1311
+ if a:
1312
+ return a
1313
+ return 0
1314
+ will be converted to:
1315
+ if 12345:
1316
+ return 12345
1317
+ return 0
1318
+ """
1319
+ replace_name = {}
1320
+ res_stmts = []
1321
+ for stmt in stmts:
1322
+ if isinstance(stmt, ast.Assign):
1323
+ assert len(stmt.targets) == 1, 'Unpacking local variables is not supported yet'
1324
+ var, val = stmt.targets[0], stmt.value
1325
+ assert isinstance(var, ast.Name)
1326
+ replace_name[var.id] = val
1327
+ continue
1328
+ res_stmts.append(
1329
+ self._replace_nodes(stmt, replace_name=replace_name)
1330
+ )
1331
+ return res_stmts
1332
+
1333
+ def if_stmt(self, stmt: ast.If) -> ast.IfExp:
1334
+ """
1335
+ Classic if statement with limited set of allowed statements in the body:
1336
+ * only one statement in the body
1337
+ * statement can be return or another if statement with same rules as above
1338
+
1339
+ For example:
1340
+ if tick['X'] > 0:
1341
+ return 'POS'
1342
+ elif tick['X'] == 0:
1343
+ return 'ZERO'
1344
+ else:
1345
+ return 'NEG'
1346
+ will be converted to OneTick's CASE function:
1347
+ CASE(X > 0, 1, 'POS', CASE(X = 0, 1, 'ZERO', 'NEG'))
1348
+ """
1349
+ # TODO: support many statements in body
1350
+ if len(stmt.body) != 1:
1351
+ raise ValueError("this function can't be converted to CASE function, "
1352
+ "too many statements in if body")
1353
+ body = self.statement(stmt.body[0])
1354
+
1355
+ if len(stmt.orelse) > 1:
1356
+ raise ValueError("this function can't be converted to CASE function, "
1357
+ "too many statements in else body")
1358
+ if stmt.orelse and not isinstance(stmt.orelse[0], ast.Pass):
1359
+ orelse = self.statement(stmt.orelse[0])
1360
+ else:
1361
+ e = self.expression_parser.expression(body)
1362
+ orelse = ast.Constant(ott.default_by_type(ott.get_type_by_objects(e.values)))
1363
+ return ast.IfExp(test=stmt.test, body=body, orelse=orelse)
1364
+
1365
+ def return_stmt(self, stmt: ast.Return) -> ast.expr:
1366
+ """
1367
+ Return statement.
1368
+ Will be converted to value according to OneTick's syntax.
1369
+ """
1370
+ if stmt.value is None:
1371
+ raise ValueError('return statement must have value when converting to CASE function')
1372
+ return stmt.value
1373
+
1374
+ def pass_stmt(self, _stmt: ast.Pass) -> ast.Constant:
1375
+ """
1376
+ Pass statement.
1377
+ Will be converted to None according to OneTick's syntax.
1378
+ """
1379
+ return ast.Constant(None)
1380
+
1381
+ def compress(self, stmts: List[ast.stmt]) -> Union[ast.If, ast.Return]:
1382
+ """
1383
+ Compress list of statements to single statement.
1384
+ This is possible only if simple if and return statements are used.
1385
+ """
1386
+ stmts = self._replace_local_variables(stmts)
1387
+ flat_stmts = self._flatten_for_stmts(stmts)
1388
+ stmt = self._compress_stmts_to_one_stmt(flat_stmts) # type: ignore[arg-type]
1389
+ return stmt
1390
+
1391
+ def statement(self, stmt: ast.stmt) -> ast.expr:
1392
+ """Return statement converted to expression."""
1393
+ return {
1394
+ ast.If: self.if_stmt,
1395
+ ast.Return: self.return_stmt,
1396
+ ast.Pass: self.pass_stmt,
1397
+ }[type(stmt)](stmt) # type: ignore[operator]
1398
+
1399
+
1400
+ class StatementParser(CaseStatementParser):
1401
+ """
1402
+ Class with methods to convert ast statements to per tick script lines.
1403
+ """
1404
+
1405
+ def __init__(self, fun: 'FunctionParser'):
1406
+ super().__init__(fun)
1407
+ self.expression_parser = ExpressionParser(fun=fun) # type: ignore[assignment]
1408
+ self.operator_parser = OperatorParser()
1409
+ self._for_counter = 0
1410
+
1411
+ @staticmethod
1412
+ def _transform_if_expr_to_if_stmt(stmt: Union[ast.Assign, ast.AugAssign]) -> ast.If:
1413
+ """
1414
+ Per tick script do not support if expressions, so converting it to if statement.
1415
+ For example:
1416
+ tick['X'] = 'A' if tick['S'] > 0 else 'B'
1417
+ will be converted to:
1418
+ if (S > 0) {
1419
+ X = 'A';
1420
+ }
1421
+ else {
1422
+ X = 'B';
1423
+ }
1424
+ """
1425
+ if not isinstance(stmt.value, ast.IfExp):
1426
+ raise ValueError()
1427
+ if_expr: ast.IfExp = stmt.value
1428
+ body, orelse = deepcopy(stmt), deepcopy(stmt)
1429
+ body.value = if_expr.body
1430
+ orelse.value = if_expr.orelse
1431
+
1432
+ return ast.If(
1433
+ test=if_expr.test,
1434
+ body=[body],
1435
+ orelse=[orelse],
1436
+ )
1437
+
1438
+ def assign(self, stmt: ast.Assign) -> str:
1439
+ """
1440
+ Assign statement: tick['X'] = 1
1441
+ Will be converted to OneTick syntax: X = 1;
1442
+ """
1443
+ assert len(stmt.targets) == 1, 'Unpacking variables is not yet supported'
1444
+
1445
+ if isinstance(stmt.value, ast.IfExp):
1446
+ if_stmt = self._transform_if_expr_to_if_stmt(stmt)
1447
+ return self.statement(if_stmt)
1448
+
1449
+ var = self.expression_parser.expression(stmt.targets[0])
1450
+ val = self.expression_parser.expression(stmt.value)
1451
+
1452
+ default_expr = f'{var} = {val};'
1453
+
1454
+ if var.lhs:
1455
+ expr = var.value(val.value)
1456
+ return expr or default_expr
1457
+
1458
+ if var.is_local_variable:
1459
+ var_name = var.value.name
1460
+ if val.is_static:
1461
+ val = Expression(val.value.value)
1462
+ if var_name in self.fun.emulator.STATIC_VARS:
1463
+ raise ValueError(f"Trying to define static variable '{var_name}' more than once")
1464
+ if var_name in self.fun.emulator.LOCAL_VARS:
1465
+ raise ValueError(f"Can't redefine variable '{var_name}' as static")
1466
+
1467
+ if self.fun.emulator.NEW_VALUES:
1468
+ raise ValueError('Mixed definition of static variables and new columns is not supported')
1469
+
1470
+ if val.is_tick:
1471
+ # recreating tick object here, because it doesn't have name yet
1472
+ self.fun.emulator.STATIC_VARS[var_name] = val.dtype(var_name)
1473
+ return f'static {val.value._definition} {var};'
1474
+ self.fun.emulator.STATIC_VARS[var_name] = val.value
1475
+ return f'static {ott.type2str(val.dtype)} {var} = {val};'
1476
+
1477
+ variables = None
1478
+ if var_name in self.fun.emulator.STATIC_VARS:
1479
+ variables = self.fun.emulator.STATIC_VARS
1480
+ elif var_name in self.fun.emulator.LOCAL_VARS:
1481
+ variables = self.fun.emulator.LOCAL_VARS
1482
+
1483
+ if variables is None:
1484
+ if val.is_tick:
1485
+ raise ValueError('Only primitive types are allowed for non static local variables.')
1486
+ if self.fun.emulator.NEW_VALUES:
1487
+ raise ValueError('Mixed definition of local variables and new columns is not supported')
1488
+ self.fun.emulator.LOCAL_VARS[var_name] = val.value
1489
+ return f'{ott.type2str(val.dtype)} {var} = {val};'
1490
+
1491
+ dtype = ott.get_type_by_objects([variables[var_name]])
1492
+ if val.dtype != dtype:
1493
+ raise ValueError(f"Wrong type for variable '{var_name}': should be {dtype}, got {val.dtype}")
1494
+
1495
+ return default_expr
1496
+
1497
+ def aug_assign(self, stmt: ast.AugAssign) -> str:
1498
+ """
1499
+ Assign with inplace operation statement: tick['X'] += 1.
1500
+ Will be converted to OneTick syntax: X = X + 1;
1501
+ """
1502
+ target = deepcopy(stmt.target)
1503
+ target.ctx = ast.Load()
1504
+ return self.assign(
1505
+ ast.Assign(
1506
+ targets=[stmt.target],
1507
+ value=ast.BinOp(
1508
+ left=target,
1509
+ op=stmt.op,
1510
+ right=stmt.value,
1511
+ )
1512
+ )
1513
+ )
1514
+
1515
+ def if_stmt(self, stmt: ast.If) -> str: # type: ignore[override]
1516
+ """
1517
+ Classic if statement:
1518
+ if tick['X'] > 0:
1519
+ tick['Y'] = 1
1520
+ elif tick['X'] == 0:
1521
+ tick['Y'] = 0
1522
+ else:
1523
+ tick['Y'] = -1
1524
+ Will be converted to:
1525
+ if (X > 0) {
1526
+ Y = 1;
1527
+ }
1528
+ else {
1529
+ if (X == 0) {
1530
+ Y = 0;
1531
+ }
1532
+ else {
1533
+ Y = -1;
1534
+ }
1535
+ }
1536
+ """
1537
+ test = self.expression_parser.expression(stmt.test)
1538
+ test.convert_to_operation()
1539
+ body = [self.statement(s) for s in stmt.body]
1540
+ orelse = [self.statement(s) for s in stmt.orelse]
1541
+ if test.predefined:
1542
+ if test.value:
1543
+ return '\n'.join(body)
1544
+ return '\n'.join(orelse)
1545
+ lines = []
1546
+ lines.append('if (%s) {' % test)
1547
+ lines.extend(body)
1548
+ lines.append('}')
1549
+ if orelse:
1550
+ lines.append('else {')
1551
+ lines.extend(orelse)
1552
+ lines.append('}')
1553
+ return '\n'.join(lines)
1554
+
1555
+ def return_stmt(self, stmt: ast.Return) -> str: # type: ignore[override]
1556
+ """
1557
+ Return statement.
1558
+ For now we support returning only boolean values or nothing.
1559
+ Will be converted to: return true;
1560
+ """
1561
+ # if return is empty then it is not filter
1562
+ v = stmt.value if stmt.value is not None else ast.Constant(value=True)
1563
+ value = self.expression_parser.expression(v)
1564
+ dtype = ott.get_object_type(value.value)
1565
+ if not self.fun.inner_function:
1566
+ if dtype is not bool:
1567
+ raise TypeError(f"Not supported return type {dtype}")
1568
+ if stmt.value is not None:
1569
+ self.fun.returns = True
1570
+ else:
1571
+ assert isinstance(self.fun.ast_node, ast.FunctionDef)
1572
+ msg = (f"Function '{self.fun.ast_node.name}'"
1573
+ f" has return annotation '{self.fun.return_annotation.__name__}',"
1574
+ f" but the type of statement ({astunparse(stmt)}) is '{dtype.__name__}'")
1575
+ try:
1576
+ widest_type = ott.get_type_by_objects([dtype, self.fun.return_annotation])
1577
+ except TypeError as e:
1578
+ raise TypeError(msg) from e
1579
+ if widest_type is not self.fun.return_annotation:
1580
+ raise TypeError(msg)
1581
+ self.fun.returns = True
1582
+ return f'return {value};'
1583
+
1584
+ @staticmethod
1585
+ def _check_break(*nodes) -> bool:
1586
+ """
1587
+ Check if break statement in the list of nodes (recursively).
1588
+ """
1589
+ class FoundBreakException(Exception):
1590
+ pass
1591
+
1592
+ class FindBreak(ast.NodeVisitor):
1593
+ def visit_Break(self, n: ast.Break):
1594
+ raise FoundBreakException()
1595
+
1596
+ try:
1597
+ for node in nodes:
1598
+ FindBreak().visit(node)
1599
+ return False
1600
+ except FoundBreakException:
1601
+ return True
1602
+
1603
+ def while_stmt(self, stmt: ast.While) -> str:
1604
+ """
1605
+ Classic while statement:
1606
+ while tick['X'] > 0:
1607
+ tick['Y'] = 1
1608
+ Will be converted to:
1609
+ while (X > 0) {
1610
+ Y = 1;
1611
+ }
1612
+ """
1613
+ test = self.expression_parser.expression(stmt.test)
1614
+ test.convert_to_operation()
1615
+ body = [self.statement(s) for s in stmt.body]
1616
+ if test.predefined:
1617
+ is_break_found = self._check_break(*stmt.body)
1618
+ if not is_break_found:
1619
+ raise ValueError(f'The condition of while statement always evaluates to {bool(test.value)}'
1620
+ ' and there is no break statement in the loop body.'
1621
+ ' That will result in infinite loop.'
1622
+ ' Change condition or add break statements.')
1623
+ lines = []
1624
+ lines.append('while (%s) {' % test)
1625
+ lines.extend(body)
1626
+ lines.append('}')
1627
+ return '\n'.join(lines)
1628
+
1629
+ def for_stmt(self, stmt: ast.For) -> str:
1630
+ """
1631
+ For now for statement in most cases will not be converted to per tick script's for statement.
1632
+ Instead, the statements from the body of the for statement will be duplicated
1633
+ for each iteration.
1634
+
1635
+ For example:
1636
+ for i in [1, 2, 3]:
1637
+ tick['X'] += i
1638
+ will be converted to:
1639
+ X += 1;
1640
+ X += 2;
1641
+ X += 3;
1642
+
1643
+ But simple case with range object will be translated more correctly:
1644
+ For example:
1645
+ for i in range(1, 4):
1646
+ tick['X'] += i
1647
+ will be converted to:
1648
+ for (LOCAL::i = 1; LOCAL::i < 4; LOCAL::i += 1) {
1649
+ X += LOCAL::i;
1650
+ }
1651
+ """
1652
+ lines = []
1653
+ iter_ = self.expression_parser.expression(stmt.iter)
1654
+ if isinstance(iter_.value, _TickSequence):
1655
+ target = stmt.target
1656
+ assert isinstance(target, ast.Name), "Tuples can't be used while iterating on tick sequences"
1657
+ state_tick = iter_.value._tick_obj(target.id)
1658
+ # TODO: ugly
1659
+ state_tick_name = f"_______state_tick_{self._for_counter}_______"
1660
+ self._for_counter += 1
1661
+ ast_tick = ast.Name(state_tick_name, ctx=ast.Load())
1662
+ lines.append('for (%s %s : %s) {' % (state_tick._definition, state_tick, iter_.value))
1663
+ with self.expression_parser._replace_context(
1664
+ inspect.ClosureVars({}, {state_tick_name: state_tick}, {}, set())
1665
+ ):
1666
+ for s in stmt.body:
1667
+ s = self._replace_nodes(s, replace_name={target.id: ast_tick})
1668
+ lines.append(self.statement(s))
1669
+ lines.append('}')
1670
+ elif isinstance(iter_.value, range):
1671
+ target = stmt.target
1672
+ assert isinstance(target, ast.Name), "Tuples can't be used in for loop"
1673
+ var_name = target.id
1674
+ if var_name not in self.fun.emulator.LOCAL_VARS:
1675
+ # initialize counter variable
1676
+ self.fun.emulator.LOCAL_VARS[var_name] = int
1677
+ self.fun.emulator.LOCAL_VARS_NEW_VALUES[var_name].append(0)
1678
+ elif self.fun.emulator.LOCAL_VARS[var_name] is not int:
1679
+ raise ValueError(f'Variable {var_name} was declared before with conflicting type.')
1680
+ counter_var = LocalVariable(var_name, int)
1681
+ counter_var_str = str(counter_var)
1682
+ range_obj = iter_.value
1683
+ start_expr = f'{counter_var_str} = {range_obj.start}'
1684
+ if (range_obj.start < range_obj.stop and range_obj.step <= 0 or
1685
+ range_obj.start > range_obj.stop and range_obj.step >= 0):
1686
+ raise ValueError(f'Range object {range_obj} will result in infinite loop')
1687
+ if range_obj.start < range_obj.stop:
1688
+ condition_expr = f'{counter_var_str} < {range_obj.stop}'
1689
+ else:
1690
+ condition_expr = f'{counter_var_str} > {range_obj.stop}'
1691
+ increment_expr = f'{counter_var_str} += {range_obj.step}'
1692
+ lines.append('for (%s; %s; %s) {' % (start_expr, condition_expr, increment_expr))
1693
+ try:
1694
+ for s in stmt.body:
1695
+ lines.append(self.statement(s))
1696
+ lines.append('}')
1697
+ except Exception:
1698
+ if var_name in self.fun.emulator.LOCAL_VARS_NEW_VALUES:
1699
+ self.fun.emulator.LOCAL_VARS_NEW_VALUES.pop(var_name)
1700
+ self.fun.emulator.LOCAL_VARS.pop(var_name)
1701
+ lines = [self.statement(s) for s in self._flatten_for_stmt(stmt)]
1702
+ else:
1703
+ lines = [self.statement(s) for s in self._flatten_for_stmt(stmt)]
1704
+ return '\n'.join(lines)
1705
+
1706
+ def break_stmt(self, _stmt: ast.Break) -> str:
1707
+ return 'break;'
1708
+
1709
+ def continue_stmt(self, _stmt: ast.Continue) -> str:
1710
+ return 'continue;'
1711
+
1712
+ def pass_stmt(self, stmt: ast.Pass) -> str: # type: ignore[override]
1713
+ """Pass statement is not converted to anything"""
1714
+ return ''
1715
+
1716
+ def yield_expr(self, expr: ast.Yield) -> Expression:
1717
+ """
1718
+ Yield expression, like: yield
1719
+ Values for yield are not supported.
1720
+ Will be translated to PROPAGATE_TICK() function.
1721
+ Can be used only as a statement, so this function is here and not in ExpressionParser.
1722
+ """
1723
+ if expr.value is not None:
1724
+ raise ValueError("Passing value with yield expression is not supported.")
1725
+ return Expression('PROPAGATE_TICK();')
1726
+
1727
+ def expression(self, stmt: ast.Expr) -> str:
1728
+ """
1729
+ Here goes raw strings and yield expression.
1730
+ For example:
1731
+ if tick['A'] == 0:
1732
+ 'return 0;'
1733
+ Here 'return 0;' is used as a statement and an expression.
1734
+ Expression's returned value *must* be a string and
1735
+ this string will be injected in per tick script directly.
1736
+ """
1737
+ if isinstance(stmt.value, ast.Yield):
1738
+ expression = self.yield_expr(stmt.value)
1739
+ else:
1740
+ expression = self.expression_parser.expression(stmt.value)
1741
+ assert isinstance(expression.value, (str, _Operation)), (
1742
+ f"The statement '{astunparse(stmt)}' can't be used here"
1743
+ " because the value of such statement can be string only"
1744
+ " as it's value will be injected directly in per tick script."
1745
+ )
1746
+ value = str(expression.value)
1747
+ if value and value[-1] != ';':
1748
+ value += ';'
1749
+ return value
1750
+
1751
+ def with_stmt(self, stmt: ast.With) -> str:
1752
+ """
1753
+ Used only with special context managers. Currently only `_ONCE` is supported.
1754
+ """
1755
+ if len(stmt.items) != 1:
1756
+ raise ValueError('Currently it is possible to use only one context manager in single with statement')
1757
+ with_item = stmt.items[0]
1758
+ if with_item.optional_vars:
1759
+ raise ValueError('It is not allowed to use "as" in with statements for per-tick script')
1760
+ context_expr = with_item.context_expr
1761
+ if isinstance(context_expr, ast.Call):
1762
+ expr = self.expression_parser.expression(context_expr.func)
1763
+ else:
1764
+ raise ValueError(f'{context_expr} is not called')
1765
+ if not issubclass(expr.value, once):
1766
+ raise ValueError(f'{expr.value} is not supported in per-tick script with statements')
1767
+ return expr.value().get_str('\n'.join([self.statement(s) for s in stmt.body]))
1768
+
1769
+ def statement(self, stmt: ast.stmt) -> str: # type: ignore[override]
1770
+ """Return parsed statement according to its type."""
1771
+ return {
1772
+ ast.Assign: self.assign,
1773
+ ast.AugAssign: self.aug_assign,
1774
+ ast.If: self.if_stmt,
1775
+ ast.Return: self.return_stmt,
1776
+ ast.While: self.while_stmt,
1777
+ ast.For: self.for_stmt,
1778
+ ast.Break: self.break_stmt,
1779
+ ast.Continue: self.continue_stmt,
1780
+ ast.Pass: self.pass_stmt,
1781
+ ast.Expr: self.expression,
1782
+ ast.With: self.with_stmt,
1783
+ }[type(stmt)](stmt) # type: ignore[operator]
1784
+
1785
+
1786
+ class EndOfBlock(Exception):
1787
+ pass
1788
+
1789
+
1790
+ class LambdaBlockFinder:
1791
+ """
1792
+ This is simplified version of
1793
+ inspect.BlockFinder
1794
+ that supports multiline lambdas.
1795
+ """
1796
+
1797
+ OPENING_BRACKETS = {
1798
+ '[': ']',
1799
+ '(': ')',
1800
+ '{': '}',
1801
+ }
1802
+ CLOSING_BRACKETS = {c: o for o, c in OPENING_BRACKETS.items()}
1803
+ BRACKETS_MATCHING = dict(OPENING_BRACKETS.items() | CLOSING_BRACKETS.items())
1804
+
1805
+ def __init__(self):
1806
+ # current indentation level
1807
+ self.indent = 0
1808
+ # row and column index for the start of lambda expression
1809
+ self.start = None
1810
+ # row and column index for the end of lambda expression
1811
+ self.end = None
1812
+ # stack with brackets
1813
+ self.brackets = deque()
1814
+ self.prev = None
1815
+ self.current = None
1816
+
1817
+ def tokeneater(self, type, token, srowcol, erowcol, line, start_row=0):
1818
+ srowcol = (srowcol[0] + start_row, srowcol[1])
1819
+ erowcol = (erowcol[0] + start_row, erowcol[1])
1820
+ self.prev = self.current
1821
+ self.current = tokenize.TokenInfo(type, token, srowcol, erowcol, line)
1822
+ self.end = erowcol
1823
+ if token == 'lambda':
1824
+ self.start = srowcol
1825
+ elif type == tokenize.INDENT:
1826
+ self.indent += 1
1827
+ elif type == tokenize.DEDENT:
1828
+ self.indent -= 1
1829
+ # the end of matching indent/dedent pairs ends a block
1830
+ if self.indent <= 0:
1831
+ raise EndOfBlock
1832
+ elif not self.start:
1833
+ self.indent = 0
1834
+ elif type == tokenize.NEWLINE:
1835
+ if self.indent == 0 or (
1836
+ # if lambda is the argument of the function
1837
+ self.prev and self.prev.type == tokenize.OP and self.prev.string == ','
1838
+ ):
1839
+ raise EndOfBlock
1840
+ elif token in self.OPENING_BRACKETS:
1841
+ self.brackets.append(token)
1842
+ elif token in self.CLOSING_BRACKETS:
1843
+ try:
1844
+ assert self.brackets.pop() == self.CLOSING_BRACKETS[token]
1845
+ except (IndexError, AssertionError):
1846
+ self.end = self.prev.end
1847
+ raise EndOfBlock # noqa: W0707
1848
+
1849
+
1850
+ def get_lambda_source(lines):
1851
+ """Extract the block of lambda code at the top of the given list of lines."""
1852
+ blockfinder = LambdaBlockFinder()
1853
+ start_row = 0
1854
+ while True:
1855
+ try:
1856
+ tokens = tokenize.generate_tokens(iter(lines[start_row:]).__next__)
1857
+ for _token in tokens:
1858
+ blockfinder.tokeneater(*_token, start_row=start_row)
1859
+ break
1860
+ except IndentationError as e:
1861
+ # indentation errors are possible because
1862
+ # we started eating tokens from line with lambda
1863
+ # not from the start of the statement
1864
+ # trying to eat again from the current row in this case
1865
+ start_row = e.args[1][1] - 1
1866
+ continue
1867
+ except EndOfBlock:
1868
+ break
1869
+ start_row, start_column = blockfinder.start
1870
+ end_row, end_column = blockfinder.end
1871
+ # crop block to get rid of tokens from the context around lambda
1872
+ lines = lines[start_row - 1: end_row]
1873
+ lines[-1] = lines[-1][:end_column]
1874
+ lines[0] = lines[0][start_column:]
1875
+ # add brackets around lambda in case it is multiline lambda
1876
+ return ''.join(['(', *lines, ')'])
1877
+
1878
+
1879
+ def is_lambda(lambda_f) -> bool:
1880
+ return isinstance(lambda_f, types.LambdaType) and lambda_f.__name__ == '<lambda>'
1881
+
1882
+
1883
+ def get_source(lambda_f) -> str:
1884
+ """
1885
+ Get source code of the function or lambda.
1886
+ """
1887
+ if is_lambda(lambda_f):
1888
+ # that's a hack for multiline lambdas in brackets
1889
+ # inspect.getsource parse them wrong
1890
+ source_lines, lineno = inspect.findsource(lambda_f)
1891
+ if 'lambda' not in source_lines[lineno]:
1892
+ # inspect.findsource fails sometimes too
1893
+ lineno = lambda_f.__code__.co_firstlineno + 1
1894
+ while 'lambda' not in source_lines[lineno]:
1895
+ lineno -= 1
1896
+ source = get_lambda_source(source_lines[lineno:])
1897
+ else:
1898
+ source = inspect.getsource(lambda_f)
1899
+ # doing dedent because self.ast_node do not like indented source code
1900
+ return textwrap.dedent(source)
1901
+
1902
+
1903
+ class FunctionParser:
1904
+ """
1905
+ Class to parse callable objects (lambdas and functions) to
1906
+ OneTick's per tick script or case functions.
1907
+ Only simple functions corresponding to OneTick syntax supported
1908
+ (without inner functions, importing modules, etc.)
1909
+ You can call simple functions inside,
1910
+ do operations with captured variables (without assigning to them),
1911
+ but using non-pure functions is not recommended because
1912
+ the code in function may not be executed in the order you expect.
1913
+ """
1914
+ SOURCE_CODE_ATTRIBUTE = '___SOURCE_CODE___'
1915
+
1916
+ def __init__(self, lambda_f, emulator=None, check_arg_name=True, inner_function=False):
1917
+ """
1918
+ Parameters
1919
+ ----------
1920
+ emulator
1921
+ otp.Source emulator that will be tracking changes made to source
1922
+ check_arg_name
1923
+ if True, only callables with zero or one parameter will be allowed.
1924
+ inner_function
1925
+ if True, then function is treated like inner per-tick script function.
1926
+ First argument will be checked and more arguments will be allowed too.
1927
+ """
1928
+
1929
+ assert isinstance(lambda_f, (types.LambdaType, types.FunctionType, types.MethodType)), (
1930
+ f"It is expected to get a function, method or lambda, but got '{type(lambda_f)}'"
1931
+ )
1932
+ self.lambda_f = lambda_f
1933
+ self.emulator = emulator
1934
+ self.check_arg_name = check_arg_name
1935
+ self.inner_function = inner_function
1936
+ if self.inner_function and not self.check_arg_name:
1937
+ self.check_arg_name = self.check_arg_name or True
1938
+ self.statement_parser = StatementParser(fun=self)
1939
+ self.expression_parser = ExpressionParser(fun=self)
1940
+ self.case_expression_parser = CaseExpressionParser(fun=self)
1941
+ self.case_statement_parser = CaseStatementParser(fun=self)
1942
+ # if the function returns some values or not
1943
+ self.returns = False
1944
+ # will be set to True when args_annotations will be calculated, need it to break recursion
1945
+ self._from_args_annotations = False
1946
+ # calling property here, so we can raise exception as early as possible
1947
+ _ = self.arg_name
1948
+
1949
+ @cached_property
1950
+ def is_method(self) -> bool:
1951
+ return isinstance(self.lambda_f, types.MethodType)
1952
+
1953
+ @cached_property
1954
+ def source_code(self) -> str:
1955
+ """
1956
+ Get source code of the function or lambda.
1957
+ """
1958
+ # first try to get code from special attribute else get code the usual way
1959
+ return getattr(self.lambda_f, self.SOURCE_CODE_ATTRIBUTE, None) or get_source(self.lambda_f)
1960
+
1961
+ @cached_property
1962
+ def closure_vars(self) -> inspect.ClosureVars:
1963
+ """
1964
+ Get closure variables of the function.
1965
+ These are variables that were captured from the context before function definition.
1966
+ For example:
1967
+ A = 12345
1968
+ def a():
1969
+ print(A + 1)
1970
+ In this function variable A is the captured variable.
1971
+ We need closure variables, so we can use them when parsing ast tree.
1972
+ """
1973
+ return inspect.getclosurevars(self.lambda_f)
1974
+
1975
+ @cached_property
1976
+ def ast_node(self) -> Union[ast.FunctionDef, ast.Lambda]:
1977
+ """
1978
+ Convert function or lambda to ast module statement.
1979
+ """
1980
+ source_code = self.source_code
1981
+ tree = ast.parse(source_code)
1982
+ for node in ast.walk(tree):
1983
+ if isinstance(node, (ast.FunctionDef, ast.Lambda)):
1984
+ if isinstance(node, ast.FunctionDef) and ast.get_docstring(node):
1985
+ # remove comment section from function body
1986
+ node.body.pop(0)
1987
+ return node
1988
+ raise ValueError("Can't find function or lambda in source code")
1989
+
1990
+ @cached_property
1991
+ def arg_name(self) -> Optional[str]:
1992
+ """Get name of the first function or lambda argument."""
1993
+ node = self.ast_node
1994
+ argv = list(node.args.args)
1995
+ argc = len(argv)
1996
+ if argc > 1 and argv[0].arg == 'self' and self.is_method:
1997
+ argv.pop(0)
1998
+ argc -= 1
1999
+ if self.check_arg_name and argc > 1 and not self.inner_function:
2000
+ raise ValueError(
2001
+ "It is allowed to pass only functions or lambdas that take either one or"
2002
+ f" zero parameters, but got {argc}"
2003
+ )
2004
+ arg_name = argv[0].arg if argv else None
2005
+ if isinstance(self.check_arg_name, str) and arg_name != self.check_arg_name:
2006
+ assert isinstance(node, ast.FunctionDef)
2007
+ msg = f"Function '{node.name}' is expected to have first argument named '{self.check_arg_name}'"
2008
+ if arg_name is None:
2009
+ msg += ', but no argument is found'
2010
+ else:
2011
+ msg += f", but argument with name '{arg_name}' is found"
2012
+ raise ValueError(msg)
2013
+ return arg_name
2014
+
2015
+ @cached_property
2016
+ def args_annotations(self) -> dict:
2017
+ node = self.ast_node
2018
+ if not isinstance(node, ast.FunctionDef):
2019
+ return {}
2020
+ argv = list(node.args.args)
2021
+ if argv and argv[0].arg == 'self' and self.is_method:
2022
+ argv.pop(0)
2023
+ if argv and argv[0].arg == self.arg_name:
2024
+ argv.pop(0)
2025
+ if node.args.defaults:
2026
+ raise ValueError("Default values for arguments are not supported"
2027
+ f" in per-tick script function '{node.name}'")
2028
+ annotations = {}
2029
+ for arg in argv:
2030
+ name = arg.arg
2031
+ annotation = getattr(arg, 'annotation', None)
2032
+ if not annotation:
2033
+ raise ValueError(f"Parameter '{name}' in function '{node.name}' doesn't have type annotation")
2034
+ # TODO: remove hacking
2035
+ self._from_args_annotations = True
2036
+ dtype = self.expression_parser.expression(annotation).value
2037
+ self._from_args_annotations = False
2038
+ if not ott.is_type_basic(dtype):
2039
+ raise ValueError(f"Parameter '{name}' in function '{node.name}' has unsupported type: {dtype}")
2040
+ annotations[name] = dtype
2041
+
2042
+ return annotations
2043
+
2044
+ @cached_property
2045
+ def return_annotation(self):
2046
+ node = self.ast_node
2047
+ annotation = getattr(node, 'returns')
2048
+ if not annotation:
2049
+ raise ValueError(f"Function '{node.name}' doesn't have return type annotation")
2050
+ dtype = self.expression_parser.expression(annotation).value
2051
+ if not ott.is_type_basic(dtype):
2052
+ raise ValueError(f"Function '{node.name}' has unsupported return type: {dtype}")
2053
+ return dtype
2054
+
2055
+ def per_tick_script(self) -> str:
2056
+ """
2057
+ Convert function to OneTick's per tick script.
2058
+ """
2059
+ node = self.ast_node
2060
+
2061
+ lines = []
2062
+
2063
+ assert isinstance(node, ast.FunctionDef), 'lambdas are not supported in per-tick-script yet'
2064
+ function_def: ast.FunctionDef = node
2065
+
2066
+ for stmt in function_def.body:
2067
+ line = self.statement_parser.statement(stmt)
2068
+ if line:
2069
+ lines.append(line)
2070
+
2071
+ if not self.inner_function and self.returns:
2072
+ # if there were return statement anywhere in the code
2073
+ # then we add default return at the end
2074
+ # TODO: but the default behaviour in OneTick is to propagate all ticks?
2075
+ # changing that will break backward-compatibility
2076
+ lines.append(self.statement_parser.statement(ast.Return(ast.Constant(False))))
2077
+
2078
+ if self.emulator is not None and not self.inner_function:
2079
+ # per tick script syntax demand that we declare variables before using them
2080
+ # so we get all new variables from emulator and declare them.
2081
+
2082
+ def var_definition(key, values):
2083
+ dtype = ott.get_type_by_objects(values)
2084
+ return f'{ott.type2str(dtype)} {str(key)} = {ott.value2str(ott.default_by_type(dtype))};'
2085
+
2086
+ new_columns = []
2087
+ for key, values in self.emulator.NEW_VALUES.items():
2088
+ new_columns.append(var_definition(key, values))
2089
+ new_local_vars = []
2090
+ for key, values in self.emulator.LOCAL_VARS_NEW_VALUES.items():
2091
+ new_local_vars.append(var_definition(LocalVariable(key), values))
2092
+ lines = new_columns + new_local_vars + lines
2093
+
2094
+ if not lines:
2095
+ raise ValueError("The resulted body of PER TICK SCRIPT is empty")
2096
+
2097
+ if not self.inner_function:
2098
+ lines = ['long main() {'] + lines + ['}']
2099
+ for function, *_ in self.emulator.FUNCTIONS.values():
2100
+ lines.append(function)
2101
+ else:
2102
+ if not self.returns:
2103
+ raise ValueError(f"Function '{node.name}' must return values")
2104
+ return_type = ott.type2str(self.return_annotation)
2105
+ args = [
2106
+ f'{ott.type2str(dtype)} {name}'
2107
+ for name, dtype in self.args_annotations.items()
2108
+ ]
2109
+ args: str = ', '.join(args) # type: ignore[no-redef]
2110
+ lines = [f'{return_type} {node.name}({args})' + ' {'] + lines + ['}']
2111
+
2112
+ return '\n'.join(lines) + '\n'
2113
+
2114
+ def compress(self) -> ast.expr:
2115
+ """
2116
+ Convert lambda or function to AST expression.
2117
+ """
2118
+ node = self.ast_node
2119
+ if isinstance(node, ast.Lambda):
2120
+ return node.body
2121
+ stmt = self.case_statement_parser.compress(node.body)
2122
+ return self.case_statement_parser.statement(stmt)
2123
+
2124
+ def case(self) -> Tuple[str, List[Any]]:
2125
+ """
2126
+ Convert lambda or function to OneTick's CASE() function.
2127
+ """
2128
+ expr = self.compress()
2129
+ expr = self.case_expression_parser._convert_bool_op_to_if_expr(expr)
2130
+ expression = self.case_expression_parser.expression(expr)
2131
+ # this will raise type error if type of the expression is not supported
2132
+ ott.default_by_type(ott.get_type_by_objects(expression.values))
2133
+ return str(expression), expression.values
2134
+
2135
+
2136
+ def remote(fun):
2137
+ """
2138
+ This decorator is needed in case function ``fun``
2139
+ is used in :py:meth:`~onetick.py.Source.apply` method in a `Remote OTP with Ray` context.
2140
+
2141
+ We want to get source code of the function locally
2142
+ because we will not be able to get source code on the remote server.
2143
+
2144
+ See also
2145
+ --------
2146
+ :ref:`Remote OTP with Ray <ray-remote>`.
2147
+ """
2148
+ # see PY-424
2149
+ @wraps(fun)
2150
+ def wrapper(*args, **kwargs):
2151
+ return fun(*args, **kwargs)
2152
+ setattr(wrapper, FunctionParser.SOURCE_CODE_ATTRIBUTE, get_source(fun))
2153
+ return wrapper
2154
+
2155
+
2156
+ class once:
2157
+ """
2158
+ Used with a statement or a code block to make it run only once (the first time control reaches to the statement).
2159
+ """
2160
+ def __enter__(self):
2161
+ # __enter__ and __exit__ methods are only used to express syntax for per-tick script, thus no implementation
2162
+ pass
2163
+
2164
+ def __exit__(self, exc_type, exc_val, exc_tb):
2165
+ # __enter__ and __exit__ methods are only used to express syntax for per-tick script, thus no implementation
2166
+ pass
2167
+
2168
+ def get_str(self, string: str) -> str:
2169
+ return f"_ONCE\n{{\n{string}\n}}"
2170
+
2171
+
2172
+ class Once(once):
2173
+ def __init__(self):
2174
+ warnings.warn('Using `otp.Once` is deprecated, please, use `otp.once` instead', FutureWarning)
2175
+ super().__init__()
2176
+
2177
+
2178
+ def logf(message, severity, *args) -> str:
2179
+ """
2180
+ Call built-in OneTick ``LOGF`` function from per-tick script.
2181
+
2182
+ Parameters
2183
+ ----------
2184
+ message: str
2185
+ Log message/format string. The underlying formatting engine is the Boost Format Library:
2186
+ https://www.boost.org/doc/libs/1_53_0/libs/format/doc/format.html
2187
+
2188
+ severity: str
2189
+ Severity of message. Supported values: ``ERROR``, ``WARNING`` and ``INFO``.
2190
+
2191
+ args: list
2192
+ Parameters for format string (optional).
2193
+
2194
+ Returns
2195
+ -------
2196
+ str
2197
+
2198
+ Examples
2199
+ --------
2200
+ >>> t = otp.Ticks({'X': [1, 2, 3]})
2201
+
2202
+ >>> def test_script(tick):
2203
+ ... otp.logf("Tick with value X=%1% processed", "INFO", tick["X"])
2204
+
2205
+ >>> t = t.script(test_script)
2206
+
2207
+ See also
2208
+ --------
2209
+ :ref:`Per-Tick Script Guide <python callable parser>`
2210
+ """
2211
+
2212
+ if severity not in {"ERROR", "WARNING", "INFO"}:
2213
+ raise ValueError(f"Param severity expected to be one of ERROR, WARNING or INFO. Got \"{severity}\"")
2214
+
2215
+ message = ott.value2str(message)
2216
+ severity = ott.value2str(severity)
2217
+
2218
+ if args:
2219
+ params = ", ".join([ott.value2str(arg) for arg in args])
2220
+ return f"LOGF({message}, {severity}, {params});"
2221
+ else:
2222
+ return f"LOGF({message}, {severity});"
2223
+
2224
+
2225
+ def throw_exception(message: str) -> str:
2226
+ """
2227
+ Call built-in OneTick ``THROW_EXCEPTION`` function from per-tick script.
2228
+
2229
+ Parameters
2230
+ ----------
2231
+ message: str
2232
+ Message string that defines the error message to be thrown.
2233
+
2234
+ Returns
2235
+ -------
2236
+ str
2237
+
2238
+ Examples
2239
+ --------
2240
+ >>> t = otp.Ticks({'X': [1, -2, 6]})
2241
+
2242
+ >>> def test_script(tick):
2243
+ ... if tick["X"] <= 0:
2244
+ ... otp.throw_exception("Tick column X should be greater than zero.")
2245
+
2246
+ >>> t = t.script(test_script)
2247
+
2248
+ See also
2249
+ --------
2250
+ :ref:`Per-Tick Script Guide <python callable parser>`
2251
+ """
2252
+ message = ott.value2str(message)
2253
+ return f"THROW_EXCEPTION({message});"