-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathoperator.py
More file actions
150 lines (126 loc) Β· 5 KB
/
operator.py
File metadata and controls
150 lines (126 loc) Β· 5 KB
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
import json
import math
from enum import Enum
class Condition(Enum):
EQUAL = "equal"
NOT_EQUAL = "notEqual"
GREATER_THAN = "greaterThan"
GREATER_THAN_EQUAL = "greaterThanEqual"
LESS_THAN = "lessThan"
LESS_THAN_EQUAL = "lessThanEqual"
CONTAINS = "contains"
IS_NULL = "isNull"
IS_NOT_NULL = "isNotNull"
class Operator():
def __init__(self, method, values=None):
self.method = method
if values is not None:
self.values = values if isinstance(values, list) else [values]
def __str__(self):
return json.dumps(
self.__dict__,
separators=(",", ":"),
default=lambda obj: obj.__dict__
)
@staticmethod
def increment(value=1, max=None):
if isinstance(value, float) and (math.isnan(value) or math.isinf(value)):
raise ValueError("Value cannot be NaN or Infinity")
if max is not None and isinstance(max, float) and (math.isnan(max) or math.isinf(max)):
raise ValueError("Max cannot be NaN or Infinity")
values = [value]
if max is not None:
values.append(max)
return str(Operator("increment", values))
@staticmethod
def decrement(value=1, min=None):
if isinstance(value, float) and (math.isnan(value) or math.isinf(value)):
raise ValueError("Value cannot be NaN or Infinity")
if min is not None and isinstance(min, float) and (math.isnan(min) or math.isinf(min)):
raise ValueError("Min cannot be NaN or Infinity")
values = [value]
if min is not None:
values.append(min)
return str(Operator("decrement", values))
@staticmethod
def multiply(factor, max=None):
if isinstance(factor, float) and (math.isnan(factor) or math.isinf(factor)):
raise ValueError("Factor cannot be NaN or Infinity")
if max is not None and isinstance(max, float) and (math.isnan(max) or math.isinf(max)):
raise ValueError("Max cannot be NaN or Infinity")
values = [factor]
if max is not None:
values.append(max)
return str(Operator("multiply", values))
@staticmethod
def divide(divisor, min=None):
if isinstance(divisor, float) and (math.isnan(divisor) or math.isinf(divisor)):
raise ValueError("Divisor cannot be NaN or Infinity")
if min is not None and isinstance(min, float) and (math.isnan(min) or math.isinf(min)):
raise ValueError("Min cannot be NaN or Infinity")
if divisor == 0:
raise ValueError("Divisor cannot be zero")
values = [divisor]
if min is not None:
values.append(min)
return str(Operator("divide", values))
@staticmethod
def modulo(divisor):
if isinstance(divisor, float) and (math.isnan(divisor) or math.isinf(divisor)):
raise ValueError("Divisor cannot be NaN or Infinity")
if divisor == 0:
raise ValueError("Divisor cannot be zero")
return str(Operator("modulo", [divisor]))
@staticmethod
def power(exponent, max=None):
if isinstance(exponent, float) and (math.isnan(exponent) or math.isinf(exponent)):
raise ValueError("Exponent cannot be NaN or Infinity")
if max is not None and isinstance(max, float) and (math.isnan(max) or math.isinf(max)):
raise ValueError("Max cannot be NaN or Infinity")
values = [exponent]
if max is not None:
values.append(max)
return str(Operator("power", values))
@staticmethod
def array_append(values):
return str(Operator("arrayAppend", values))
@staticmethod
def array_prepend(values):
return str(Operator("arrayPrepend", values))
@staticmethod
def array_insert(index, value):
return str(Operator("arrayInsert", [index, value]))
@staticmethod
def array_remove(value):
return str(Operator("arrayRemove", [value]))
@staticmethod
def array_unique():
return str(Operator("arrayUnique", []))
@staticmethod
def array_intersect(values):
return str(Operator("arrayIntersect", values))
@staticmethod
def array_diff(values):
return str(Operator("arrayDiff", values))
@staticmethod
def array_filter(condition, value=None):
values = [condition.value if isinstance(condition, Condition) else condition, value]
return str(Operator("arrayFilter", values))
@staticmethod
def string_concat(value):
return str(Operator("stringConcat", [value]))
@staticmethod
def string_replace(search, replace):
return str(Operator("stringReplace", [search, replace]))
@staticmethod
def toggle():
return str(Operator("toggle", []))
@staticmethod
def date_add_days(days):
return str(Operator("dateAddDays", [days]))
@staticmethod
def date_sub_days(days):
return str(Operator("dateSubDays", [days]))
@staticmethod
def date_set_now():
return str(Operator("dateSetNow", []))