-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoperators.py
78 lines (57 loc) · 1.39 KB
/
operators.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
#
# Copyright © 2023 Ingram Micro Inc. All rights reserved.
#
from operator import ( # noqa: F401
eq,
ge,
gt,
le,
lt,
ne,
)
from py_rql.constants import (
ComparisonOperators,
ListOperators,
LogicalOperators,
SearchOperators,
)
def and_op(iterable):
return all(iterable)
def or_op(iterable):
return any(iterable)
def not_op(a):
return not a[0]
def in_op(a, b):
return a in b
def out_op(a, b):
return a not in b
def like(a, b):
if b[0] == '*' and b[-1] == '*':
return b[1:-1] in a
if b[0] == '*':
return a.endswith(b[1:])
if b[-1] == '*':
return a.startswith(b[:-1])
return a == b
def ilike(a, b):
return like(a.lower(), b.lower())
def range_op(a, b):
return b[0] <= a <= b[1]
def get_operator_func_by_operator(op):
mapping = {
ComparisonOperators.EQ: eq,
ComparisonOperators.NE: ne,
ComparisonOperators.GE: ge,
ComparisonOperators.GT: gt,
ComparisonOperators.LE: le,
ComparisonOperators.LT: lt,
ListOperators.IN: in_op,
ListOperators.OUT: out_op,
ListOperators.RANGE: range_op,
f'{LogicalOperators.AND}_op': and_op,
f'{LogicalOperators.OR}_op': or_op,
f'{LogicalOperators.NOT}_op': not_op,
SearchOperators.LIKE: like,
SearchOperators.I_LIKE: ilike,
}
return mapping[op]