-
Notifications
You must be signed in to change notification settings - Fork 233
/
expressions.py
698 lines (651 loc) · 20.8 KB
/
expressions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
import json
from rply import LexerGenerator
functions = {
# Aggregations
"MIN": {
"description": "Returns the minimum value in a numeric dataset.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "MIN(COL_NAME)",
"text": "MIN",
},
"MAX": {
"description": "Returns the maximum value in a numeric dataset.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "MIN(COL_NAME)",
"text": "MAX",
},
"MEAN": {
"description": "Returns the numerical average value in a dataset, ignoring text.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "MEAN(COL_NAME)",
"text": "MEAN",
},
"MODE": {
"description": "Returns the most commonly occurring value in a dataset.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "MODE(COL_NAME)",
"text": "MODE",
},
"STD": {
"description": "Calculates the standard deviation based on a sample.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "STD(COL_NAME)",
"text": "STD",
},
"SUM": {
"description": "Returns the sum of a series of numbers and/or cells.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "SUM(COL_NAME)",
"text": "SUM",
},
"VAR": {
"description": "Calculates the variance based on a sample.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "VAR(COL_NAME)",
"text": "VAR",
},
"KURTOSIS": {
"description": "Calculates the kurtosis of a dataset, which describes the shape, and in particular the 'peakedness' of that dataset.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "KURT(COL_NAME)",
"text": "KURT",
},
"SKEW": {
"description": "Calculates the skewness of a dataset, which describes the symmetry of that dataset about the mean.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value."
},
],
"example": "SKEW(COL_NAME)",
"text": "SKEW",
},
"MAD": {
"description": "Calculates the skewness of a dataset, which describes the symmetry of that dataset about the mean.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Column name or value"
},
],
"example": "MAD(COL_NAME)",
"text": "MAD",
},
# Math functions
"ABS": {
"description": "Returns the absolute value of a number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The number of which to return the absolute value."
},
],
"example": "ABS(-2)",
"text": "ABS",
},
"MOD": {
"description": "Returns the result of the modulo operator, the remainder after a division operation.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The number to be divided to find the remainder."
},
{
"type": "number",
"name": "divisor",
"description": "The number to divide by."
}
],
"example": "MOD(10, 4)",
"text": "MOD",
},
"EXP": {
"description": "Returns Euler's number, e (~2.718) raised to a power.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The number of which to return the absolute value."
},
],
"example": "ABS(-2)",
"text": "EXP",
},
"LOG": {
"description": "Returns the logarithm of a number with respect to a base.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the logarithm."
},
],
"example": "LOG(128, 2)",
"text": "LOG",
},
"LN": {
"description": "Returns the logarithm of a number, base e (Euler's number).",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the logarithm, base e."
},
],
"example": "LN(100)",
"text": "LN",
},
"POW": {
"description": "Returns a number raised to a power.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The number to raise to the exponent power."
},
{
"type": "number",
"name": "exponent",
"description": "The exponent to raise base to."
}
],
"example": "POW(4, 0.5)",
"text": "POW",
},
"CEIL": {
"description": "Rounds a number up to the nearest integer multiple of specified significance factor.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value to round up to the nearest integer multiple of factor."
},
],
"example": "CEILING(23.25)",
"text": "CEIL",
},
"SQRT": {
"description": "Returns the positive square root of a positive number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The number for which to calculate the positive square root."
},
],
"example": "SQRT(9)",
"text": "SQRT",
},
"FLOOR": {
"description": "Rounds a number down to the nearest integer multiple of specified significance factor.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value to round down to the nearest integer multiple of factor."
},
],
"example": "FLOOR(23.25)",
"text": "FLOOR",
},
"ROUND": {
"description": "Rounds a number to a certain number of decimal places according to standard rules.",
"parameters": [
{
"type": "series",
"name": "series",
"description": "The value to round to places number of places."
},
{
"type": "number",
"name": "decimals",
"description": "The value to round to places number of places."
}
],
"example": "ROUND(99.44, 1)",
"text": "ROUND",
},
# "RADIANS": "RADIANS function description", # Trigonometric Functions
# "DEGREES": "DEGREES function description",
"SIN": {
"description": "Returns the sin of an angle provided in radians.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The angle to find the sine of, in radians."
},
],
"example": "SIN(3.14)",
"text": "SIN",
},
"COS": {
"description": "Returns the cosine of an angle provided in radians.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The angle to find the cosine of, in radians."
},
],
"example": "COS(3.14)",
"text": "COS",
},
"TAN": {
"description": "Returns the tangent of an angle provided in radians.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The angle to find the tangent of, in radians."
},
],
"example": "TAN(3.14)",
"text": "TAN",
},
"ASIN": {
"description": "Returns the inverse sine of a value, in radians.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the inverse sine. Must be between -1 and 1, inclusive."
},
],
"example": "ASIN(0)",
"text": "ASIN",
},
"ACOS": {
"description": "Returns the inverse cosine of a value, in radians.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the inverse cosine. Must be between -1 and 1, inclusive."
},
],
"example": "ACOS(0)",
"text": "ACOS",
},
"ATAN": {
"description": "Returns the inverse tangent of a value, in radians.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the inverse tangent."
},
],
"example": "ATAN(0)",
"text": "ATAN",
},
"SINH": {
"description": "Returns the hyperbolic sine of any real number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Any real value to calculate the hyperbolic sine of."
},
],
"example": "SINH(2)",
"text": "SINH",
},
"COSH": {
"description": "Returns the hyperbolic cosine of any real number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Any real value to calculate the hyperbolic cosine of."
},
],
"example": "COSH(0.48)",
"text": "COSH",
},
"TANH": {
"description": "Returns the hyperbolic tangent of any real number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "Any real value to calculate the hyperbolic tangent of."
},
],
"example": "TANH(1)",
"text": "TANH",
},
"ASINH": {
"description": "Returns the hyperbolic tangent of any real number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the inverse hyperbolic sine."
},
],
"example": "ASINH(0.9)",
"text": "ASINH",
},
"ACOSH": {
"description": "Returns the inverse hyperbolic cosine of a number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the inverse hyperbolic cosine. Must be greater than or equal to 1."
},
],
"example": "ACOSH(2)",
"text": "ACOSH",
},
"ATANH": {
"description": "Returns the inverse hyperbolic tangent of a number.",
"parameters": [
{
"type": ["column", "number"],
"name": "col_name",
"description": "The value for which to calculate the inverse hyperbolic tangent. Must be between -1 and 1, exclusive."
},
],
"example": "ATANH(0.9)",
"text": "ATANH",
},
"UPPER": { # String Transformations
"description": "Returns the inverse hyperbolic tangent of a number.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "Converts a specified string to uppercase."
},
],
"example": "UPPER('lorem ipsum')",
"text": "UPPER",
},
"LOWER": {
"description": "Converts a specified string to lowercase.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The string to convert to lowercase."
},
],
"example": "LOWER('LOREM IPSUM')",
"text": "LOWER",
},
"PROPER": {
"description": "Capitalizes each word in a specified string.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The text which will be returned with the first letter of each word in uppercase and all other letters in lowercase."
},
],
"example": "PROPER('optimus prime')",
"text": "PROPER",
},
"TRIM": {
"description": "Removes leading, trailing, and repeated spaces in text.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The text or reference to a cell containing text to be trimmed."
},
],
"example": "TRIM('optimus prime')",
"text": "TRIM",
},
"REMOVE": "ATANH function description",
"LEN": {
"description": "Returns the length of a string.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The string whose length will be returned."
},
],
"example": "LEN('optimus prime')",
"text": "LEN",
},
"FIND": {
"description": "Returns the position at which a string is first found.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The string to look for."
},
{
"type": "string",
"name": "text_to_search",
"description": "The text to search for the first occurrence of search_for."
}
],
"example": "LEN('optimus prime')",
"text": "LEN",
},
"RFIND": "ATANH function description",
"LEFT": {
"description": "Returns a substring from the beginning of a specified string.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The string from which the left portion will be returned."
},
],
"example": "LEFT('Optimus', 2)",
"text": "LEN",
},
"RIGHT": {
"description": "Returns a substring from the end of a specified string.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The string from which the right portion will be returned."
},
],
"example": "LEN('optimus prime')",
"text": "LEN",
},
"STARTS_WITH": "ATANH function description",
"ENDS_WITH": "ATANH function description",
"EXACT": "ATANH function description",
"YEAR": { # Date Functions
"description": "Returns the year specified by a given date.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The date from which to extract the year."
},
],
"example": "YEAR()",
"text": "YEAR",
},
"MONTH": {
"description": "Returns the month of the year a specific date falls in, in numeric format.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The date from which to extract the month."
},
],
"example": "MONTH()",
"text": "MONTH",
},
"DAY": {
"description": "Returns the day of the month that a specific date falls on, in numeric format.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The date from which to extract the day."
},
],
"example": 'DAY()',
"text": "DAY",
},
"HOUR": {
"description": "Returns the hour component of a specific time, in numeric format.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The time from which to calculate the hour value."
},
],
"example": 'HOUR()',
"text": "HOUR",
},
"MINUTE": {
"description": "Returns the minute component of a specific time, in numeric format.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The time from which to calculate the minute value."
},
],
"example": 'MINUTE()',
"text": "MINUTE",
},
"SECOND": {
"description": "Returns the second component of a specific time, in numeric format.",
"parameters": [
{
"type": ["column", "string"],
"name": "col_name",
"description": "The time from which to calculate the second value"
},
],
"example": 'SECOND()',
"text": "SECOND",
},
}
unary_operators = {
"~": "~ function description",
"|": "| function description",
"&": "& function description",
"+": "+ function description",
"-": "- function description",
}
binary_operators = {
"+": "+ function description",
"-": "- function description",
"*": "* function description",
"/": "/ function description",
"%": "- function description"
}
# functions = op_functions
reserved_words = json.dumps(
{"functions": functions, "operators": {"unary": unary_operators, "binary": binary_operators}}, ensure_ascii=False)
"""
Parse an expression to optimus code
"""
lexer_generator = LexerGenerator()
lexer = lexer_generator.build()
l_g = lexer_generator
for f in functions:
rx = f'(?i){f}(?!\w)'
l_g.add(f, rx)
# Parenthesis
l_g.add('OPEN_PAREN', r'\(')
l_g.add('CLOSE_PAREN', r'\)')
# Semi Colon
l_g.add('SEMI_COLON', r'\;')
l_g.add('COMMA', r'\,')
l_g.add('IDENTIFIER', r'{(.*?)}') # Reference
l_g.add('IDENTIFIER', r'[^\W0-9 ]\w*') # Column names
l_g.add('STRINGS', r'"(.*?)"') # Reference
# Operators
l_g.add('SUM_OPERATOR', r'\+')
l_g.add('SUB_OPERATOR', r'\-')
l_g.add('MUL_OPERATOR', r'\*')
l_g.add('DIV_OPERATOR', r'\/')
# Number
l_g.add('FLOAT', r'[-+]?[0-9]*\.?[0-9]+')
l_g.add('INTEGER', r'\d+')
# Ignore spaces
l_g.ignore('\s+')
def parse(text_input, df_name="df"):
"""
:param text_input: string to be parsed
:param df_name: dataframe name
:return:
"""
tokens = lexer.lex(text_input)
result = []
for token in tokens:
token_value = token.value
if token.name == "IDENTIFIER":
for r in (("{", ""), ("}", "")):
token_value = token_value.replace(*r)
result_element = f"""{df_name}.data['{token_value}']"""
elif token.name in functions:
result_element = f"""{df_name}.functions.{token_value.lower()}"""
else:
result_element = token_value
result.append(result_element)
result = "".join(result)
return result