-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.py
106 lines (76 loc) · 3.35 KB
/
expression.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
from abc import abstractmethod, ABCMeta
import pandas as pd
# 抽象表达式接口,根据业务场景规范表达式
# 使用抽象类需要使用python3
class Expression(metaclass=ABCMeta):
@abstractmethod
def interpret(self, context):
pass
# 构建变量表达式,或者叫终端表达式,其他表达式求值时通过层层追溯最后指向这里
# 变量与执行环境的Key对应,最终会通过key获取传入的值
class VarExpression(Expression):
def __init__(self, key):
Expression.__init__(self)
self.key = key
# 覆盖表达式,根据key获取变量
def interpret(self, context):
return context.get(self.key)
class SelectoneExpression(Expression):
def __init__(self, df, expr_one):
Expression.__init__(self)
self.df = df
self.expr_one = expr_one
def interpret(self, context):
dataframe = self.df.interpret(context)
if self.expr_one.interpret(context) == "*":
return dataframe
if "+" in self.expr_one.interpret(context):
return SelectaddExpression(self.df, self.expr_one).interpret(context)
if "*" in self.expr_one.interpret(context):
return SelectonemutipleExpression(self.df, self.expr_one).interpret(context)
else:
return dataframe[self.expr_one.interpret(context)]
class SelectaddExpression(Expression):
def __init__(self, df, expr_one):
Expression.__init__(self)
self.df = df
self.expr_one = expr_one
def interpret(self, context):
left, right = (self.expr_one.interpret(context).split("+"))
# print(left, right)
if left.isdigit():
self.num, self.column = int(left), right
else:
self.num, self.column = int(right), left
context.add('column'+str(self.column), self.column)
context.add('addnum'+str(self.num), self.num)
var_column = VarExpression('column'+str(self.column))
var_num = VarExpression('addnum'+str(self.num))
return SelectoneExpression(self.df, var_column).interpret(context) + var_num.interpret(context)
class SelectonemutipleExpression(Expression):
def __init__(self, df, expr_one):
Expression.__init__(self)
self.df = df
self.expr_one = expr_one
def interpret(self, context):
left, right = (self.expr_one.interpret(context).split("*"))
print(left, right)
if left.isdigit():
self.num, self.column = int(left), right
else:
self.num, self.column = int(right), left
context.add('column'+str(self.column), self.column)
context.add('num'+str(self.num), self.num)
var_column = VarExpression('column'+str(self.column))
var_num = VarExpression('num'+str(self.num))
return SelectoneExpression(self.df, var_column).interpret(context) * var_num.interpret(context)
class SelectaddanotherExpression(Expression):
def __init__(self, df, basedf, expr_another):
Expression.__init__(self)
self.df = df
self.basedf = basedf
self.expr_another = expr_another
self.SelectoneExpression = SelectoneExpression(df, expr_another)
def interpret(self, context):
basedataframe = self.basedf.interpret(context)
return pd.concat([basedataframe, self.SelectoneExpression.interpret(context)], axis=1)