Skip to content

Commit 35516f5

Browse files
wangdan-fit2cloudwangliang181230
authored andcommitted
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 238c627 commit 35516f5

14 files changed

Lines changed: 97 additions & 24 deletions

File tree

apps/application/flow/compare/__init__.py

Lines changed: 2 additions & 0 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
@@ -42,6 +43,7 @@
4243
'le': LECompare(),
4344
'lt': LTCompare(),
4445
'len_eq': LenEqualCompare(),
46+
'len_not_eq': LenNotEqualCompare(),
4547
'len_ge': LenGECompare(),
4648
'len_gt': LenGTCompare(),
4749
'len_le': LenLECompare(),

apps/application/flow/compare/is_not_true.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@
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 str(source_value).lower() not in ('true', '1')

apps/application/flow/compare/is_true.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@
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 str(source_value).lower() in ('true', '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/wildcard_compare.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
from .compare import Compare
1313
from common.cache.mem_cache import MemCache
1414

15-
1615
match_cache = MemCache('wildcard_to_regex', {
17-
'TIMEOUT': 3600, # 缓存有效期为 1 小时
16+
'TIMEOUT': 3600, # 缓存有效期为 1 小时
1817
'OPTIONS': {
19-
'MAX_ENTRIES': 500, # 最多缓存 500 个条目
20-
'CULL_FREQUENCY': 10, # 达到上限时,删除约 1/10 的缓存
18+
'MAX_ENTRIES': 500, # 最多缓存 500 个条目
19+
'CULL_FREQUENCY': 10, # 达到上限时,删除约 1/10 的缓存
2120
},
2221
})
2322

@@ -26,10 +25,11 @@ def translate_and_compile_and_cache(wildcard):
2625
match = match_cache.get(wildcard)
2726
if not match:
2827
regex = fnmatch.translate(wildcard)
29-
match = re.compile(regex).match
28+
match = re.compile(regex).fullmatch
3029
match_cache.set(wildcard, match)
3130
return match
3231

32+
3333
class WildcardCompare(Compare):
3434

3535
def compare(self, source_value, compare, target_value):

0 commit comments

Comments
 (0)