Skip to content

Commit 8b8eec2

Browse files
feat: 1) support length_not_equal comparison; 2) All length comparators support numbers; 3) Optimize the is_true and is_not_true comparisons
1 parent 7241c06 commit 8b8eec2

15 files changed

Lines changed: 114 additions & 40 deletions

File tree

apps/application/flow/compare/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .len_gt_compare import LenGTCompare
2424
from .len_le_compare import LenLECompare
2525
from .len_lt_compare import LenLTCompare
26+
from .len_not_equal_compare import LenNotEqualCompare
2627
from .lt_compare import LTCompare
2728
from .not_contain_compare import NotContainCompare
2829
from .not_equal_compare import NotEqualCompare
@@ -35,13 +36,16 @@
3536
'is_not_null': IsNotNullCompare(),
3637
'contain': ContainCompare(),
3738
'not_contain': NotContainCompare(),
39+
'regex': RegexCompare(),
40+
'wildcard': WildcardCompare(),
3841
'eq': EqualCompare(),
3942
'not_eq': NotEqualCompare(),
4043
'ge': GECompare(),
4144
'gt': GTCompare(),
4245
'le': LECompare(),
4346
'lt': LTCompare(),
4447
'len_eq': LenEqualCompare(),
48+
'len_not_eq': LenNotEqualCompare(),
4549
'len_ge': LenGECompare(),
4650
'len_gt': LenGTCompare(),
4751
'len_le': LenLECompare(),
@@ -50,8 +54,6 @@
5054
'is_not_true': IsNotTrueCompare(),
5155
'start_with': StartWithCompare(),
5256
'end_with': EndWithCompare(),
53-
'regex': RegexCompare(),
54-
'wildcard': WildcardCompare(),
5557
}
5658

5759

apps/application/flow/compare/is_not_true.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
@Author:虎
55
@file: is_not_true.py
66
@date:2025/4/7 13:44
7-
@desc:
7+
@desc: 不为真比较器
88
"""
99
from .compare import Compare
1010

1111

1212
class IsNotTrueCompare(Compare):
1313

1414
def compare(self, source_value, compare, target_value):
15-
try:
16-
return source_value is False
17-
except Exception:
18-
return False
15+
return source_value not in (True, 'True', 'true', 1, '1')

apps/application/flow/compare/is_true.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
"""
33
@project: MaxKB
44
@Author:虎
5-
@file: IsTrue.py
5+
@file: is_true.py
66
@date:2025/4/7 13:38
7-
@desc:
7+
@desc: 为真比较器
88
"""
99
from .compare import Compare
1010

1111

1212
class IsTrueCompare(Compare):
1313

1414
def compare(self, source_value, compare, target_value):
15-
try:
16-
return source_value is True
17-
except Exception:
18-
return False
15+
return source_value in (True, 'True', 'true', 1, '1')

apps/application/flow/compare/len_equal_compare.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,45 @@
44
@Author:虎
55
@file: equal_compare.py
66
@date:2024/6/7 14:44
7-
@desc:
7+
@desc: 长度等于比较器
88
"""
99
from .compare import Compare
1010

1111

12+
def compute_length(source_value, target_length) -> list:
13+
"""
14+
计算长度
15+
16+
Args:
17+
source_value: 引用变量
18+
target_length: 目标长度字符串
19+
20+
Raises:
21+
ValueError: 当 target_value 不是数字 或 小于0时,抛出该异常
22+
"""
23+
# 获取target_value的长度
24+
target_length = int(target_length) if target_length else 0
25+
if target_length < 0:
26+
raise ValueError("The target length must be greater than or equal to 0")
27+
28+
# 获取source_value的长度
29+
try:
30+
source_length = len(source_value) if source_value is not None else 0
31+
except Exception:
32+
# 可计算数字长度
33+
source_length = len(str(source_value))
34+
35+
return [source_length, target_length]
36+
37+
1238
class LenEqualCompare(Compare):
1339

1440
def compare(self, source_value, compare, target_value):
1541
try:
16-
return len(source_value) == int(target_value)
17-
except Exception as e:
42+
# 计算长度
43+
source_length, target_length = compute_length(source_value, target_value)
44+
45+
# 长度等于 比较
46+
return source_length == target_length
47+
except Exception:
1848
return False

apps/application/flow/compare/len_ge_compare.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
@Author:虎
55
@file: lt_compare.py
66
@date:2024/6/11 9:52
7-
@desc: 大于比较器
7+
@desc: 长度大于等于比较器
88
"""
99
from .compare import Compare
10+
from .len_equal_compare import compute_length
1011

1112

1213
class LenGECompare(Compare):
1314

1415
def compare(self, source_value, compare, target_value):
1516
try:
16-
return len(source_value) >= int(target_value)
17+
# 计算长度
18+
source_length, target_length = compute_length(source_value, target_value)
19+
20+
# 长度大于等于 比较
21+
return source_length >= target_length
1722
except Exception:
1823
return False

apps/application/flow/compare/len_gt_compare.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
@Author:虎
55
@file: lt_compare.py
66
@date:2024/6/11 9:52
7-
@desc: 大于比较器
7+
@desc: 长度大于比较器
88
"""
99
from .compare import Compare
10+
from .len_equal_compare import compute_length
1011

1112

1213
class LenGTCompare(Compare):
1314

1415
def compare(self, source_value, compare, target_value):
1516
try:
16-
return len(source_value) > int(target_value)
17+
# 计算长度
18+
source_length, target_length = compute_length(source_value, target_value)
19+
20+
# 长度大于 比较
21+
return source_length > target_length
1722
except Exception:
1823
return False

apps/application/flow/compare/len_le_compare.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
@Author:虎
55
@file: lt_compare.py
66
@date:2024/6/11 9:52
7-
@desc: 小于比较器
7+
@desc: 长度小于等于比较器
88
"""
99
from .compare import Compare
10+
from .len_equal_compare import compute_length
1011

1112

1213
class LenLECompare(Compare):
1314

1415
def compare(self, source_value, compare, target_value):
1516
try:
16-
return len(source_value) <= int(target_value)
17+
# 计算长度
18+
source_length, target_length = compute_length(source_value, target_value)
19+
20+
# 长度小于等于 比较
21+
return source_length <= target_length
1722
except Exception:
1823
return False

apps/application/flow/compare/len_lt_compare.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
@Author:虎
55
@file: lt_compare.py
66
@date:2024/6/11 9:52
7-
@desc: 小于比较器
7+
@desc: 长度小于比较器
88
"""
99
from .compare import Compare
10+
from .len_equal_compare import compute_length
1011

1112

1213
class LenLTCompare(Compare):
1314

1415
def compare(self, source_value, compare, target_value):
1516
try:
16-
return len(source_value) < int(target_value)
17+
# 计算长度
18+
source_length, target_length = compute_length(source_value, target_value)
19+
20+
# 长度小于 比较
21+
return source_length < target_length
1722
except Exception:
1823
return False
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author: wangliang181230
5+
@file: len_not_equal_compare.py
6+
@date: 2026/4/28 20:17
7+
@desc: 长度不等于比较器
8+
"""
9+
from .compare import Compare
10+
from .len_equal_compare import compute_length
11+
12+
13+
class LenNotEqualCompare(Compare):
14+
15+
def compare(self, source_value, compare, target_value):
16+
try:
17+
# 计算长度
18+
source_length, target_length = compute_length(source_value, target_value)
19+
20+
# 长度不等于 比较
21+
return source_length != target_length
22+
except Exception:
23+
return False

apps/application/flow/compare/regex_compare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
def compile_and_cache(regex):
2424
match = match_cache.get(regex)
2525
if not match:
26-
match = re.compile(regex).fullmatch
26+
match = re.compile(regex, flags=re.DOTALL).fullmatch
2727
match_cache.set(regex, match)
2828
return match
2929

0 commit comments

Comments
 (0)