Skip to content

Commit

Permalink
Update utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vstar37 authored Dec 28, 2024
1 parent e9b2fd1 commit e0611e7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ def sort_dict(obj):
def subtract_rules(base_data, subtract_data):
"""从 base_data 中剔除 subtract_data 的规则"""
for key in ["process_name", "domain", "domain_suffix", "ip_cidr", "domain_regex"]:
# 确保 base_data["rules"] 和 subtract_data["rules"] 都是列表
base_set = set(base_data["rules"].get(key, [])) if isinstance(base_data["rules"], dict) else set(base_data["rules"])
subtract_set = set(subtract_data["rules"].get(key, [])) if isinstance(subtract_data["rules"], dict) else set(subtract_data["rules"])
# 获取 base_data 和 subtract_data 中的规则,确保它们是可哈希的
base_list = base_data["rules"].get(key, [])
subtract_list = subtract_data["rules"].get(key, [])

# 将元素转换为可哈希类型(例如元组),避免字典等不可哈希的类型
base_set = set(tuple(item.items()) if isinstance(item, dict) else item for item in base_list)
subtract_set = set(tuple(item.items()) if isinstance(item, dict) else item for item in subtract_list)

# 更新 base_data["rules"][key] 为去重后的列表
base_data["rules"][key] = list(base_set - subtract_set)
Expand All @@ -165,7 +169,6 @@ def save_json(data, filepath):
with open(filepath, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)


def deduplicate_json(data, enable_trie_filtering=False):
"""
对输入的 JSON 数据进行三轮去重操作:
Expand Down Expand Up @@ -313,4 +316,4 @@ def filter_domains_with_trie(domains, domain_suffixes):
print(f"过滤后的 domains: {final_domains}")
print(f"被过滤的数量: {filtered_count}")
'''
'''

0 comments on commit e0611e7

Please sign in to comment.